Skip to content

Commit 97a66e6

Browse files
committed
fix(auth): logout
1 parent b111fcb commit 97a66e6

File tree

5 files changed

+54
-24
lines changed

5 files changed

+54
-24
lines changed

react-app/src/App.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ import { AuthProvider } from 'providers/AuthProvider';
55
import Navbar from 'components/NavBar';
66

77
const links = [
8-
{ href: '/my', title: 'Go to My Page' },
9-
{ href: '/login', title: 'Go to login' },
10-
{ href: '/', title: 'Go to index' },
8+
{ href: '/', title: 'Home' },
9+
{ href: '/my', title: 'My Page' },
10+
{ href: '/login', title: 'Login', credential: true },
11+
{ href: '/logout', title: 'Logout', credential: false },
1112
];
1213

1314
const App = () => {

react-app/src/components/NavBar.jsx

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,47 @@
11
import { AppBar, Box, Toolbar, Typography, styled } from '@mui/material';
2+
import React, { useCallback } from 'react';
23

34
import { Link } from 'react-router-dom';
5+
import { useAuth } from 'providers/AuthProvider';
46

57
const NavItem = styled(Link)`
68
color: white;
79
text-decoration-line: none;
810
`;
911

10-
const NavLink = ({ href, title, ...props }) => (
11-
<NavItem to={href} key={`menu-${href}`}>
12-
<Typography>{title}</Typography>
13-
</NavItem>
14-
);
12+
const NavBar = ({ links }) => {
13+
const { isAuthenticated } = useAuth();
1514

16-
const NavBar = ({ links }) => (
17-
<AppBar position="relative">
18-
<Toolbar>
19-
{links &&
20-
links.map((link) => (
21-
<Box sx={{ flexGrow: 1 }} key={`nav-${link.href}`}>
22-
<NavLink href={link.href} title={link.title} />
15+
const isHidden = (credential) => {
16+
if (credential === undefined) return false;
17+
return credential === isAuthenticated;
18+
};
19+
20+
const Item = useCallback(
21+
({ href, title, credential }) => {
22+
console.log('item', href, credential, isAuthenticated);
23+
if (isHidden(credential)) {
24+
return <></>;
25+
} else {
26+
return (
27+
<Box sx={{ flexGrow: 1 }}>
28+
<NavItem to={href} key={`menu-${href}`}>
29+
<Typography>{title}</Typography>
30+
</NavItem>
2331
</Box>
24-
))}
25-
</Toolbar>
26-
</AppBar>
27-
);
32+
);
33+
}
34+
},
35+
[isAuthenticated],
36+
);
37+
38+
return (
39+
<AppBar position="relative">
40+
<Toolbar>
41+
{links && links.map((link) => <Item key={`nav-item-${link.href}`} {...link} />)}
42+
</Toolbar>
43+
</AppBar>
44+
);
45+
};
2846

2947
export default NavBar;

react-app/src/pages/Auth.jsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,10 @@ export const Login = () => {
129129
};
130130

131131
export const Logout = () => {
132+
const { logout } = useAuth();
132133
const location = useNavigate();
133134
useEffect(() => {
135+
logout();
134136
location(-1);
135137
}, [location]);
136138
};

react-app/src/pages/MyPage.jsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
import { Avatar, Container, Grid, Typography, Button, Stack } from '@mui/material';
1+
import { Avatar, Container, Typography, Button, Stack } from '@mui/material';
22
import React, { useEffect, useCallback, useState } from 'react';
33

44
import { Card } from 'components/Card';
5-
import { ProtectedRoute } from 'providers/AuthProvider';
5+
import { ProtectedRoute, useAuth } from 'providers/AuthProvider';
66
import { customAxios } from 'libs/customAxios';
77

88
const MyPage = () => {
99
const [userData, setUserData] = useState(null);
10+
const { logout } = useAuth();
11+
1012
useEffect(() => {
1113
customAxios()
1214
.get('/users/me')
@@ -49,7 +51,9 @@ const MyPage = () => {
4951
</Typography>
5052
</div>
5153
<div style={{ textAlign: 'center' }}>
52-
<Button variant="contained">Logout</Button>
54+
<Button variant="contained" onClick={() => logout({ redirectUrl: '/' })}>
55+
Logout
56+
</Button>
5357
</div>
5458
</Stack>
5559
</Card>

react-app/src/providers/AuthProvider.jsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,17 @@ export const AuthProvider = ({ afterLogin, children }) => {
5555
const { location } = window;
5656
const url = new URL(location.href);
5757
const next = url.searchParams.get('next');
58-
navigate(redirectUrl || afterLogin || next || '/');
58+
navigate(redirectUrl || next || afterLogin || '/');
5959
};
6060

61-
const handleLogout = () => {
61+
const handleLogout = (options) => {
62+
const { redirectUrl } = options || {};
6263
console.log('handleLogout', getToken());
6364
updateToken(null);
65+
setAuthenticated(false);
66+
if (redirectUrl) {
67+
navigate(redirectUrl);
68+
}
6469
};
6570

6671
const value = {

0 commit comments

Comments
 (0)