Created
July 26, 2020 02:32
-
-
Save changhuixu/3e755438985b029e888fc619c60d3179 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Injectable({ | |
providedIn: 'root', | |
}) | |
export class AuthGuard implements CanActivate { | |
constructor(private router: Router, private authService: AuthService) {} | |
canActivate( | |
next: ActivatedRouteSnapshot, | |
state: RouterStateSnapshot | |
): | |
| Observable<boolean | UrlTree> | |
| Promise<boolean | UrlTree> | |
| boolean | |
| UrlTree { | |
return this.authService.user$.pipe( | |
map((user) => { | |
if (user) { | |
return true; | |
} else { | |
this.router.navigate(['login'], { | |
queryParams: { returnUrl: state.url }, | |
}); | |
return false; | |
} | |
}) | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment