-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathauth.guard.ts
executable file
·33 lines (30 loc) · 1.17 KB
/
auth.guard.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import {Injectable} from '@angular/core';
import {CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router} from '@angular/router';
import Auth from '@aws-amplify/auth';
import {Logger} from "@aws-amplify/core";
import {Observable} from 'rxjs';
const log = new Logger('auth-guard');
/**
* THe AuthGuard prevents access to routes without prior authentication. If the user
* is not authenticated they will be redirected to auth/signin route.
*
* https://angular.io/guide/router
* https://aws-amplify.github.io/docs/js/authentication
*/
@Injectable({
providedIn: "root"
})
export class AuthGuard implements CanActivate {
constructor(private _router: Router) { }
canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
return Auth.currentAuthenticatedUser().then(() => { return true; })
.catch((e) => {
log.debug(e);
this._router.navigate(["auth/signin"],
{queryParams: {_return: location.href}, queryParamsHandling: "merge"});
return false;
});
}
}