From e67aa337e5fedcf1e7bc169ff1c6f0f730eebfaa Mon Sep 17 00:00:00 2001 From: AlexisSouquiere Date: Thu, 20 Jul 2023 16:29:47 +0200 Subject: [PATCH 1/2] Redirect on login page when token expires --- client/src/components/Root/Root.jsx | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/client/src/components/Root/Root.jsx b/client/src/components/Root/Root.jsx index 907e4b350..abd26076c 100644 --- a/client/src/components/Root/Root.jsx +++ b/client/src/components/Root/Root.jsx @@ -1,6 +1,7 @@ import { Component } from 'react'; import axios from 'axios'; import { get, put, post, remove } from '../../utils/api'; +import { uriLogin } from '../../utils/endpoints'; class Root extends Component { cancel = axios.CancelToken.source(); @@ -14,6 +15,19 @@ class Root extends Component { } this.cancelAxiosRequests(); + + axios.interceptors.response.use( + response => { + return response; + }, + error => { + // Used for token expiration by redirecting on login page + if (error.response.status === 401) { + window.location = uriLogin(); + } + return error; + } + ); } cancelAxiosRequests() { From 5a7b4365c4cf8eb59a0dae09e09bf48ccb65f688 Mon Sep 17 00:00:00 2001 From: AlexisSouquiere Date: Mon, 31 Jul 2023 15:53:08 +0200 Subject: [PATCH 2/2] Move interceptor at the right place --- client/src/App.js | 19 ++++++++++++++++++- client/src/components/Root/Root.jsx | 14 -------------- 2 files changed, 18 insertions(+), 15 deletions(-) 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() { diff --git a/client/src/components/Root/Root.jsx b/client/src/components/Root/Root.jsx index abd26076c..907e4b350 100644 --- a/client/src/components/Root/Root.jsx +++ b/client/src/components/Root/Root.jsx @@ -1,7 +1,6 @@ import { Component } from 'react'; import axios from 'axios'; import { get, put, post, remove } from '../../utils/api'; -import { uriLogin } from '../../utils/endpoints'; class Root extends Component { cancel = axios.CancelToken.source(); @@ -15,19 +14,6 @@ class Root extends Component { } this.cancelAxiosRequests(); - - axios.interceptors.response.use( - response => { - return response; - }, - error => { - // Used for token expiration by redirecting on login page - if (error.response.status === 401) { - window.location = uriLogin(); - } - return error; - } - ); } cancelAxiosRequests() {