React.js的新手
我正在从不导出react组件的类中的react中运行一个简单的函数。
该函数调用localStorage.setItem()来保存成功的身份验证过程的结果。
localStorage.setItem('access_token', authResult.accessToken);
localStorage.setItem('id_token', authResult.idToken);
localStorage.setItem('expires_at', expiresAt);
代码将被执行,但不会将这些值保存在localStorage或其他任何地方。
还有其他人经历过吗?
Callback.js
import React, { Component } from 'react';
import LoaderAnimation from './LoaderAnimation';
import Auth from '../services/auth';
class Callback extends React.Component {
constructor(props) {
super(props)
this.state = {
auth: new Auth()
}
}
componentDidMount() {
this.state.auth.handleAuthentication();
this.props.history.push('/home')
}
render() {
return(
<LoaderAnimation />
);
}
}
export default Callback;
auth.js
handleAuthentication() {
this.auth0.parseHash((err, authResult) => {
if (authResult && authResult.accessToken && authResult.idToken) {
this.setSession(authResult);
} else if (err) {
history.replace('/');
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);
}