Angular 认证后路由

Angular routing after authentication

实际上,在 Angular js 2 上的复杂 Web 应用程序中成功登录身份验证后,我正在努力寻找正确的路由。我正在关注 link:

https://github.com/auth0-blog/angular2-authentication-sample

我理解代码,但问题是在成功登录后,如果我再次将 url 更改为 /login 再次将我带到我有登录表单的登录屏幕,但我担心的是为什么我被重定向登录屏幕,虽然我知道当前的代码是这样做的,但我该如何解决这个问题并以正确的方式实现导航。

请帮助实现相同目标的正确方法是什么。

在登录内部添加一个条件来检查 id_token 是否存在于 localstorage 中并且它不是未定义的。如果存在,请使用 this.router.navigate(['home']);

将用户导航到主页

假设您正在使用令牌(如 jwt)和 localstorage,您可以这样做:

  //This could be different, but you must save somewhere the token.
    // It will depend on your startegy
// For example, when loggin in, you may save it on localstorage:
    localStorage.setItem('currentUser', JSON.stringify(user)); // just an example. 

    //THEN, you can redirect using the router
    this.router.navigate(['yourRoute']);

希望对您有所帮助!请告诉我您是否需要更详细的示例。