angular - 使用 Angular 2 Meteor 进行登录逻辑?

标签 angular meteor authentication angular-meteor angular2-meteor

我想实现典型的登录/注册设置:

  • 阻止用户在未登录的情况下访问/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/

相关文章:

ios - 使用 Apple 登录 : Possibility To Create Duplicate Account Issue

swift - 从 SharedWebCredential 获取所有登录信息

bash: ngc: 找不到命令

Angular/Spring Boot/Azure Ingress - URL 重写

javascript - Iron-Router 在 Meteor 中随机返回未定义

javascript - 有什么更好的方式来拥有独特的用户房间

c# - mvc5 全局认证检查

css - 以 Angular 为细节 DIV 提供滑动动画

javascript - 在 Angular 2 中找不到引用的文件

javascript - Mongo/Meteor 游标更新事件