Created
February 21, 2022 09:03
-
-
Save Mustafa-Omran/4adebc7a94a97f596c391e7325bf076b to your computer and use it in GitHub Desktop.
Angular- Auth Guard
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 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