58 lines
1.9 KiB
JavaScript
58 lines
1.9 KiB
JavaScript
import { login } from '../../actions/isLogin';
|
|
import React, { Component } from 'react';
|
|
import "react-datepicker/dist/react-datepicker.css";
|
|
import { HOST } from '../../config/index';
|
|
import Store from '../../store';
|
|
import { role } from '../../actions/role';
|
|
class AutoLogin extends Component {
|
|
constructor(props) {
|
|
super(props);
|
|
|
|
this.state = {
|
|
dataRole: Store.getState().role.role,
|
|
isLogin: Store.getState().isLogin.isLogin,
|
|
};
|
|
Store.subscribe(() => {
|
|
this.setState({
|
|
isLogin: Store.getState().isLogin.isLogin,
|
|
dataRole: Store.getState().role.role,
|
|
}, () => {
|
|
|
|
});
|
|
});
|
|
}
|
|
componentDidMount() {
|
|
if(this.props.match.params.token){
|
|
localStorage.setItem("access_token", "Bearer " + this.props.match.params.token);
|
|
Store.dispatch(login("Bearer " + this.props.match.params.token));
|
|
let data = fetch(`${HOST}/api/get_info`, {
|
|
method: 'GET',
|
|
headers: {
|
|
'Accept': 'application/json',
|
|
'Content-Type': 'application/json',
|
|
'Authorization': "Bearer " + this.props.match.params.token,
|
|
// 'Authorization': token,
|
|
},
|
|
}).then((response) => {
|
|
return response.json()
|
|
}).then((data) => {
|
|
if(data.status === 10000){
|
|
localStorage.setItem("roles", JSON.stringify(data.roles));
|
|
Store.dispatch(role(data.roles));
|
|
window.location.href = "/";
|
|
}else{
|
|
alert('Sai token!!!');
|
|
window.location.href = "/login";
|
|
}
|
|
})
|
|
}
|
|
}
|
|
render() {
|
|
console.log('oke',Store.getState().role.role)
|
|
return(
|
|
<div></div>
|
|
)
|
|
}
|
|
}
|
|
|
|
export default AutoLogin; |