如果用户已经登录,Angular 登录组件重定向

标签 angular firebase firebase-authentication angular5 angular6

这是我的 Angular 项目的路由器守卫。基本上在我的系统中我有很多用户 Angular 色。在守卫中,如果用户是 Lawyer 和 isLawyerApproved,我会检查 userProfile 节点。如果这些条件成立,律师就可以访问该路由。在 canActivate 函数中,我返回 this.loggedin && this.isApproved。在我的登录组件中,我有一个 ngOnit 函数来检查用户是否已经登录,如果是,则重定向到/dashboard 路由。实际上,这不起作用,我希望如果经过身份验证的用户访问登录页面,自动重定向到/dashboard。请帮助我

class MyGuardService implements CanActivate{
  loggedIn = false;
  isApproved:boolean;

  constructor(private authService:AuthService, private router:Router){
    this.authService.isLogged().subscribe((response)=>{
      if(response && response.uid){
        this.loggedIn = true;
        this.authService.getUserProfile(response.uid).valueChanges()
          .subscribe(
             (userProfile:UserProfile)=>{
                if(userProfile.isLawyer && userProfile.isLawyerApproved){
                   this.isApproved = true;
                }
             }
           )
        }else{
          this.loggedIn = false
        }
      }, (error)=>{
      this.loggedIn = false;
    })
  }

  canActivate(route:ActivatedRouteSnapshot, state:RouterStateSnapshot):Observable<boolean>|Promise<boolean>|boolean {
    console.log('El abogado es aprobado??')
    console.log(this.isApproved)
    if(!this.loggedIn){
      this.router.navigate(['/abogado-login']);
    }
    return (this.loggedIn && this.isApproved)
  }
}

//登录组件

ngOnInit() {
  this.authService.isLogged().subscribe((result)=>{
    if(result && result.uid){
      console.log('Estoy logueado debo redirigir');
      this.router.navigate(['/abogado'])
    }
  })
}

最佳答案

在您的 gaurd 中,只需在登录成功时通过 improting router 添加重定向,或者您的任何条件,并将该 gaurd 放置在路由器模块中的登录路由上。

constructor(private authService:AuthService, private router:Router){
  this.authService.isLogged().subscribe((response)=>{
  if(response && response.uid){
  this.loggedIn = true;

  this.authService.getUserProfile(response.uid).valueChanges()
  .subscribe(
   (userProfile:UserProfile)=>{
    if(userProfile.isLawyer && userProfile.isLawyerApproved){
     this.isApproved = true;
     this.router.navigate(['/addroutewhereusershouldgowhenapproved']);
    } else {
     this.router.navigate(['/abogado']);
   }
  )
}else{
 this.loggedIn = false
}
}, (error)=>{
 this.loggedIn = false;
})

在路由器模块中,在路由路径上添加类似下面的东西。

{ path:'', component: LoginComponent, canActivate: [AuthGuard]}

关于如果用户已经登录,Angular 登录组件重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51941918/

相关文章:

java - 应用数据清除后如何退出Google?

firebase-authentication - 将 firebaseui-web 与 Google Home 结合使用时出现 "Cannot parse context"错误

Angular 使用 Karma 和 Jasmine TypeError : this. Service.<foo> 不是 Lifecycle Hook 中的函数

javascript - 在 Ionic 中从 Firebase 获取列表期间 $ionicLoading 未显示

android - 如何将 Pushid 与 Uid Firebase android 一起使用

ios - 未找到框架 GoogleToolboxForMac

firebase - FCM 注册 ID token 的最大长度是多少?

javascript - Angular 2 : Promise return

javascript - 无法绑定(bind)到 'ngModel',因为它不是 'input' 的已知属性

node.js - 由 : org. gradle.api.InvalidUserDataException 引起:无法添加任务 ':client:test',因为同名任务已存在