-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: 로그인, 로그아웃 커스텀 훅 구현 * feat: 토큰 조회 모킹 서버 구현 * feat: LoginRedirectPage 컴포넌트 구현 * feat: 로그인 리다이렉트 페이지 라우터 적용 * chore: 클라이언트 아이디 환경변수 설정 * feat: login, logout 기능 구현
- Loading branch information
Showing
20 changed files
with
205 additions
and
17 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
API_URL="" | ||
CLIENT_ID="cb83d95cd5644436b090" |
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,6 @@ | ||
import axiosInstance from '@api/axiosInstance'; | ||
|
||
export const getAccessToken = async (code: string) => { | ||
const response = await axiosInstance.get(`/api/login/token?code=${code}`); | ||
return response.data; | ||
}; |
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,24 @@ | ||
import { ReactNode, createContext, useState } from 'react'; | ||
|
||
import noop from '@utils/noop'; | ||
|
||
interface LoginProviderProps { | ||
children: ReactNode; | ||
} | ||
|
||
interface ContextType { | ||
isLoggedIn: boolean; | ||
setIsLoggedIn: React.Dispatch<React.SetStateAction<boolean>>; | ||
} | ||
|
||
const hasAccessToken = !!window.localStorage.getItem('accessToken'); | ||
|
||
export const LoginContext = createContext<ContextType>({ | ||
isLoggedIn: false, | ||
setIsLoggedIn: noop, | ||
}); | ||
|
||
export const LoginProvider = ({ children }: LoginProviderProps) => { | ||
const [isLoggedIn, setIsLoggedIn] = useState(hasAccessToken); | ||
return <LoginContext.Provider value={{ isLoggedIn, setIsLoggedIn }}>{children}</LoginContext.Provider>; | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { useContext } from 'react'; | ||
|
||
import { LoginContext } from '@context/login/LoginProvider'; | ||
|
||
export const useAuth = () => { | ||
const { setIsLoggedIn } = useContext(LoginContext); | ||
|
||
const login = (accesssToken: string) => { | ||
localStorage.setItem('accessToken', accesssToken); | ||
setIsLoggedIn(true); | ||
}; | ||
|
||
const logout = () => { | ||
localStorage.removeItem('accessToken'); | ||
setIsLoggedIn(false); | ||
}; | ||
|
||
return { login, logout }; | ||
}; |
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
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,12 @@ | ||
import { rest } from 'msw'; | ||
|
||
export const tokenHandlers = [ | ||
rest.get('/api/login/token', (req, res, ctx) => { | ||
const code = req.url.searchParams.get('code'); | ||
|
||
if (!code) { | ||
return res(ctx.status(400), ctx.json({ message: '잘못된 요청입니다.' })); | ||
} | ||
return res(ctx.status(200), ctx.json({ token: 'asddfasdfassdf' })); | ||
}), | ||
]; |
48 changes: 48 additions & 0 deletions
48
frontend/src/pages/login-redirect-page/LoginRedirectPage.tsx
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,48 @@ | ||
import { useQuery } from 'react-query'; | ||
import { Navigate, useSearchParams } from 'react-router-dom'; | ||
|
||
import type { TokenQueryData } from '@custom-types/index'; | ||
|
||
import { getAccessToken } from '@api/getAccessToken'; | ||
|
||
import { useAuth } from '@hooks/useAuth'; | ||
|
||
import Wrapper from '@components/wrapper/Wrapper'; | ||
|
||
const LoginRedirectPage: React.FC = () => { | ||
const [searchParams] = useSearchParams(); | ||
const codeParam = searchParams.get('code') as string; | ||
const { login } = useAuth(); | ||
|
||
const { data, isSuccess, isError, error } = useQuery<TokenQueryData, Error>( | ||
['redirect', codeParam], | ||
() => getAccessToken(codeParam), | ||
{ | ||
enabled: !!codeParam, | ||
cacheTime: 0, | ||
}, | ||
); | ||
|
||
if (!codeParam) { | ||
alert('잘못된 접근입니다.'); | ||
return <Navigate to="/" replace={true} />; | ||
} | ||
|
||
if (isSuccess) { | ||
login(data.token); | ||
return <Navigate to="/" replace={true} />; | ||
} | ||
|
||
if (isError) { | ||
alert(error.message); | ||
return <Navigate to="/" replace={true} />; | ||
} | ||
|
||
return ( | ||
<Wrapper> | ||
<div>로그인 진행 중입니다...</div> | ||
</Wrapper> | ||
); | ||
}; | ||
|
||
export default LoginRedirectPage; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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