代码之家  ›  专栏  ›  技术社区  ›  Johan Rin

auth0重定向到http://localhost:4200/直接登录后为空

  •  0
  • Johan Rin  · 技术社区  · 8 年前

    我尝试使用auth0实现访问管理 this tutorial

    http://localhost:4200/null

    当我期望根路径 http://localhost:4200

    我查了一下 repo of the tutorial 确认我的密码,但我没发现什么特别的。也许你对解决这个问题有什么意见?

    在我的代码下面:

    授权服务.ts

    export class AuthService {
    
      / ... /
    
    handleAuth() {
      // When Auth0 hash parsed, get profile
      this.auth0.parseHash((err, authResult) => {
        if (authResult && authResult.accessToken) {
          window.location.hash = '';
          this.getProfile(authResult);
        } else if (err) {
          this.clearRedirect();
          this.router.navigate(['/']);
          console.error(`Error authenticating: ${err.error}`);
        }
      });
    }
    
    private getProfile(authResult) {
      this.loggingIn = true;
      // Use access token to retrieve user's profile and set session
      this.auth0.client.userInfo(authResult.accessToken, (err, profile) => {
        if (profile) {
          this.setSession(authResult, profile);
          this.redirect();
        } else if (err) {
          console.warn(`Error retrieving profile: ${err.error}`);
        }
      });
    }
    
    private redirect() {
      const redirect = decodeURI(localStorage.getItem('authRedirect'));
      const navArr = [redirect || '/'];
    
      this.router.navigate(navArr);
      // Redirection completed; clear redirect from storage
      this.clearRedirect();
    

    const navArr = [redirect || '/']; 应导航到存储的重定向URL或主页。但在我的例子中,它重定向到 http://localhost:4200/null

    2 回复  |  直到 8 年前
        1
  •  0
  •   Enrico    8 年前

    问题是你的localstorage对象 authRedirect http://localhost:4200/null

    您应该检查该值是否为空,并将用户重定向到主页( / )当它是。

     private _redirect() {
        var redirect = localStorage.getItem('authRedirect') && decodeURI(localStorage.getItem('authRedirect'));
    
    
        if (!redirect || redirect == null)
        {
          //redirect is null so set route to homepage
          redirect = "/";
        }
    
        const navArr = [redirect || '/'];
        this.router.navigate(navArr);
        // Redirection completed; clear redirect from storage
        this._clearRedirect();
      }
    
        2
  •  -1
  •   Johan Rin    7 年前

    {
      path: '**',
      redirectTo: '',
      pathMatch: 'full'
    }
    
    推荐文章