使用 Angular 2 Meteor 的登录逻辑?
Login Logic With Angular 2 Meteor?
我想实现典型的 login/register 设置:
- 阻止用户在未登录的情况下访问 /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]}
。但是,这不会阻止用户在登录后访问 login/register 页面。我想知道是否有更好的方法来执行此操作,而无需创建另一个单独的 Injectable。
**注意 - 我没有使用 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']);
}
}
我想实现典型的 login/register 设置:
- 阻止用户在未登录的情况下访问 /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]}
。但是,这不会阻止用户在登录后访问 login/register 页面。我想知道是否有更好的方法来执行此操作,而无需创建另一个单独的 Injectable。
**注意 - 我没有使用 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']);
}
}