Skip to content

Commit

Permalink
Merge pull request #25 from dongbin420/feat/log-out
Browse files Browse the repository at this point in the history
feat/쿠키다시 설정하고 로그아웃 구현
  • Loading branch information
k22y22 authored Jul 23, 2023
2 parents 827a541 + 36c08c3 commit d4406d4
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
7 changes: 5 additions & 2 deletions client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions client/src/components/LogoutButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { removeCookie } from '../utils/cookie';
import { useNavigate } from 'react-router-dom';

const LogoutButton = () => {
const navigate = useNavigate();

const handleLogout = () => {
removeCookie('jwtToken');
window.alert('로그아웃 되었습니다.');
navigate('/');
};

return (
<button className="mt-2 w-96 rounded bg-mainblack px-1 py-2 text-white" onClick={handleLogout}>
로그아웃
</button>
);
};

export default LogoutButton;
8 changes: 7 additions & 1 deletion client/src/pages/LoginPage/LoginPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useState } from 'react';
import { useNavigate, Link } from 'react-router-dom';
import api from '../../utils/api';
import SocialLogin from '../../components/SocialLogin';
import { setCookie } from '../../utils/cookie';

const LoginPage: React.FC = () => {
const [userEmail, setUserEmail] = useState('');
Expand Down Expand Up @@ -31,7 +32,12 @@ const LoginPage: React.FC = () => {
email: userEmail,
password: userPassword,
});
console.log('서버 응답:', response.headers);
const authHeader = response.headers.authorization;
if (authHeader && authHeader.startsWith('Bearer ')) {
const token = authHeader.substring(7);
console.log('추출된 JWT 토큰:', token);
}
setCookie('jwtToken', 'token');
if (response.status === 200) {
window.alert('환영합니다!');
navigate('/');
Expand Down
11 changes: 11 additions & 0 deletions client/src/utils/Login/saveTokenFromResponse.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { setCookie } from '../cookie';

export const saveTokenFromResponse = (response: Response) => {
const authHeader = response.headers.get('Authorization');

if (authHeader && authHeader.startsWith('Bearer ')) {
const token = authHeader.substring(7);
console.log('추출된 JWT 토큰:', token);
setCookie('jwtToken', token);
}
};

0 comments on commit d4406d4

Please sign in to comment.