-
Notifications
You must be signed in to change notification settings - Fork 117
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
d607cf6
commit 9398480
Showing
8 changed files
with
111 additions
and
30 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,61 @@ | ||
import { getUser, postSignIn, postSignUp } from '@/api/auth'; | ||
import { PropsWithChildren, createContext, useEffect, useState } from 'react'; | ||
|
||
interface User { | ||
id: number; | ||
name: string; | ||
imageSource: string; | ||
email: string; | ||
} | ||
|
||
interface AuthContext { | ||
user?: User; | ||
signIn: (data: any) => void; | ||
signUp: (data: any) => void; | ||
signOut: () => void; | ||
} | ||
|
||
export const authContext = createContext<AuthContext>({ | ||
user: undefined, | ||
signIn: () => {}, | ||
signUp: () => {}, | ||
signOut: () => {}, | ||
}); | ||
|
||
const AuthProvider = ({ children }: PropsWithChildren) => { | ||
const [user, setUser] = useState<User>(); | ||
|
||
const signIn = async (data: any) => { | ||
const { accessToken, refreshToken } = await postSignIn(data); | ||
|
||
window.localStorage.setItem('accessToken', accessToken); | ||
window.localStorage.setItem('refreshToken', refreshToken); | ||
}; | ||
const signUp = async (data: any) => { | ||
const { accessToken, refreshToken } = await postSignUp(data); | ||
|
||
window.localStorage.setItem('accessToken', accessToken); | ||
window.localStorage.setItem('refreshToken', refreshToken); | ||
}; | ||
|
||
const signOut = () => { | ||
// TODO: 로그아웃 기능 구현 | ||
}; | ||
|
||
useEffect(() => { | ||
const fetchUser = async () => { | ||
try { | ||
const user = await getUser(); | ||
setUser(user); | ||
} catch (error) { | ||
console.error((error as Error).message); | ||
} | ||
}; | ||
|
||
fetchUser(); | ||
}, []); | ||
|
||
return <authContext.Provider value={{ user, signIn, signUp, signOut }}>{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,11 @@ | ||
import { authContext } from '@/context/authProvider'; | ||
import { useContext } from 'react'; | ||
|
||
export const useAuth = () => { | ||
const context = useContext(authContext); | ||
if (!context) { | ||
throw new Error('반드시 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
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