From 0519dbcbdbe75730c669c1508def9ee088ff67bc Mon Sep 17 00:00:00 2001 From: Alexis SOUQUIERE Date: Mon, 31 Jul 2023 22:31:06 +0200 Subject: [PATCH] fix(ui): redirect on login page when token expires (#1539) --- client/src/App.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/client/src/App.js b/client/src/App.js index 0214dc4d6..98320d4df 100644 --- a/client/src/App.js +++ b/client/src/App.js @@ -1,15 +1,32 @@ import React from 'react'; import { BrowserRouter as Router } from 'react-router-dom'; -import { basePath } from './utils/endpoints'; +import { basePath, uriLogin } from './utils/endpoints'; import Routes from './utils/Routes'; import { MuiPickersUtilsProvider } from '@material-ui/pickers'; import MomentUtils from '@date-io/moment'; import { ToastContainer } from 'react-toastify'; import { loadProgressBar } from 'axios-progress-bar'; +import axios from 'axios'; class App extends React.Component { componentDidMount() { loadProgressBar(); + + axios.interceptors.response.use(null, error => { + try { + // Used for token expiration by redirecting on login page + if (error.response.status === 401) { + window.location = uriLogin(); + } + + return error; + } catch (e) { + // Propagate cancel + if (axios.isCancel(error)) { + throw new axios.Cancel('Operation canceled by the user.'); + } + } + }); } render() {