-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathunauth.guard.ts
executable file
·36 lines (35 loc) · 1.05 KB
/
unauth.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
34
35
36
import {Injectable, NgZone} from '@angular/core';
import {
CanActivate,
ActivatedRouteSnapshot,
RouterStateSnapshot,
Router, ActivatedRoute
} from '@angular/router';
import Auth from '@aws-amplify/auth';
import {Observable} from 'rxjs';
/**
* Prevents the user from accessing signup/signin pages when they are already authenticated.
* This is used as a guard around those routes.
*
* https://angular.io/guide/router
* https://aws-amplify.github.io/docs/js/authentication
*
*/
@Injectable({
providedIn: 'root'
})
export class UnauthGuard implements CanActivate {
constructor(private _router: Router, private _route: ActivatedRoute) { }
canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
return Auth.currentAuthenticatedUser()
.then(() => {
window.location.replace(this._route.snapshot.queryParams["_return"]);
return false;
})
.catch(() => {
return true;
});
}
}