Skip to content

Commit

Permalink
fix: redirect to sign in page if you get a 401 on refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
bjarneo committed Jan 21, 2024
1 parent 426ae0f commit 4b8fc96
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/client/components/header/index.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Link } from 'react-router-dom';
import { useEffect } from 'react';
import { useEffect, useState } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { Navigate } from 'react-router-dom';
import { Anchor, Burger, Container, Group, Grid, Modal, Button } from '@mantine/core';
import { useDisclosure } from '@mantine/hooks';

Expand All @@ -20,6 +21,7 @@ const Header = () => {

const [opened, { toggle }] = useDisclosure(false);
const [openRefreshModal, { open, close }] = useDisclosure(false);
const [redirect, setRedirect] = useState(false);

useEffect(() => {
if (!isLoggedIn && username) {
Expand Down Expand Up @@ -64,7 +66,8 @@ const Header = () => {
const data = await refresh();

if (data.statusCode === 401) {
return;
setRedirect(true);
close();
}

dispatch(userLogin(cookie));
Expand All @@ -75,6 +78,8 @@ const Header = () => {

return (
<>
{redirect && <Navigate to="/signin" />}

<Modal opened={openRefreshModal} onClose={close} title="Authentication">
<p>Your session is about to expire. Do you want to extend it?</p>
<Button onClick={handleRefreshCookie} color="hemmelig">
Expand Down

0 comments on commit 4b8fc96

Please sign in to comment.