Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/#465 logout #467

Merged
merged 3 commits into from
Sep 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 deletions frontend/src/components/MyInfo/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,42 @@ import Space from '../common/Space';
import { useState } from 'react';
import { ProfileProps } from '../../types/Profile';
import UpdateMyInfo from './UpdateMyInfo';
import Button from '../common/Button';
import useToast from '../../hooks/useToast';
import { postApi } from '../../apis/postApi';

const user = JSON.parse(localStorage.getItem('user') || '{}');
const accessToken = localStorage.getItem('userToken');

const MyInfo = () => {
const { showToast } = useToast();

const [isThereImg, setIsThereImg] = useState<boolean>(true);
const [isModifyMyInfo, setIsModifyMyInfo] = useState<boolean>(false);
const [myInfoNameAndEmail, setMyInfoNameAndEmail] = useState<ProfileProps>({
name: user.nickName,
email: user.email,
});

const onClickLogout = async (e: React.MouseEvent<HTMLButtonElement>) => {
try {
await postApi(
`/logout`,
{
accessToken: accessToken,
},
'x-www-form-urlencoded',
);

localStorage.removeItem('user');
localStorage.removeItem('userToken');
window.location.href = '/';
showToast('info', '로그아웃 되었습니다.');
} catch {
showToast('error', '로그아웃에 실패했습니다');
}
};

if (isModifyMyInfo) {
return (
<UpdateMyInfo
Expand All @@ -39,9 +64,14 @@ const MyInfo = () => {
<MyInfoImg src={user.imageUrl} />
<Space size={5} />
<Box>
<Text color="black" $fontSize="medium" $fontWeight="bold">
{user.nickName}
</Text>
<Flex $justifyContent="space-between" $alignItems="center">
<Text color="black" $fontSize="medium" $fontWeight="bold">
{user.nickName}
</Text>
<Button variant="primary" onClick={onClickLogout}>
로그아웃
</Button>
</Flex>
<Text color="black" $fontSize="small" $fontWeight="normal">
{user.email}
</Text>
Expand Down