closes #1905 Added section for RouterLinkActive Added section for global query params and fragments Added section for RouterState Added wildcard route to example configuration Updated code samples Renamed .guard files to .service Renamed interfaces.ts to can-deactivate-guard.service.ts Removed unused files
33 lines
1010 B
TypeScript
Executable File
33 lines
1010 B
TypeScript
Executable File
// #docregion
|
|
import { Injectable } from '@angular/core';
|
|
import { CanActivate, Router,
|
|
ActivatedRouteSnapshot,
|
|
RouterStateSnapshot } from '@angular/router';
|
|
import { AuthService } from './auth.service';
|
|
|
|
@Injectable()
|
|
export class AuthGuard implements CanActivate {
|
|
constructor(private authService: AuthService, private router: Router) {}
|
|
|
|
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
|
|
if (this.authService.isLoggedIn) { return true; }
|
|
|
|
// Store the attempted URL for redirecting
|
|
this.authService.redirectUrl = state.url;
|
|
|
|
// Create a dummy session id
|
|
let sessionId = 123456789;
|
|
|
|
// Set our navigation extras object
|
|
// that contains our global query params and fragment
|
|
let navigationExtras = {
|
|
queryParams: { 'session_id': sessionId },
|
|
fragment: 'anchor'
|
|
};
|
|
|
|
// Navigate to the login page with extras
|
|
this.router.navigate(['/login'], navigationExtras);
|
|
return false;
|
|
}
|
|
}
|