Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular AuthGuard阻止登录_Angular_Firebase_Firebase Authentication_Angularfire2 - Fatal编程技术网

Angular AuthGuard阻止登录

Angular AuthGuard阻止登录,angular,firebase,firebase-authentication,angularfire2,Angular,Firebase,Firebase Authentication,Angularfire2,当我想一直登录到authguard系统时,会阻止此登录并显示指定的消息。如何修复它?它应该被阻止,只有当我想去一个未经授权的网页,我设置了一个守卫,而没有登录 身份验证服务: export class AuthService { private user: Observable<firebase.User>; authState: any; constructor(private afAuth: AngularFireAuth, private router:

当我想一直登录到authguard系统时,会阻止此登录并显示指定的消息。如何修复它?它应该被阻止,只有当我想去一个未经授权的网页,我设置了一个守卫,而没有登录

身份验证服务:

export class AuthService {
  private user: Observable<firebase.User>;
  authState: any;

  constructor(private afAuth: AngularFireAuth,
    private router: Router) {
    this.user = afAuth.authState;
  }

  authUser() {
    return this.user;
  }

  get isLoggedIn(): boolean {
    const user = JSON.parse(localStorage.getItem('users'));
    return (user !== null) ? true : false;
  }

  currentUserId() {
    return this.afAuth.authState
  }

  signIn(email: string, password: string) {
    return this.afAuth.auth.signInWithEmailAndPassword(email, password).then(
      (user) => {
        this.authState = user;
        this.setUserStatus('online');
        this.router.navigate(['chat']);
      }
    );
  }

}
导出类身份验证服务{
私人用户:可观察;
国家:任何;
构造函数(专用afAuth:AngularFireAuth,
专用路由器(路由器){
this.user=afAuth.authState;
}
authUser(){
返回此.user;
}
get isLoggedIn():布尔值{
const user=JSON.parse(localStorage.getItem('users');
返回(用户!==null)?真:假;
}
currentUserId(){
返回this.afAuth.authState
}
登录(电子邮件:字符串,密码:字符串){
使用email和password(电子邮件,密码)返回此.afAuth.auth.Signin。然后(
(用户)=>{
this.authState=用户;
此.setUserStatus('online');
这个.router.navigate(['chat']);
}
);
}
}
auth.guard:

export class AuthGuard implements CanActivate {
  constructor(private auth: AuthService, private router: Router) { }

  canActivate(
    next: ActivatedRouteSnapshot,
    state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
    if (this.auth.isLoggedIn !== true) {
      window.alert('Test!')
      this.router.navigate(['sign-in'])
    }
    return true;
  }
}
导出类AuthGuard实现CanActivate{
构造函数(私有身份验证:身份验证服务,私有路由器:路由器){}
激活(
下一步:ActivatedRouteSnapshot,
状态:RouterStateSnashot):可观察的|承诺|布尔值{
if(this.auth.isLoggedIn!==true){
window.alert('Test!')
this.router.navigate(['sign-in'])
}
返回true;
}
}
auth.service.ts auth.guard:
导出类AuthGuard实现CanActivate{
构造函数(私有身份验证:身份验证服务,私有路由器:路由器){}
激活(
下一步:ActivatedRouteSnapshot,
状态:RouterStateSnashot):可观察的|承诺|布尔值{
// 

如果(this.auth.isLoggedIn()!==true){//为什么不在
auth.service.ts
中返回用户!==null;
 isLoggedIn(): boolean {
    const user = JSON.parse(localStorage.getItem('users'));
    return (user !== null) ? true : false;
  }
export class AuthGuard implements CanActivate {
  constructor(private auth: AuthService, private router: Router) { }

  canActivate(
    next: ActivatedRouteSnapshot,
    state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
     // 
    if (this.auth.isLoggedIn() !== true) { // <====
      window.alert('Test!')
      this.router.navigate(['sign-in'])
       return false;
    } 
    return true;
  }
}