Files
Face_Classify_frontend/app/src/components/AutoLogin/AutoLogin.js
Nguyễn Hải Dũng 3c714fc8c5 fix avatar show
2022-12-22 18:31:09 +07:00

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;