import React from "react"; import { Switch, Route, Redirect } from "react-router-dom"; import Home from "./views/Home"; import Dashboard from "./views/DashboardTest"; import Signup from "./views/Signup"; import Login from "./views/Login"; import ForgotPass from "./views/Forgot-password"; import ChangePass from "./views/Forgot-pass-reset"; import queryString from 'query-string'; import { call } from './client_server'; class App extends React.Component { constructor(props) { super(props); let loggedIn = false; try { let userJsonString = localStorage.getItem("token"); if (userJsonString) { loggedIn = true; } } catch (exception) { } this.state = { loggedIn: loggedIn, }; this.auth = this.auth.bind(this); this.logout = this.logout.bind(this); } componentDidMount() { const values = queryString.parse(this.props.location.search); this.setState({ query : values }); } auth(data) { return call.login(data.params) .then(res => { console.log(res); if (res.data && res.status === 200) { this.setState( { user: data.params.user, token: res.data.jwttoken, loggedIn: true, } ); localStorage.setItem("token", JSON.stringify(res.data.jwttoken)); this.props.history.push("/dashboard"); } else { this.setState({ error: res.data.message }); return this.state.error; } }).catch(err => { console.log(err); return {status : "Errore"}; }); }; logout() { this.setState({ loggedIn : false, }); localStorage.removeItem("token"); this.props.history.push("/"); }; render() { return ( { this.state.loggedIn ? : } {this.state.loggedIn ? : } ); } } export default App;