2020-03-11 16:38:04 +00:00
|
|
|
import React, {Component} from "react";
|
2020-03-07 10:59:04 +00:00
|
|
|
import {BrowserRouter, Switch, Route, Redirect } from "react-router-dom";
|
2020-03-02 15:59:32 +00:00
|
|
|
import Home from "./views/Home";
|
2020-03-11 16:38:04 +00:00
|
|
|
import Dashboard from "./views/Dashboard";
|
2020-03-02 15:59:32 +00:00
|
|
|
import Signup from "./views/Signup";
|
|
|
|
import Login from "./views/Login";
|
2020-03-07 10:59:04 +00:00
|
|
|
import FourOhFour from "./views/FourOhFour";
|
2020-03-05 10:02:33 +00:00
|
|
|
import ForgotPass from "./views/Forgot-password";
|
|
|
|
import ChangePass from "./views/Forgot-pass-reset";
|
2020-03-14 14:48:33 +00:00
|
|
|
import ConfirmForgotPasswrod from "./views/ConfirmForgotPassword";
|
|
|
|
import ConfirmRegistration from "./views/ConfirmRegistration";
|
2020-03-17 14:06:40 +00:00
|
|
|
import Instruction from "./views/Instruction";
|
2020-03-05 17:08:17 +00:00
|
|
|
import queryString from 'query-string';
|
2020-02-25 13:18:25 +00:00
|
|
|
|
2020-03-02 15:59:32 +00:00
|
|
|
import { call } from './client_server';
|
2020-02-25 13:18:25 +00:00
|
|
|
|
2020-03-11 16:38:04 +00:00
|
|
|
/*let userJsonString = JSON.parse(localStorage.getItem("token"));
|
|
|
|
let date = new Date().getTime().toString();
|
|
|
|
let delta = date - userJsonString.timestamp;
|
|
|
|
if (delta < 5*60*60*1000) {
|
|
|
|
loggedIn = true;
|
|
|
|
}*/
|
|
|
|
|
|
|
|
class App extends Component {
|
2020-03-02 15:59:32 +00:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
let loggedIn = false;
|
2020-03-17 16:38:03 +00:00
|
|
|
|
2020-03-02 15:59:32 +00:00
|
|
|
try {
|
2020-03-11 16:38:04 +00:00
|
|
|
let userJsonString = localStorage.getItem("token");
|
2020-03-22 16:58:27 +00:00
|
|
|
let exp = localStorage.getItem("exp");
|
|
|
|
let date = new Date().getTime();
|
|
|
|
if (userJsonString && exp && date < exp) {
|
2020-03-11 16:38:04 +00:00
|
|
|
loggedIn = true;
|
|
|
|
}
|
|
|
|
}catch(exception) {
|
|
|
|
|
2020-03-02 15:59:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
loggedIn: loggedIn,
|
|
|
|
};
|
|
|
|
|
|
|
|
this.auth = this.auth.bind(this);
|
|
|
|
this.logout = this.logout.bind(this);
|
|
|
|
}
|
|
|
|
|
2020-03-05 17:08:17 +00:00
|
|
|
componentDidMount() {
|
2020-03-19 10:52:13 +00:00
|
|
|
if (window.location) {
|
|
|
|
const values = queryString.parse(window.location.search);
|
|
|
|
console.log(values);
|
2020-03-07 10:59:04 +00:00
|
|
|
this.setState({
|
|
|
|
query : values
|
|
|
|
});
|
2020-03-19 10:52:13 +00:00
|
|
|
} else {
|
|
|
|
this.setState({
|
|
|
|
query : "ciao"
|
|
|
|
});
|
2020-03-07 10:59:04 +00:00
|
|
|
}
|
2020-03-05 17:08:17 +00:00
|
|
|
}
|
|
|
|
|
2020-03-02 15:59:32 +00:00
|
|
|
auth(data) {
|
2020-03-04 15:12:58 +00:00
|
|
|
return call.login(data.params)
|
2020-03-02 15:59:32 +00:00
|
|
|
.then(res => {
|
2020-03-04 15:12:58 +00:00
|
|
|
if (res.data && res.status === 200) {
|
2020-03-11 16:38:04 +00:00
|
|
|
localStorage.setItem("token", res.data.jwttoken);
|
2020-03-22 16:58:27 +00:00
|
|
|
localStorage.setItem("exp", new Date().getTime() + (60*60*5))
|
2020-03-20 17:42:38 +00:00
|
|
|
call.setToken(res.data.jwttoken);
|
2020-03-02 15:59:32 +00:00
|
|
|
this.setState(
|
|
|
|
{
|
|
|
|
user: data.params.user,
|
|
|
|
token: res.data.jwttoken,
|
|
|
|
loggedIn: true,
|
|
|
|
}
|
|
|
|
);
|
2020-03-14 14:48:33 +00:00
|
|
|
return res;
|
2020-03-11 16:38:04 +00:00
|
|
|
//this.props.history.push("/dashboard");
|
2020-03-02 15:59:32 +00:00
|
|
|
} else {
|
|
|
|
this.setState({
|
|
|
|
error: res.data.message
|
|
|
|
});
|
2020-03-04 17:49:56 +00:00
|
|
|
return this.state.error;
|
2020-03-02 15:59:32 +00:00
|
|
|
}
|
|
|
|
}).catch(err => {
|
|
|
|
console.log(err);
|
2020-03-04 17:49:56 +00:00
|
|
|
return {status : "Errore"};
|
2020-03-02 15:59:32 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
logout() {
|
|
|
|
this.setState({
|
|
|
|
loggedIn : false,
|
|
|
|
});
|
|
|
|
|
|
|
|
localStorage.removeItem("token");
|
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
2020-03-07 10:59:04 +00:00
|
|
|
<BrowserRouter>
|
2020-03-02 15:59:32 +00:00
|
|
|
<Switch>
|
|
|
|
<Route path="/" exact component={Home} />
|
|
|
|
<Route path="/login" >
|
2020-03-17 16:38:03 +00:00
|
|
|
{ this.state.loggedIn && this.state.token ? <Redirect tkn={this.state.token} to="/dashboard" /> : <Login auth={this.auth} /> }
|
2020-03-02 15:59:32 +00:00
|
|
|
</Route>
|
|
|
|
<Route path="/signup" exact component={Signup} />
|
|
|
|
<Route path="/dashboard" >
|
2020-03-18 14:49:33 +00:00
|
|
|
{this.state.loggedIn ? <Dashboard tkn={this.state.token} logout={this.logout} /> : <Redirect to="/login" />}
|
2020-03-02 15:59:32 +00:00
|
|
|
</Route>
|
2020-03-05 10:02:33 +00:00
|
|
|
<Route path="/forgot-password" >
|
|
|
|
<ForgotPass />
|
|
|
|
</Route>
|
2020-03-14 14:48:33 +00:00
|
|
|
<Route path="/sent-email" >
|
|
|
|
<ConfirmForgotPasswrod />
|
|
|
|
</Route>
|
|
|
|
<Route path="/sent-email-reg" >
|
|
|
|
<ConfirmRegistration />
|
|
|
|
</Route>
|
2020-03-17 14:06:40 +00:00
|
|
|
<Route path="/instruction" >
|
|
|
|
<Instruction />
|
|
|
|
</Route>
|
2020-03-21 12:38:34 +00:00
|
|
|
<Route path="/forgot-pass-reset" > </Route>
|
2020-03-19 10:52:13 +00:00
|
|
|
<Route path="/password-reset" >
|
2020-03-07 09:54:42 +00:00
|
|
|
<ChangePass query={this.state.query}/>
|
2020-03-05 10:02:33 +00:00
|
|
|
</Route>
|
2020-03-07 10:59:04 +00:00
|
|
|
<Route component={FourOhFour} />
|
2020-03-02 15:59:32 +00:00
|
|
|
</Switch>
|
2020-03-07 10:59:04 +00:00
|
|
|
</BrowserRouter>
|
2020-03-02 15:59:32 +00:00
|
|
|
);
|
|
|
|
}
|
2020-02-24 14:22:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default App;
|
2020-03-02 15:59:32 +00:00
|
|
|
|