auth.jsAuth0不重定向到指定的URL(反應)
import auth0 from 'auth0-js';
export default class Auth {
constructor() {
this.auth0 = new auth0.WebAuth({
domain: '<properURL>',
clientID: '<properID>',
redirectUri: 'http://localhost:3000/',
audience: '<blahblah>',
responseType: 'token id_token',
scope: '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);
}
login() {
// console.log(this.auth0);
this.auth0.authorize();
}
handleAuthentication() {
this.auth0.parseHash((err, authResult) => {
if (authResult && authResult.accessToken && authResult.idToken) {
this.setSession(authResult);
// history.replace('/lobby');
} else if (err) {
// history.replace('/lobby');
console.log(err);
}
});
}
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('/lobby');
}
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('/lobby');
}
isAuthenticated() {
// Check whether the current time is past the
// access token's expiry time
//return localStorage.length > 0;
// console.log(localStorage)
let expiresAt = JSON.parse(localStorage.getItem('expires_at'));
return new Date().getTime() < expiresAt;
}
}
Lobby.jsx
import React, { Component } from 'react';
import Auth from '../Auth/Auth.js';
import {
HashRouter as Router,
Route,
Link
} from 'react-router-dom'
export default class Lobby extends Component {
constructor(props) {
super(props)
this.auth = new Auth();
this.state = {
meep: 'whooosh'
}
}
render() {
return (
<div>
{!this.auth.isAuthenticated() ?
<button onClick={this.auth.login}>Please Login to Play</button>
:
<Link to='/room'>
<h1>Click here to join game</h1>
</Link>
}
</div>
);
}
}
我一直在關注的Auth0教程與工作的反應,但我不能讓它實際上正常工作。點擊登錄按鈕時,它會經歷整個身份驗證過程,但無法重定向到我指定的redirectUri。它將所有訪問令牌信息附加到URL中,並且幾乎中斷了反應路由器。頁面上沒有任何內容加載。但是,如果我console.log本地存儲,我看到正確的身份驗證已完成。如果我從url中刪除訪問令牌信息,所以它只是服務器的主路由,它會檢測到我已通過身份驗證並允許我繼續。
所以它只是不正確的重定向。任何想法爲什麼?