Example project using adal-angular4 for authentication to Azure Active Directory tenent.
12-18-2018: Updated all components, Tested to Work with adal-angular4 major version 4 12-11-2018: Updated to Angular 7, Improved README
npm install -g @angular/cli@latest
ng new adal-angular6-example --routing
cd adal-angular6-example
ng add @angular/material
ng g module Material
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { MatButtonModule, MatToolbarModule } from '@angular/material';
@NgModule({
imports: [
CommonModule,
MatButtonModule,
MatToolbarModule
],
exports: [
MatButtonModule,
MatToolbarModule
],
declarations: []
})
export class MaterialModule { }
import {MaterialModule} from './material/material.module';
imports: [
...
MaterialModule,
...
],
ng g component Home
ng g component NotFound
ng g component Toolbar
...
import { HomeComponent } from './home/home.component';
import { NotFoundComponent } from './not-found/not-found.component';
...
...
const routes: Routes = [
{ path: '', component: HomeComponent},
{ path: '**', component: NotFoundComponent }
];
...
npm install --save adal-angular4
...
import { AdalService, AdalGuard, AdalInterceptor } from 'adal-angular4';
...
providers: [
...
AdalService,
AdalGuard,
{ provide: HTTP_INTERCEPTORS, useClass: AdalInterceptor, multi: true }],
...
...
{ path: '', component: HomeComponent, canActivate: [AdalGuard] },
...
export const environment = {
...
config: {
tenant: 'careportfol.io',
clientId: '14c39115-7cae-4c02-b865-20d7b2d205f8',
endpoints: {
'https://graph.microsoft.com': '00000003-0000-0000-c000-000000000000'
}
}
...
};
...
import { AdalService } from 'adal-angular4';
import { environment } from '../environments/environment';
...
...
export class AppComponent {
constructor(private adalService: AdalService) {
adalService.init(environment.config);
}
...
<mat-toolbar color="primary">
<span>ADAL Example for Angular 6</span>
<!-- This fills the remaining space of the current row -->
<span class="fill-remaining-space"></span>
<span *ngIf="!authenticated" (click)="login()">
<button mat-raised-button>Login</button>
</span>
<span *ngIf="authenticated" (click)="logout()">
<button mat-raised-button>Logout</button>
</span>
</mat-toolbar>
import { Component, OnInit } from '@angular/core';
import { AdalService } from 'adal-angular4';
@Component({
selector: 'app-toolbar',
templateUrl: './toolbar.component.html',
styleUrls: ['./toolbar.component.css']
})
export class ToolbarComponent implements OnInit {
constructor(private adalService: AdalService) { }
ngOnInit() {
this.adalService.handleWindowCallback();
console.log(this.adalService.userInfo);
}
login() {
this.adalService.login();
}
logout() {
this.adalService.logOut();
}
get authenticated(): boolean {
return this.adalService.userInfo.authenticated;
}
}
<app-toolbar></app-toolbar>
<router-outlet></router-outlet>