【问题标题】:Angular routing after authentication身份验证后的角度路由
【发布时间】:2017-04-25 16:32:27
【问题描述】:

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

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

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

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

【问题讨论】:

标签: javascript angular typescript angular2-routing


【解决方案1】:

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

将用户导航到家

【讨论】:

    【解决方案2】:

    假设您正在使用令牌(如 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']);
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多