Created
March 16, 2020 20:29
-
-
Save oivoodoo/5740eab83999e882985e37ea3fe83202 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import { | |
BrowserRouter, | |
Route, | |
Switch, | |
Redirect | |
} from 'react-router-dom'; | |
import Dashboard from './pages/dashboard' | |
import "@fortawesome/fontawesome-free/css/all.min.css"; | |
import Auth from './lib/authentication'; | |
interface AppState { | |
authenticated?: boolean | null; | |
email?: string; | |
} | |
interface AuthResponse { | |
email: string; | |
} | |
class App extends React.Component<null, AppState> { | |
constructor(props: any) { | |
super(props); | |
this.state = { | |
authenticated: null | |
}; | |
} | |
public componentDidMount() { | |
Auth.authenticateWithCookie() | |
.then((response) => { | |
response.json().then((resp: AuthResponse) => { | |
this.setState({ | |
authenticated: true, | |
email: resp.email | |
}); | |
}); | |
}) | |
.catch(() => { | |
this.setState({ authenticated: false }); | |
}); | |
} | |
public render() { | |
if (this.state.authenticated === null) { | |
return null; | |
} else if (this.state.authenticated === false) { | |
return <Redirect to="/login" />; | |
} else { | |
return ( | |
<BrowserRouter> | |
<Switch> | |
<Route path="/dashboard" component={Dashboard} exact /> | |
<Redirect from="/" to="/dashboard" /> | |
</Switch> | |
</BrowserRouter> | |
); | |
} | |
} | |
} | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment