如何在auth0中访问用户配置文件

时间:2017-12-18 17:32:59

标签: reactjs auth0

我在我的项目中使用auth0。它的宠物项目,所以我想为什么不使用auth0。我正在尝试访问用户的个人资料。这是我的auth.js代码。

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/apphome',

    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);
    this.getProfile = this.getProfile.bind(this);
  }

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

  getAccessToken() {
      const accessToken = localStorage.getItem('access_token');
      console.log("access token from ",localStorage.getItem('access_token'))
      console.log("acces token is ",accessToken)
      if (!accessToken) {
        throw new Error('No access token found');
      }

      else{
          return accessToken;
      }

    }
  getProfile(cb) {
  //let accessToken = localStorage.getItem("accessToken");;
  console.log("inside getprofile")
  let accessToken = this.getAccessToken();
  this.auth0.client.userInfo(accessToken, (err, profile) => {
    if (profile) {
      console.log("profile is ",profile)
      this.userProfile = profile;
    }
    //cb(err, profile);
    console.log("err is ",err)
  });
}

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

  setSession(authResult) {
    console.log("enetred in setsession ")

    // Set the time that the access token will expire at
    let expiresAt = JSON.stringify((authResult.expiresIn * 1000) + new Date().getTime());
    console.log("entered in session")
    localStorage.setItem('access_token', authResult.accessToken);
    localStorage.setItem('id_token', authResult.idToken);

    localStorage.setItem('expires_at', expiresAt);
    console.log("access_token",authResult.accessToken,"id_token",authResult.idToken,"expires_at",expiresAt)
    console.log("access token from ",localStorage.getItem('access_token'))
    // navigate to the home route
    history.replace('/apphome');
    this.getProfile()
  }

  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'));
    console.log("expires at ",expiresAt,new Date().getTime() < expiresAt)
    return new Date().getTime() < expiresAt;
  }
}

现在,当我控制日志配置文件时。我在控制台中看到了这个

子:&#34; auth0 | 5a340b94197c4f68f0083e3a&#34;

我期待用户的电子邮件。

我在做什么错?我在本地存储中获取访问令牌expires_at和id令牌。

1 个答案:

答案 0 :(得分:1)

scope: 'openid profile'

您应该将'scope'变量更改为:

EitherT

更多信息请访问:Auth0 Scopes