2017-01-27 44 views
2

我想实现一个典型的登录/注册设置:使用Angular 2 Meteor登录逻辑?

  • 访问/应用程序防止用户和/应用程序的所有的孩子,如果他们没有在
  • 阻止用户登录访问/登录和/注册,如果他们登录

现在,我使用的设置,看起来是这样的:

@Injectable() 
export class LoggedInGuard implements CanActivate, CanActivateChild { 
    canActivate(...): boolean { 
     if (Meteor.userId() != null) { 
      return true; 
     } 
     this.router.navigate(['/login']); 
     return false; 
    } 
    canActivateChild(...): boolean { 
     return this.canActivate(childRoute, state); 
    } 
} 

,我把我的路线s,我把{path: 'app', component: TasksListComponent, canActivate: [LoggedInGuard]}。但是,这并不妨碍用户在登录时访问登录/注册页面。我想知道是否有更好的方法来完成此操作,而不创建另一个单独的Injectable。

**注 - 我不使用铁路由器,我使用@角/路由器

回答

2

,如果他们是登录您可以重定向用户到其他组件。把这首先放在ngOnInit()中。

ngOnInit() { 
//*** checking if user is already login if login redirect to someother page based on your custom condition 
     if (Meteor.userId()) { 
      this._router.navigate(['otherpage']); 
     } 

    }