我已成功与 angular-6-social-login ( https://www.npmjs.com/package/angular-6-social-login ) 集成以使用 google 帐户登录我的 angular APP。
我正在创建一个 Angular Guard 来保护路由,以便只有用户在登录后才能访问。来自 angular-6-social-login 的 AuthService 不支持任何直接功能来查看用户是否已登录。请问这里有什么建议吗?
import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router } from '@angular/router';
import { Observable } from 'rxjs';
import { AuthService } from 'angular-6-social-login';
@Injectable({
providedIn: 'root'
})
export class LoginGuardGuard implements CanActivate {
constructor(private authService: AuthService, private router: Router) {}
canActivate(nextRoute: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
const requiresLogin = nextRoute.data.requiresLogin || false;
console.log("AccessGuard canActivate is being called")
// Check that the user is logged in...
//TODO: need help with the if condition
if (!<userLoggedin>) {
this.router.navigate(['login'])
return false;
}
return true;
}
}