Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Create AuthGuardService
import { Injectable } from '@angular/core';
import { CanActivate, Router, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { AuthService } from './auth.service';
@Injectable({
providedIn: 'root'
})
export class AuthGuardService implements CanActivate {
constructor(private authService: AuthService, private router: Router) {}
// the Router call canActivate() method,
// if canActivate is registered in Routes[]
canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise<boolean> {
// here we check if user is logged in or not
// the authService returs user object, or
// it returns undefined/null when user is not logged in
// SINCE OUR 'authService.user' IS OF TYPE 'Observable'
// WE MUST USE 'subscribe' TO GET VALUE OF 'user'
return new Promise((resolve, reject) => {
this.authService.user.subscribe((user) => {
// here we check if user is logged in or not
// the authService returs user object, or
// it returns undefined/null when user is not logged in
if (!user) {
// just return false - if user is not logged in
return resolve(false);
} else {
// just return true - if user is logged in
return resolve(true);
}
});
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.