all repos — caroster @ f4954fa9c323bcb73ba8dcaf38ef7f5ba4016d09

[Octree] Group carpool to your event https://caroster.io

Resolve "Les tokens expirés ne sont pas vérifiés quand on revient sur l'app après un mois"
Hadrien Froger e3k8y9i0k3s8o9x4@octreea17.slack.com
Thu, 10 Sep 2020 08:04:51 +0000
commit

f4954fa9c323bcb73ba8dcaf38ef7f5ba4016d09

parent

1ba99edd2c4acce72f49716048badd62e7948d23

M app/src/Router.jsapp/src/Router.js

@@ -12,23 +12,26 @@ import SignUp from './pages/SignUp';

import SignIn from './pages/SignIn'; import LostPassword from './pages/LostPassword.js'; import ResetPassword from './pages/ResetPassword.js'; - +import ErrorBoundary from './containers/ErrorBoundary'; const Router = () => { useGTM(); return ( <BrowserRouter> - <Switch> - <Route path="/e/:eventId" component={Event} /> - <Route path="/" exact component={Home} /> - <Route path="/new" exact component={Home} /> - <Route path="/register" exact component={SignUp} /> - <Route path="/lost-password" exact component={LostPassword} /> - <Route path="/reset-password" exact component={ResetPassword} /> - <Route path="/login" exact component={SignIn} /> - <Route path="/dashboard" exact component={Dashboard} /> - <Route path="/profile" exact component={Profile} /> - <Route component={NotFound} /> - </Switch> + <ErrorBoundary> + <Switch> + <Route path="/e/:eventId" component={Event} /> + <Route path="/" exact component={Home} /> + <Route path="/new" exact component={Home} /> + <Route path="/register" exact component={SignUp} /> + <Route path="/lost-password" exact component={LostPassword} /> + <Route path="/reset-password" exact component={ResetPassword} /> + <Route path="/login" exact component={SignIn} /> + <Route path="/dashboard" exact component={Dashboard} /> + <Route path="/profile" exact component={Profile} /> + <Route path="/error" exact component={NotFound} /> + <Route component={NotFound} /> + </Switch> + </ErrorBoundary> </BrowserRouter> ); };
A app/src/containers/ErrorBoundary/LogoutAndRedirect.js

@@ -0,0 +1,14 @@

+import React, {useEffect} from 'react'; +import {Redirect} from 'react-router-dom'; +import {useAuth} from 'strapi-react-context'; + +const LogoutAndRedirect = () => { + const {logout, token} = useAuth(); + useEffect(() => { + logout(); + }, []); + if (token) return null; + return <Redirect to="/" />; +}; + +export default LogoutAndRedirect;
A app/src/containers/ErrorBoundary/index.js

@@ -0,0 +1,43 @@

+import React, {Component} from 'react'; +import LogoutAndRedirect from './LogoutAndRedirect'; +import {ApiProblem} from 'strapi-react-context'; +import {Redirect} from 'react-router-dom'; + +/** + * Error boundary to catch all the error from our code. + * If the error catched is an `unauthorized` APIProblem, will logout and redirect. Otherwise + * will redirect to an /error page. (nicer than blank page) + */ +class ErrorBoundary extends Component { + state = {error: null}; + + /** + * @param {Error} error + * @return {Object} new state after derivation + */ + static getDerivedStateFromError(error) { + if (error instanceof ApiProblem) { + return {error: error.kind}; + } + return {error: 'unknown'}; + } + + /** + * Component did catch an error, log it. + * @param {Error} error + */ + componentDidCatch(error) { + console.error('App did catch an error', {error}); + } + + /** + * @override + */ + render() { + if (this.state.error === null) return this.props.children; + + if (this.state.error === 'unauthorized') return <LogoutAndRedirect />; + return <Redirect to="error" />; + } +} +export default ErrorBoundary;