I would like to provide a server-side authentication before I give access to a specific route in angular.
I have a AuthGuard which implements CanActivate and a servi
You might do bit of changes as follows,
Your service,
@Injectable()
export class LoginService {
public isUserLoggedIn: boolean;
constructor(
private router: Router,
private auth: AuthService) {
this.isUserLoggedIn = false;
}
public loginService(data): void {
this.auth.isAuthentication(data)
.subscribe((response) => {
if (response) {
this.isUserLoggedIn = true;
}else {
this.router.navigate(['login']);
}
});
}
public getUserLoggedIn() {
return this.isUserLoggedIn;
}
and then,
@Injectable()
export class AuthGuard implements CanActivate {
constructor(private login: LoginService) { }
canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable | Promise | boolean {
return this.login.getUserLoggedIn();
}
}