From 0b7cddb28d70813300d6c130343635c7482ac5e2 Mon Sep 17 00:00:00 2001 From: Jon Date: Fri, 11 Oct 2024 23:29:43 +0930 Subject: [PATCH] feat(component): Add 401 login redirect ref: #1 nofusscomputing/centurion_erp#248 nofusscomputing/centurion_erp#307 nofusscomputing/centurion_erp#319 --- src/App.js | 10 ++++++++++ src/components/page/Navbar.jsx | 15 +++++++++++++-- src/hooks/apiFetch.js | 9 ++++++--- 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/src/App.js b/src/App.js index 56afd40..39e23f7 100644 --- a/src/App.js +++ b/src/App.js @@ -19,6 +19,15 @@ import ModelForm from "./layout/ModelForm"; import History from "./layout/history"; import Settings from "./layout/Settings"; +const Login = () => { + + window.location.replace('http://127.0.0.1:8002/api/v2/auth/login'); + + return ( +
redirect
+ ); +} + function App() { @@ -39,6 +48,7 @@ function App() { />} errorElement={ } > + }/> { - apiFetch( + let response = apiFetch( '', (data) => { @@ -43,6 +45,15 @@ const Navbar = ({ 'OPTIONS' ) + .then(response => { + + console.log(`the data returned`) + + if( response.status === 401 ) { + navigate('/login') + } + }) + },[]) diff --git a/src/hooks/apiFetch.js b/src/hooks/apiFetch.js index 0d0ce1a..f7726ba 100644 --- a/src/hooks/apiFetch.js +++ b/src/hooks/apiFetch.js @@ -59,14 +59,17 @@ export async function apiFetch( }); + if( response.status != 204 ) { + api_data = await response.json() - } + - if( callback ) { + if( callback ) { - callback(api_data) + callback(api_data) + } } return response;