Reactjs 如何在auth0中访问用户配置文件
我正在我的项目中使用auth0。它是我最喜欢的项目,所以我想为什么不使用auth0呢。我正在尝试访问用户的配置文件。这是我的auth.js代码Reactjs 如何在auth0中访问用户配置文件,reactjs,auth0,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: '0ZxhmD
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;
}
}
从“../history”导入历史记录;
从“auth0 js”导入auth0;
导出默认类身份验证{
auth0=new auth0.WebAuth({
域:“app1163.auth0.com”,
clientID:'0ZxhmDKrojya1j85kPsQEdUgXUvmKdYr',
重定向URI:'http://localhost:3000/apphome',
responseType:“令牌id\u令牌”,
作用域:“openid”
});
构造函数(){
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);
}
登录(){
this.auth0.authorize();
}
getAccessToken(){
const accessToken=localStorage.getItem('access_token');
log(“访问令牌来自”,localStorage.getItem(“访问令牌”))
log(“acces令牌为”,ACCESTOKEN为)
如果(!accessToken){
抛出新错误(“未找到访问令牌”);
}
否则{
返回accessToken;
}
}
getProfile(cb){
//让accessToken=localStorage.getItem(“accessToken”);;
log(“在getprofile中”)
让accessToken=this.getAccessToken();
this.auth0.client.userInfo(accessToken,(err,profile)=>{
如果(配置文件){
日志(“概要文件是”,概要文件)
this.userProfile=profile;
}
//cb(err,profile);
log(“err is”,err)
});
}
手工验证(){
this.auth0.parseHash((err,authResult)=>{
if(authResult&&authResult.accessToken&&authResult.idToken){
此.setSession(authResult);
替换('/apphome');
}否则如果(错误){
替换('/apphome');
控制台日志(err);
警报(`Error:${err.Error}。有关详细信息,请查看控制台。`);
}
});
}
设置会话(authResult){
console.log(“设置会话中的enetred”)
//设置访问令牌将在过期的时间
让expiresAt=JSON.stringify((authResult.expiresIn*1000)+new Date().getTime());
console.log(“在会话中输入”)
setItem('access_token',authResult.accessToken);
setItem('id_token',authResult.idToken);
setItem('expires_at',expiresAt);
log(“access_token”,authResult.accessToken,“id_token”,authResult.idToken,“expires_at”,expiresAt)
log(“访问令牌来自”,localStorage.getItem(“访问令牌”))
//导航到主路线
替换('/apphome');
这个文件名为
}
注销(){
//从本地存储中清除访问令牌和ID令牌
localStorage.removietem('access_token');
localStorage.removietem('id_token');
localStorage.removietem('expires_at');
//导航到主路线
历史记录。替换('/home');
}
未经验证(){
//检查当前时间是否超过当前时间
//访问令牌的到期时间
让expiresAt=JSON.parse(localStorage.getItem('expires_at'));
log(“expires at”,expiresAt,new Date().getTime()
现在,当我打开控制台日志配置文件时。我在控制台中看到这个
sub:“auth0 | 5a340b94197c4f68f0083e3a”
我在等用户的电子邮件
我犯了什么错误?我正在本地存储中获取访问令牌和id令牌
auth0 = new auth0.WebAuth({
domain: 'app1163.auth0.com',
clientID: '0ZxhmDKrojya1j85kPsQEdUgXUvmKdYr',
redirectUri: 'http://localhost:3000/apphome',
responseType: 'token id_token',
scope: 'openid' });
您应该将“scope”变量更改为:
scope: 'openid profile'
更多信息,请访问: