Skip to content

Commit

Permalink
feat: 네비게이션 bar 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
yechan-kim committed Nov 19, 2024
1 parent 73e85bd commit 0fc2cb1
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 39 deletions.
4 changes: 2 additions & 2 deletions src/components/NavBar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ const NavBar = () => {
{username ? (
<div style={{display: 'flex', alignItems: 'center'}}>
<span className={styles.username}>{username}</span>
&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<LogoutButton onClick={handleLogout}/>
</div>
) : (
<GoogleButton onClick={login} name="Google"/>
<GoogleButton onClick={() => login()} name="Google"/>
)}
</li>
</ul>
Expand Down
80 changes: 44 additions & 36 deletions src/hooks/useAuth.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,48 @@
import { useState } from 'react';
import {useState, useEffect} from 'react';
import axios from 'axios';

export const useAuth = () => {
const [username, setUsername] = useState(localStorage.getItem('username'));

const handleLogin = (newUsername) => {
setUsername(newUsername);
};

const handleLogout = () => {
localStorage.removeItem('accessToken');
localStorage.removeItem('refreshToken');
localStorage.removeItem('username');
localStorage.removeItem('session');
setUsername(null);
};

const handleCallback = async (code) => {
try {
const backendResponse = await axios.get(`${import.meta.env.VITE_DEFAULT_API_URI}/login?code=${code}`);
const { accessToken, refreshToken, username, session } = backendResponse.data;

localStorage.setItem('accessToken', accessToken);
localStorage.setItem('refreshToken', refreshToken);
localStorage.setItem('session', session);

setUsername(username);
} catch (error) {
console.error('Error sending code to backend:', error);
}
};

return {
username,
handleLogin,
handleLogout,
handleCallback,
};
const [username, setUsername] = useState(localStorage.getItem('username'));

useEffect(() => {
const storedUsername = localStorage.getItem('username');
if (storedUsername) {
setUsername(storedUsername);
}
}, []);

const handleLogin = (newUsername) => {
setUsername(newUsername);
};

const handleLogout = () => {
localStorage.removeItem('accessToken');
localStorage.removeItem('refreshToken');
localStorage.removeItem('username');
localStorage.removeItem('session');
setUsername(null);
};

const handleCallback = async (code) => {
try {
const backendResponse = await axios.get(`${import.meta.env.VITE_DEFAULT_API_URI}/login?code=${code}`);
const {accessToken, refreshToken, username, session} = backendResponse.data;

localStorage.setItem('accessToken', accessToken);
localStorage.setItem('refreshToken', refreshToken);
localStorage.setItem('username', username);
localStorage.setItem('session', session);

setUsername(username);
} catch (error) {
console.error('Error sending code to backend:', error);
}
};

return {
username,
handleLogin,
handleLogout,
handleCallback,
};
};
9 changes: 8 additions & 1 deletion src/styles/NavBar.module.css
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
.username {
color: rgba(0, 0, 0, 0.54); /* text-black-54 */
font-weight: 600; /* font-semibold */
font-size: 0.875rem; /* text-sm */
font-size: 1.3rem; /* text-sm */
}

.logo {
width: 100%;
height: auto;
max-width: 200px;
}

ul {
display: flex;
align-items: center;
height: 100%;
}

0 comments on commit 0fc2cb1

Please sign in to comment.