Skip to content

Commit

Permalink
feat(component): Add 401 login redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
jon-nfc committed Oct 11, 2024
1 parent 420b7e0 commit 0b7cddb
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
10 changes: 10 additions & 0 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<section>redirect</section>
);
}



function App() {
Expand All @@ -39,6 +48,7 @@ function App() {
/>}
errorElement={<ErrorPage /> }
>
<Route path='/login' element={<Login/>}/>

<Route path="/core/:model/:pk/:action"
element={<History
Expand Down
15 changes: 13 additions & 2 deletions src/components/page/Navbar.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Link, useLocation, useParams } from "react-router-dom";
import { Link, redirect, useLocation, useNavigate, useParams } from "react-router-dom";
import IconLoader from "../IconLoader";
import { useEffect, useState } from "react";
import { ResponseException } from "../../classes/Exceptions";
Expand All @@ -20,9 +20,11 @@ const Navbar = ({

const location = useLocation();

const navigate = useNavigate();

useEffect(() => {

apiFetch(
let response = apiFetch(
'',
(data) => {

Expand All @@ -43,6 +45,15 @@ const Navbar = ({
'OPTIONS'
)

.then(response => {

console.log(`the data returned`)

if( response.status === 401 ) {
navigate('/login')
}
})

},[])


Expand Down
9 changes: 6 additions & 3 deletions src/hooks/apiFetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 0b7cddb

Please sign in to comment.