Skip to content

Instantly share code, notes, and snippets.

@oivoodoo
Created March 16, 2020 20:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oivoodoo/5740eab83999e882985e37ea3fe83202 to your computer and use it in GitHub Desktop.
Save oivoodoo/5740eab83999e882985e37ea3fe83202 to your computer and use it in GitHub Desktop.
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