-
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
8 changed files
with
89 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
'use client'; | ||
|
||
import React, { createContext, useState, useEffect, useContext, PropsWithChildren } from 'react'; | ||
import { useRouter } from 'next/navigation'; | ||
import { | ||
ACCESS_TOKEN_KEY, | ||
REFRESH_TOKEN_KEY, | ||
USER_KEY, | ||
removeLocalStorageItem, | ||
getAccessTokenByLocalStorage, | ||
getRefreshTokenByLocalStorage, | ||
getUserByLocalStorage, | ||
UserType, | ||
} from '@oseek/lib'; | ||
import { PAGE_URL } from '@constant/pageUrl'; | ||
|
||
interface AuthContextProps { | ||
user: UserType | null; | ||
isLoggedIn: boolean; | ||
logout: () => void; | ||
} | ||
|
||
const AuthContext = createContext<AuthContextProps | undefined>(undefined); | ||
|
||
export const AuthProvider = ({ children }: PropsWithChildren) => { | ||
const router = useRouter(); | ||
const [user, setUser] = useState<UserType | null>(null); | ||
|
||
const logout = () => { | ||
setUser(null); | ||
removeLocalStorageItem(ACCESS_TOKEN_KEY); | ||
removeLocalStorageItem(REFRESH_TOKEN_KEY); | ||
removeLocalStorageItem(USER_KEY); | ||
router.replace(PAGE_URL.MAIN); | ||
}; | ||
|
||
const isLoggedIn = () => { | ||
const accessToken = getAccessTokenByLocalStorage(); | ||
const refreshToken = getRefreshTokenByLocalStorage(); | ||
return user !== null && accessToken !== null && refreshToken !== null; | ||
}; | ||
|
||
useEffect(() => { | ||
const storedUser = getUserByLocalStorage(); | ||
if (storedUser) { | ||
setUser(storedUser); | ||
} | ||
}, []); | ||
|
||
return <AuthContext.Provider value={{ user, isLoggedIn: isLoggedIn(), logout }}>{children}</AuthContext.Provider>; | ||
}; | ||
|
||
export const useAuth = (): AuthContextProps => { | ||
const context = useContext(AuthContext); | ||
if (!context) { | ||
throw new Error('useAuth를 사용하려면 AuthProvider를 상위에 제공해야 합니다.'); | ||
} | ||
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,5 @@ | ||
/** | ||
* @file Automatically generated by barrelsby. | ||
*/ | ||
|
||
export * from './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 |
---|---|---|
|
@@ -3,3 +3,4 @@ | |
*/ | ||
|
||
export * from './foodKeyword'; | ||
export * from './pageUrl'; |
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,4 @@ | ||
export const PAGE_URL = { | ||
MAIN: '/', | ||
SETTING_NAME: '/login/setting-name', | ||
}; |
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,4 +1,3 @@ | ||
export const ACCESS_TOKEN_KEY = 'accessToken'; | ||
export const REFRESH_TOKEN_KEY = 'refreshToken'; | ||
export const NICKNAME_KEY = 'nickname'; | ||
export const LOCATION_KEY = 'location'; | ||
export const USER_KEY = 'user'; |
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