-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
73e85bd
commit 0fc2cb1
Showing
3 changed files
with
54 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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%; | ||
} | ||
|