link token
This commit is contained in:
parent
0fa00febad
commit
38c45957b8
|
@ -25,7 +25,7 @@ function App() {
|
||||||
<Router>
|
<Router>
|
||||||
<Switch>
|
<Switch>
|
||||||
{/* <Route path={"/login"} exact render={() => <Login />} /> */}
|
{/* <Route path={"/login"} exact render={() => <Login />} /> */}
|
||||||
<Route path={"/login/:user?/:password?"} exact render={(props) => <Login {...props} />} />
|
<Route path={"/login"} exact render={() => <Login />} />
|
||||||
<ReactRouter />
|
<ReactRouter />
|
||||||
</Switch>
|
</Switch>
|
||||||
</Router>
|
</Router>
|
||||||
|
|
38
app/src/components/AutoLogin/AutoLogin.js
Normal file
38
app/src/components/AutoLogin/AutoLogin.js
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
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';
|
||||||
|
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));
|
||||||
|
window.location.href = "/";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
render() {
|
||||||
|
console.log('oke',this.props.match)
|
||||||
|
return(
|
||||||
|
<div></div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default AutoLogin;
|
|
@ -21,10 +21,6 @@ class Login extends Component {
|
||||||
localStorage.clear();
|
localStorage.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
componentDidMount(){
|
|
||||||
this.props.match.params.user !== undefined && this.logInLink(this.props.match.params.user, this.props.match.params.password)
|
|
||||||
}
|
|
||||||
|
|
||||||
CheckLogin = async (event) => {
|
CheckLogin = async (event) => {
|
||||||
var form = $("#formLoginCheck")
|
var form = $("#formLoginCheck")
|
||||||
if (form[0].checkValidity() === false) {
|
if (form[0].checkValidity() === false) {
|
||||||
|
@ -106,68 +102,6 @@ class Login extends Component {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
logInLink = async (user, pass) => {
|
|
||||||
fetch(`${HOST}/api/users/login`, {
|
|
||||||
method: 'POST',
|
|
||||||
headers: {
|
|
||||||
'Accept': 'application/json',
|
|
||||||
'Content-Type': 'application/json'
|
|
||||||
},
|
|
||||||
body: JSON.stringify({
|
|
||||||
username: user,
|
|
||||||
password: pass
|
|
||||||
})
|
|
||||||
}).then((response) => {
|
|
||||||
if (response.status === 500) {
|
|
||||||
this.setState({
|
|
||||||
error: 0,
|
|
||||||
loadingbtn: false
|
|
||||||
})
|
|
||||||
swal("Cảnh báo!", "Hiện tại hệ thống của chúng tôi đang nâng cấp, vui lòng đăng nhập lại sau 30 phút", "info");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
return response.json()
|
|
||||||
}).then((data) => {
|
|
||||||
if (data.status === 10000) {
|
|
||||||
console.log(data)
|
|
||||||
console.log(data.data.roles)
|
|
||||||
this.setState({
|
|
||||||
error: 0,
|
|
||||||
loadingbtn: false
|
|
||||||
}, () => {
|
|
||||||
})
|
|
||||||
localStorage.setItem("access_token", "Bearer " + data.access_token);
|
|
||||||
Store.dispatch(login("Bearer " + data.access_token));
|
|
||||||
localStorage.setItem("username", data.data.username);
|
|
||||||
localStorage.setItem("obj_id", data.data._id);
|
|
||||||
window.location.href = "/";
|
|
||||||
// return;
|
|
||||||
} else if (data.status === 10003) {
|
|
||||||
this.setState({
|
|
||||||
loadingbtn: false
|
|
||||||
})
|
|
||||||
swal("Cảnh báo!", "Tài khoản không tồn tại", "warning");
|
|
||||||
} else if (data.status === 10005) {
|
|
||||||
this.setState({
|
|
||||||
loadingbtn: false
|
|
||||||
})
|
|
||||||
swal("Cảnh báo", "Tài khoản của bạn đã bị khóa, vui lòng chờ 30 phút rồi đăng nhập lại hoặc liên hệ admin", "warning");
|
|
||||||
} else {
|
|
||||||
this.setState({
|
|
||||||
error: 1,
|
|
||||||
loadingbtn: false
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}).catch((error) => {
|
|
||||||
if (error) {
|
|
||||||
this.setState({
|
|
||||||
loadingbtn: false
|
|
||||||
})
|
|
||||||
swal("Lỗi!", "Vui lòng kiểm tra lại đường truyền", "error");
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
onEnterPress = (e) => {
|
onEnterPress = (e) => {
|
||||||
if (e.keyCode === 13 && e.shiftKey === false) {
|
if (e.keyCode === 13 && e.shiftKey === false) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
|
|
@ -402,7 +402,7 @@ class SearchImage extends Component {
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
console.log('oke em', this.state.isLogin);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="m-portlet m-portlet--mobile pb-3">
|
<div className="m-portlet m-portlet--mobile pb-3">
|
||||||
|
|
|
@ -16,6 +16,7 @@ import User from '../components/User/User';
|
||||||
import Test from '../components/Test/Test';
|
import Test from '../components/Test/Test';
|
||||||
import MenuBar from '../components/layouts/MenuBar'
|
import MenuBar from '../components/layouts/MenuBar'
|
||||||
import ListItem from '../components/List/ListItem';
|
import ListItem from '../components/List/ListItem';
|
||||||
|
import AutoLogin from 'components/AutoLogin/AutoLogin';
|
||||||
|
|
||||||
class ReactRouter extends Component {
|
class ReactRouter extends Component {
|
||||||
constructor(props) {
|
constructor(props) {
|
||||||
|
@ -46,6 +47,9 @@ class ReactRouter extends Component {
|
||||||
:
|
:
|
||||||
''
|
''
|
||||||
}
|
}
|
||||||
|
<>
|
||||||
|
<Route path={'/auth/:token?'} render={(props) => <AutoLogin {...props} />} />
|
||||||
|
</>
|
||||||
{
|
{
|
||||||
this.state.dataRole?.indexOf('search:view') !== -1
|
this.state.dataRole?.indexOf('search:view') !== -1
|
||||||
?
|
?
|
||||||
|
|
Loading…
Reference in New Issue
Block a user