Skip to content

Instantly share code, notes, and snippets.

@changhuixu
Created July 26, 2020 02:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save changhuixu/3e755438985b029e888fc619c60d3179 to your computer and use it in GitHub Desktop.
Save changhuixu/3e755438985b029e888fc619c60d3179 to your computer and use it in GitHub Desktop.
@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