Skip to content

Instantly share code, notes, and snippets.

@jesuslpm
Created July 12, 2019 08:17
Show Gist options
  • Save jesuslpm/d9b6b26ef9b4e365c71283a3131b6c22 to your computer and use it in GitHub Desktop.
Save jesuslpm/d9b6b26ef9b4e365c71283a3131b6c22 to your computer and use it in GitHub Desktop.
Using Session Storage to navigate to the initial page after login into Azure AD
import { Injectable } from '@angular/core';
import { Router, CanActivate, CanActivateChild, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { AdalService } from 'adal-angular4';
@Injectable()
export class AuthenticationGuard implements CanActivate, CanActivateChild {
constructor(
private router: Router,
private adal: AdalService
) { }
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
if (this.adal.userInfo.authenticated) {
return true;
}
else {
this.router.navigate(['/login'], { queryParams: { returnUrl: state.url } });
return false;
}
}
canActivateChild(childRoute: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
return this.canActivate(childRoute, state);
}
}
<p>
Click on the following button if login window doesn't show automatically beacuse
the browser blocked the popup.
</p>
<button (click)="login()">Login Azure Active Directory</button>
<p>
If you want the popup to show up automatically, please enable popups on this site.
</p>
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { AdalService } from 'adal-angular4';
import { environment } from '../../../environments/environment';
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {
constructor(private route: ActivatedRoute, private router: Router, private adal: AdalService) {
}
login() {
var context: any = this.adal["context"];
this.adal.login();
this.checkLogin();
}
checkLogin() {
const returnUrl = this.route.snapshot.queryParams['returnUrl'] || sessionStorage.returnUrl || '/';
var context: any = this.adal["context"];
var intervalHandle = window.setInterval(() => {
if (context._user) {
this.adal.refreshDataFromCache();
this.adal.acquireToken(context.config.loginResource).subscribe(token => {
clearTimeout(intervalHandle);
this.adal.userInfo.authenticated = true;
this.adal.userInfo.token = token;
this.router.navigate([returnUrl]);
}, error => {
console.error("Failed to acquire token for resource ", context.config.loginResource, error);
});
this.adal.acquireToken(this.adal.getResourceForEndpoint(environment.apiUrl)).subscribe(token => {
}, error => {
console.error("Failed to acquire token for resource ", context.config.loginResource, error);
});
}
}, 500);
}
ngOnInit() {
console.log("returnUrl %s", sessionStorage.returnUrl);
const returnUrl = this.route.snapshot.queryParams['returnUrl'] || sessionStorage.returnUrl || '/';
if (this.adal.userInfo.authenticated) {
this.router.navigate([returnUrl]);
}
else {
sessionStorage.returnUrl = returnUrl;
this.login();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment