Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit 9ba431c

Browse files
committed
using Adal.js with Angular2
1 parent da68a1f commit 9ba431c

15 files changed

+229
-7
lines changed

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"scripts": {
66
"build": "webpack --progress",
77
"watch": "npm run build -- --watch",
8-
"server": "webpack-dev-server --inline --progress --port 3000 --content-base src",
8+
"server": "webpack-dev-server --inline --progress --port 5000 --content-base src",
99
"start": "npm run server"
1010
},
1111
"contributors": [
@@ -25,15 +25,18 @@
2525
"@angular/platform-server": "~2.2.1",
2626
"@angular/router": "~3.2.1",
2727
"@angular/upgrade": "~2.2.1",
28+
"adal-angular": "^1.0.14",
2829
"angular2-in-memory-web-api": "0.0.21",
2930
"bootstrap": "^3.3.7",
3031
"core-js": "^2.4.1",
32+
"expose-loader": "^0.7.3",
3133
"ie-shim": "^0.1.0",
3234
"reflect-metadata": "^0.1.3",
3335
"rxjs": "5.0.0-beta.12",
3436
"zone.js": "~0.6.26"
3537
},
3638
"devDependencies": {
39+
"@types/adal": "^1.0.26",
3740
"@types/node": "^6.0.48",
3841
"angular2-router-loader": "^0.3.4",
3942
"angular2-template-loader": "^0.6.0",

src/app/app.component.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ <h3>
33
</h3>
44
<nav>
55
<a [routerLink]="['/']">
6+
Login
7+
</a>
8+
|
9+
<a [routerLink]="['/home']">
610
Home
711
</a>
812
|

src/app/app.module.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import { OAuthCallbackHandler } from './login-callback/oauth-callback.guard';
2+
import { OAuthCallbackComponent } from './login-callback/oauth-callback.component';
3+
import { OAuthHandshakeModule } from './login-callback/oauth-callback.module';
4+
import { SharedServicesModule } from './services/shared.services.module';
15
import { NgModule } from '@angular/core'
26
import { RouterModule } from '@angular/router';
37
import { rootRouterConfig } from './app.routes';
@@ -14,6 +18,7 @@ import { RepoListComponent } from './github/repo-list/repo-list.component';
1418
import { RepoDetailComponent } from './github/repo-detail/repo-detail.component';
1519
import { LocationStrategy, HashLocationStrategy } from '@angular/common';
1620
import { ContactComponent } from './contact/contact.component';
21+
import { LoginComponent } from './login/login.component';
1722

1823
@NgModule({
1924
declarations: [
@@ -23,17 +28,20 @@ import { ContactComponent } from './contact/contact.component';
2328
RepoListComponent,
2429
RepoDetailComponent,
2530
HomeComponent,
26-
ContactComponent
31+
ContactComponent,
32+
LoginComponent
2733
],
2834
imports: [
2935
BrowserModule,
3036
FormsModule,
3137
ReactiveFormsModule,
3238
HttpModule,
39+
OAuthHandshakeModule,
40+
SharedServicesModule,
3341
RouterModule.forRoot(rootRouterConfig, { useHash: true })
3442
],
3543
providers: [
36-
GithubService
44+
GithubService
3745
],
3846
bootstrap: [ AppComponent ]
3947
})

src/app/app.routes.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
import { Routes } from '@angular/router';
23

34
import { AboutComponent } from './about/about.component';
@@ -6,15 +7,23 @@ import { RepoBrowserComponent } from './github/repo-browser/repo-browser.compone
67
import { RepoListComponent } from './github/repo-list/repo-list.component';
78
import { RepoDetailComponent } from './github/repo-detail/repo-detail.component';
89
import { ContactComponent } from './contact/contact.component';
10+
import { LoginComponent } from './login/login.component';
11+
import { OAuthCallbackHandler } from './login-callback/oauth-callback.guard';
12+
import { OAuthCallbackComponent } from './login-callback/oauth-callback.component';
13+
import { AuthenticationGuard } from "./services/authenticated.guard";
914

1015
export const rootRouterConfig: Routes = [
11-
{ path: '', redirectTo: 'home', pathMatch: 'full' },
12-
{ path: 'home', component: HomeComponent },
16+
{ path: '', redirectTo: 'login', pathMatch: 'full' },
17+
{ path: 'home', component: HomeComponent, canActivate: [AuthenticationGuard] },
1318
{ path: 'about', component: AboutComponent },
14-
{ path: 'github', component: RepoBrowserComponent,
19+
{ path: 'login', component: LoginComponent },
20+
{ path: 'id_token', component: OAuthCallbackComponent, canActivate: [OAuthCallbackHandler] },
21+
{
22+
path: 'github', component: RepoBrowserComponent, canActivate: [AuthenticationGuard],
1523
children: [
1624
{ path: '', component: RepoListComponent },
17-
{ path: ':org', component: RepoListComponent,
25+
{
26+
path: ':org', component: RepoListComponent,
1827
children: [
1928
{ path: '', component: RepoDetailComponent },
2029
{ path: ':repo', component: RepoDetailComponent }

src/app/home/home.component.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { AdalService } from './../services/adal.service';
12
import {Component} from '@angular/core';
23

34
@Component({
@@ -6,4 +7,10 @@ import {Component} from '@angular/core';
67
templateUrl: './home.component.html'
78
})
89
export class HomeComponent {
10+
constructor(private adalService: AdalService){
11+
console.log('User info from JWT');
12+
console.log(this.adalService.userInfo);
13+
console.log('JWT Token');
14+
console.log(this.adalService.accessToken);
15+
}
916
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { Component, OnInit } from '@angular/core';
2+
import { Router } from '@angular/router';
3+
4+
import { AdalService } from './../services/adal.service';
5+
6+
@Component({
7+
template: '<div>Please wait...</div>'
8+
})
9+
export class OAuthCallbackComponent implements OnInit {
10+
constructor(private router: Router, private adalService: AdalService) {
11+
12+
}
13+
14+
ngOnInit() {
15+
if (!this.adalService.userInfo) {
16+
this.router.navigate(['login']);
17+
} else {
18+
this.router.navigate(['home']);
19+
}
20+
}
21+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { Injectable } from '@angular/core';
2+
import { Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
3+
4+
import { AdalService } from './../services/adal.service';
5+
6+
@Injectable()
7+
export class OAuthCallbackHandler implements CanActivate {
8+
constructor(private router: Router, private adalService: AdalService) {
9+
10+
}
11+
12+
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
13+
14+
this.adalService.handleCallback();
15+
16+
if (this.adalService.userInfo) {
17+
18+
var returnUrl = route.queryParams['returnUrl'];
19+
if (!returnUrl) {
20+
this.router.navigate(['home']);
21+
} else {
22+
this.router.navigate([returnUrl], { queryParams: route.queryParams });
23+
}
24+
}
25+
else {
26+
this.router.navigate(['login']);
27+
}
28+
29+
return false;
30+
}
31+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { NgModule } from '@angular/core';
2+
import { OAuthCallbackComponent } from './oauth-callback.component';
3+
import { OAuthCallbackHandler } from './oauth-callback.guard';
4+
5+
@NgModule({
6+
imports: [],
7+
declarations: [ OAuthCallbackComponent],
8+
providers: [OAuthCallbackHandler]
9+
})
10+
export class OAuthHandshakeModule { }

src/app/login/login.component.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<h3>Login</h3>
2+
<div *ngIf="!isLoggedIn">
3+
<button (click)="login()">Login with my AAD account</button>
4+
</div>
5+
6+
<div *ngIf="isLoggedIn">
7+
<button (click)="logout()">Logout</button>
8+
</div>

src/app/login/login.component.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { Router } from '@angular/router';
2+
import { AdalService } from './../services/adal.service';
3+
import { Component, OnInit } from '@angular/core';
4+
5+
@Component({
6+
templateUrl: './login.component.html'
7+
})
8+
export class LoginComponent implements OnInit {
9+
10+
constructor(private router: Router, private adalService: AdalService) { }
11+
12+
ngOnInit() {
13+
console.log(this.adalService.userInfo);
14+
}
15+
16+
login() {
17+
this.adalService.login();
18+
}
19+
20+
logout() {
21+
this.adalService.logout();
22+
}
23+
24+
public get isLoggedIn() {
25+
return this.adalService.isAuthenticated;
26+
}
27+
}

0 commit comments

Comments
 (0)