Skip to content

Instantly share code, notes, and snippets.

@Mustafa-Omran
Created February 21, 2022 09:03
Show Gist options
  • Save Mustafa-Omran/4adebc7a94a97f596c391e7325bf076b to your computer and use it in GitHub Desktop.
Save Mustafa-Omran/4adebc7a94a97f596c391e7325bf076b to your computer and use it in GitHub Desktop.
Angular- Auth Guard
@Injectable({
providedIn: 'root'
})
export class AuthGuard implements CanActivate {
constructor(
private readonly router: Router) {
}
canActivate(): Observable<boolean> | Promise<boolean> | boolean {
if (this.isLoggedIn) {
return true;
}
this.router.navigate([Routes.Login]);
return false;
}
get isLoggedIn(): boolean {
if (localStorage.getItem(AuthConstants.UserKey) !== null) {
return true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment