-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.guard.ts
31 lines (25 loc) · 915 Bytes
/
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
import { Injectable } from '@angular/core';
import { AngularFireAuth } from '@angular/fire/auth';
import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot, UrlTree } from '@angular/router';
import { Observable } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class AuthGuard implements CanActivate {
constructor(private router: Router, private afAuth: AngularFireAuth) {
}
canActivate(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
return new Promise((resolve, reject) => {
this.afAuth.onAuthStateChanged((user) => {
if (user) {
resolve(true);
} else {
this.router.navigate(['../login']);
resolve(false);
}
});
});
}
}