我想实现典型的登录/注册设置:
- 阻止用户在未登录的情况下访问/app 以及/app 的所有子级
- 阻止用户在登录后访问/login 和/register
现在,我使用的设置如下所示:
@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);
}
}
我放入了我的路线,我放入了{path: 'app', component: TasksListComponent, canActivate: [LoggedInGuard]}
。但是,这不会阻止用户在登录后访问登录/注册页面。我想知道是否有更好的方法来做到这一点,而无需创建另一个单独的可注入(inject)。
**注意 - 我没有使用 Iron Router,我使用的是 @angular/router
最佳答案
如果用户登录,您可以将他们重定向到其他组件。只需将其放在 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']);
}
}
关于angular - 使用 Angular 2 Meteor 进行登录逻辑?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41904070/