auth0错误 - 登录后,重定向回登录页面

时间:2017-12-16 17:25:15

标签: reactjs auth0

我正在使用auth0。我登录并返回登录页面。在网址中,我看到了这个

http://localhost:3000/login#access_token=TecYZ7snyHV-eIHScHWNFm885bR9akp2&expires_in=7200&token_type=Bearer&state=af6AJGNuu726MuTgk6iGUdFBfM4tmtBX&id_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImtpZCI6IlJEUTNPVFl6UmpCRE9UQkVSVEZCTWpFek16ZzFOemMxTWtJMVFqazJRelJDTmpFd01UZ3dNdyJ9.eyJpc3MiOiJodHRwczovL2FwcDExNjMuYXV0aDAuY29tLyIsInN1YiI6ImF1dGgwfDVhMzQwYjk0MTk3YzRmNjhmMDA4M2UzYSIsImF1ZCI6IjBaeGhtREtyb2p5YTFqODVrUHNRRWRVZ1hVdm1LZFlyIiwiaWF0IjoxNTEzNDQzOTQ5LCJleHAiOjE1MTYwNzM2OTUsImF0X2hhc2giOiJiU1gxa3Z5ZzJSVEYxVXFLdTV0bDhnIiwibm9uY2UiOiJzWEtoWklxVWxTYUdVMjJfSjJLOF9Hckw4VDQzQWpySCJ9.pfgRCrPuBxMMO6obMkBL_VsCANPFU4LSKYzqXXw_TPKNAYu-qSIQUVnHPtoPMS7NpndgLxWeSvZdQgUMW1oTaDtDLYsbftc8KV-IpFkc269yHcpyMO3TzEczAHLcKT6Y3FpXN5tnnrHxF0wjBSIPh4JNVMkG8AJG4nq3m-uPUuEOQRNvaQGFnFTSJet-H5A5_9aFXVmrH1FmK2LYGGLCJucwOfrC6cV-RqbJRaJ2ZCcqOm3vK08J2bUOBop8Jh7LUoCxoBB480RBmMfqctsVQW9etT2nWft7jCei6ZEo-eE2fGiRbPusq2dVvDMB78ar3KIhplEccGNrbom1fmcMww

这是我的代码

import history from '../history';
import auth0 from 'auth0-js';


export default class Auth {
  auth0 = new auth0.WebAuth({
    domain: 'app1163.auth0.com',
    clientID: '0ZxhmDKrojya1j85kPsQEdUgXUvmKdYr',
    redirectUri: 'http://localhost:3000/login',

    responseType: 'token id_token',
    scope: 'openid'
  });

  constructor() {
    this.login = this.login.bind(this);
    this.logout = this.logout.bind(this);
    this.handleAuthentication = this.handleAuthentication.bind(this);
    this.isAuthenticated = this.isAuthenticated.bind(this);
  }

  login() {
    this.auth0.authorize();
  }

  handleAuthentication() {
    this.auth0.parseHash((err, authResult) => {
      if (authResult && authResult.accessToken && authResult.idToken) {
        this.setSession(authResult);
        history.replace('/home');
      } else if (err) {
        history.replace('/home');
        console.log(err);
        alert(`Error: ${err.error}. Check the console for further details.`);
      }
    });
  }

  setSession(authResult) {
    // Set the time that the access token will expire at
    let expiresAt = JSON.stringify((authResult.expiresIn * 1000) + new Date().getTime());
    localStorage.setItem('access_token', authResult.accessToken);
    localStorage.setItem('id_token', authResult.idToken);
    localStorage.setItem('expires_at', expiresAt);
    // navigate to the home route
    history.replace('/home');
  }

  logout() {
    // Clear access token and ID token from local storage
    localStorage.removeItem('access_token');
    localStorage.removeItem('id_token');
    localStorage.removeItem('expires_at');
    // navigate to the home route
    history.replace('/home');
  }

  isAuthenticated() {
    // Check whether the current time is past the
    // access token's expiry time
    let expiresAt = JSON.parse(localStorage.getItem('expires_at'));
    return new Date().getTime() < expiresAt;
  }
}

那么,有人可以告诉我,我的代码中有什么错误以及为什么会重定向回登录页面?

我也想知道我是否能登录?如果我检查网址,看起来,我已成功登录。

1 个答案:

答案 0 :(得分:1)

你的redirectUri = localhost:3000 / home不应该吗?