frontend/smart-hut/src/App.js

125 lines
3.3 KiB
JavaScript
Raw Normal View History

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";
import queryString from 'query-string';
2020-03-02 15:59:32 +00:00
import { call } from './client_server';
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");
if (userJsonString) {
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);
}
componentDidMount() {
2020-03-07 10:59:04 +00:00
if (this.props.location) {
const values = queryString.parse(this.props.location.search);
this.setState({
query : values
});
}
}
2020-03-02 15:59:32 +00:00
auth(data) {
return call.login(data.params)
2020-03-02 15:59:32 +00:00
.then(res => {
console.log(res);
if (res.data && res.status === 200) {
2020-03-11 16:38:04 +00:00
localStorage.setItem("token", 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");
this.props.history.push("/");
};
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 13:18:37 +00:00
{/* ToDO change this back */}
{!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-07 09:54:42 +00:00
<Route path="/forgot-pass-reset" >
<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