-
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.
Merge branch 'fe-dev' of https://github.com/boostcampwm2023/web12-alg…
…o-with-me into 20-유저가-작성한-코드-결과-서버로-보내기
- Loading branch information
Showing
14 changed files
with
263 additions
and
44 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { createContext } from 'react'; | ||
|
||
const AuthContext = createContext({ | ||
isLoggedin: false, | ||
login: () => {}, | ||
logout: () => {}, | ||
}); | ||
|
||
export default AuthContext; |
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { useState } from 'react'; | ||
|
||
import AuthContext from './AuthContext'; | ||
|
||
const AuthProvider = ({ children }: { children: React.ReactNode }) => { | ||
function logout() { | ||
setIsLogined(false); | ||
} | ||
function login() { | ||
setIsLogined(true); | ||
} | ||
|
||
const [isLoggedin, setIsLogined] = useState<boolean>(false); | ||
|
||
return ( | ||
<AuthContext.Provider value={{ isLoggedin, logout, login }}>{children}</AuthContext.Provider> | ||
); | ||
}; | ||
|
||
export default AuthProvider; |
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
interface Props { | ||
size: string; | ||
} | ||
|
||
export default function Logo({ size }: Props) { | ||
// public이란 곳에 리소스 넣어두면 build시에 바로 밑에 생기기 때문에 ./으로 접근 가능합니다. | ||
return <img src="./algo.png" alt="logo" width={size} height={size} />; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { css } from '@style/css'; | ||
|
||
import Logo from '@/components/Common/Logo'; | ||
import useAuth from '@/hooks/login/useAuth'; | ||
|
||
export default function Header() { | ||
const { changeLoginInfo, changeLogoutInfo, isLoggedin } = useAuth(); | ||
|
||
const handleLogin = () => { | ||
changeLoginInfo(); | ||
}; | ||
|
||
const handleLogout = () => { | ||
changeLogoutInfo(); | ||
}; | ||
|
||
return ( | ||
<header className={headerStyle}> | ||
<Logo size="36px" /> | ||
{isLoggedin ? ( | ||
<button onClick={handleLogout}> 로그아웃 </button> | ||
) : ( | ||
<button onClick={handleLogin}> 로그인 </button> | ||
)} | ||
</header> | ||
); | ||
} | ||
|
||
const headerStyle = css({ | ||
width: '100%', | ||
height: '40px', | ||
display: 'flex', | ||
alignItems: 'center', | ||
justifyContent: 'space-between', | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { css } from '@style/css'; | ||
|
||
interface Props { | ||
onClickLogin: () => void; | ||
} | ||
|
||
export default function Login({ onClickLogin }: Props) { | ||
return ( | ||
<section className={loginWrapperStyle}> | ||
<header className={loginHeaderStyle}>Algo With Me</header> | ||
<button onClick={onClickLogin}>Github으로 로그인</button> | ||
</section> | ||
); | ||
} | ||
|
||
const loginWrapperStyle = css({ | ||
border: '1px solid white', | ||
borderRadius: '10px', | ||
width: '100%', | ||
height: '100%', | ||
maxWidth: '500px', | ||
maxHeight: '200px', | ||
display: 'flex', | ||
flexDirection: 'column', | ||
justifyContent: 'space-between', | ||
}); | ||
|
||
const loginHeaderStyle = css({ | ||
fontSize: '3rem', | ||
fontWeight: 'bold', | ||
textAlign: 'center', | ||
padding: '1rem', | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { useContext, useEffect } from 'react'; | ||
import { useLocation, useNavigate } from 'react-router-dom'; | ||
|
||
import AuthContext from '@/components/Auth/AuthContext'; | ||
|
||
import axios from 'axios'; | ||
|
||
const TOKEN_KEY = 'accessToken'; | ||
const BASE_URL = import.meta.env.VITE_API_URL; | ||
const AUTH_TEST_PATH = '/auths/tests'; | ||
|
||
const URL = `${BASE_URL}${AUTH_TEST_PATH}`; | ||
|
||
const fetchTokenValid = async (token: string) => { | ||
try { | ||
const response = await axios.get(URL, { | ||
headers: { Authorization: `Bearer ${token}` }, | ||
}); | ||
// {email: '[email protected]', nickname: '[email protected]'} | ||
// 저장할 지 말지는 나중에 결정 | ||
const data = await response.data; | ||
console.log(data, '인증 받음.'); | ||
if (response.status === 200) return true; | ||
// 올바른 유저라는 검증을 받음. | ||
} catch (e) { | ||
console.log('인증 받지 못함.', e); | ||
return false; | ||
} | ||
}; | ||
|
||
export default function useAuth() { | ||
const { isLoggedin, login, logout } = useContext(AuthContext); | ||
|
||
const location = useLocation(); | ||
const navigate = useNavigate(); | ||
|
||
useEffect(() => { | ||
if (isLoggedin) return; | ||
const queryParams = new URLSearchParams(location.search); | ||
const token = queryParams.get(TOKEN_KEY) || localStorage.getItem(TOKEN_KEY); | ||
|
||
if (!token) return; | ||
evaluateToken(token); | ||
}, []); | ||
|
||
const evaluateToken = async (token: string) => { | ||
const isValid = await fetchTokenValid(token); | ||
isValid ? saveAuthInfo(token) : removeAuthInfo(); | ||
}; | ||
|
||
const saveAuthInfo = (token: string) => { | ||
localStorage.setItem(TOKEN_KEY, token); | ||
login(); | ||
}; | ||
|
||
const removeAuthInfo = () => { | ||
// accessToken 없애기 | ||
// AuthContext 없애기 | ||
localStorage.removeItem(TOKEN_KEY); | ||
logout(); | ||
}; | ||
|
||
const changeLoginInfo = () => { | ||
// accessToken 없애기 | ||
// AuthContext 없애기 | ||
// 로그인 페이지로 이동 | ||
removeAuthInfo(); | ||
navigate('/login'); | ||
}; | ||
|
||
const changeLogoutInfo = () => { | ||
// accessToken 없애기 | ||
// AuthContext 없애기 | ||
removeAuthInfo(); | ||
}; | ||
return { changeLoginInfo, changeLogoutInfo, isLoggedin }; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import Login from '@/components/Login'; | ||
|
||
const GITHUB_AUTH_URL = 'http://101.101.208.240:3000/auths/github'; | ||
|
||
export default function LoginPage() { | ||
// 넘겨주는 함수는 handle, 함수를 넘길 때의 프로펄티 네임은 on | ||
const handleLogin = () => { | ||
try { | ||
window.location.href = GITHUB_AUTH_URL; | ||
} catch (e) { | ||
const error = e as Error; | ||
console.error(error.message); | ||
} | ||
}; | ||
|
||
return <Login onClickLogin={handleLogin} />; | ||
} |
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
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