-
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
Showing
7 changed files
with
110 additions
and
56 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
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,26 @@ | ||
'use client'; | ||
|
||
import { createContext, useContext, useState } from 'react'; | ||
|
||
interface UserStatusContextType { | ||
isFirstTimeUser: boolean; | ||
setIsFirstTimeUser: (value: boolean) => void; | ||
} | ||
|
||
const UserStatusContext = createContext<UserStatusContextType | undefined>(undefined); | ||
|
||
export const UserStatusProvider = ({ children }: { children: React.ReactNode }) => { | ||
const [isFirstTimeUser, setIsFirstTimeUser] = useState<boolean>(false); | ||
|
||
return ( | ||
<UserStatusContext.Provider value={{ isFirstTimeUser, setIsFirstTimeUser }}>{children}</UserStatusContext.Provider> | ||
); | ||
}; | ||
|
||
export const useUserStatus = () => { | ||
const context = useContext(UserStatusContext); | ||
if (context === undefined) { | ||
throw new Error('useUserStatus must be used within a UserStatusProvider'); | ||
} | ||
return context; | ||
}; |
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,51 @@ | ||
import { AxiosError } from 'axios'; | ||
import { saveAccessToken } from '../api/oauth'; | ||
import { useUserStatus } from '../context/userStatusContext'; | ||
import instance from '@/shared/api/axios/instance'; | ||
import { API_BASE_URL } from '@/shared/api/constants'; | ||
|
||
export const useLogin = () => { | ||
const { isFirstTimeUser, setIsFirstTimeUser } = useUserStatus(); | ||
|
||
const handleLogin = async (code: string) => { | ||
try { | ||
if (isFirstTimeUser) { | ||
await getNewUserLoginToken(code); | ||
|
||
return; | ||
} | ||
|
||
await getLoginToken(code); | ||
} catch (error: unknown) { | ||
if (error instanceof AxiosError) { | ||
if (error.response?.data?.code === 'ERR_4010') { | ||
// 최초 로그인 사용자인 경우 | ||
alert('최초 가입자입니다. 회원가입 페이지로 이동합니다.'); | ||
setIsFirstTimeUser(true); | ||
window.location.href = '/signup'; | ||
} else { | ||
alert('로그인 실패:'); | ||
console.error(error.response?.data); | ||
} | ||
} | ||
} | ||
}; | ||
|
||
const getLoginToken = async (code: string) => { | ||
const response = await instance.get(`${API_BASE_URL}/api/v1/oauth/login/kakao?code=${code}`); | ||
const jwt = response.headers?.['authorization'] as string; | ||
|
||
saveAccessToken(jwt); | ||
setIsFirstTimeUser(false); | ||
}; | ||
|
||
const getNewUserLoginToken = async (code: string) => { | ||
const response = await instance.get(`${API_BASE_URL}/api/v1/oauth/login/kakao/new?code=${code}`); | ||
const jwt = response.headers?.['authorization'] as string; | ||
|
||
saveAccessToken(jwt); | ||
setIsFirstTimeUser(false); | ||
}; | ||
|
||
return { handleLogin, getLoginToken, getNewUserLoginToken }; | ||
}; |
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