-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth-gaurd.service.ts
59 lines (45 loc) · 1.57 KB
/
auth-gaurd.service.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { Injectable } from '@angular/core';
import { MatDialog } from '@angular/material/dialog';
import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot } from '@angular/router';
import { Observable } from 'rxjs';
import { AuthService } from 'src/app/services/auth/auth.service';
@Injectable({
providedIn: 'root'
})
export class AuthGaurdService implements CanActivate {
constructor(private _authService: AuthService,
private _router: Router,
private dialog: MatDialog) {
}
canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
if (this._authService.isvaidRoute()) {
return true;
}
// navigate to error page
this._router.navigate(['/restricted']);
return false;
}
canDeactivate(
currentRoute: ActivatedRouteSnapshot,
currentState: RouterStateSnapshot,
nextState: RouterStateSnapshot,
State: RouterStateSnapshot
) {
// Trying to implement adding a custom dialogbox inside canDeactivate
// const dialogRef = this.dialog.open(ComfirmDialogComponent, {
// width: '500px'
// });
// dialogRef.componentInstance.confirmText = "Are you sure, you want to leave? Chuck has lot more tricks up his sleave.";
// if( dialogRef.afterClosed().map(result => {
// return result;
// })){
// return true;
// };
// return false;
if (confirm('Are you sure, you want to leave? Chuck has lot more tricks up his sleave.')) {
return true;
} else {
return false;
}
}
}