2017-01-27 42 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']); 
     } 

    }