Skip to content

Commit 383fa67

Browse files
committed
Navigate
1 parent 7dd31ea commit 383fa67

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

frontend/src/components/ProfileSettings.jsx

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,33 @@
1-
import React, { useState } from 'react';
1+
import React, { useState, useContext, useEffect } from 'react';
2+
import { useNavigate } from 'react-router-dom';
23
import { Button, Dialog, DialogTitle, DialogContent, DialogContentText, DialogActions, LinearProgress } from '@mui/material';
34
import api from '../utils/api';
5+
import UserContext from './contexts/UserContext';
6+
import { handleLogout } from '../utils/auth'; // Import handleLogout
47

58
const ProfileSettings = () => {
69
const [open, setOpen] = useState(false);
7-
const [deleteConfirmOpen, setDeleteConfirmOpen] = useState(false);
810
const [deleteInProgress, setDeleteInProgress] = useState(false);
911
const [deleteSuccess, setDeleteSuccess] = useState(false);
12+
const user = useContext(UserContext);
13+
const navigate = useNavigate();
14+
15+
useEffect(() => {
16+
if (!user) {
17+
navigate('/login');
18+
}
19+
}, [user, navigate]);
1020

1121
const handleClickOpen = () => {
1222
setOpen(true);
1323
};
1424

15-
const handleClose = () => {
25+
const handleClose = async () => {
1626
setOpen(false);
17-
};
18-
19-
const handleDeleteConfirm = () => {
20-
setDeleteConfirmOpen(true);
21-
};
22-
23-
const handleDeleteConfirmClose = () => {
24-
setDeleteConfirmOpen(false);
25-
setDeleteSuccess(false);
27+
if (deleteSuccess) {
28+
await handleLogout(); // Log out the user
29+
navigate('/login'); // Redirect to login page
30+
}
2631
};
2732

2833
const handleDeleteProfile = async () => {
@@ -37,6 +42,10 @@ const ProfileSettings = () => {
3742
setDeleteInProgress(false);
3843
};
3944

45+
if (!user) {
46+
return null; // Optionally, you can return a loading spinner or a message here
47+
}
48+
4049
return (
4150
<div className="container">
4251
<h2>Profile Settings</h2>

frontend/src/utils/auth.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import api from './api';
22
import FetchUser from './FetchUser';
33
import { toast } from 'react-toastify';
4+
import { removeTokens } from './AccessToken'; // Import removeTokens
45

56
export const handleLogin = async (username, password, recaptchaToken, onLogin, navigate, setIsLoading) => {
67
setIsLoading(true);
@@ -35,8 +36,10 @@ export const handleLogin = async (username, password, recaptchaToken, onLogin, n
3536
export const handleLogout = async () => {
3637
try {
3738
await api.post(`/auth/logout/`, {}, { withCredentials: true });
39+
removeTokens(); // Clear tokens on logout
3840
// Handle successful logout, e.g. redirect to login page
3941
} catch (err) {
42+
console.error('Error during logout:', err);
4043
// Handle errors
4144
}
4245
};

0 commit comments

Comments
 (0)