-
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.
Merge pull request #1080 from ayoung-iya/part3-윤아영-week20
[윤아영] week19 & week20
- Loading branch information
Showing
12 changed files
with
223 additions
and
33 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { BASE_URL } from '@/util/apiConstants'; | ||
|
||
export const postSignIn = async ({ email, password }: { email: string; password: string }) => { | ||
const response = await fetch(`${BASE_URL}/auth/sign-in`, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ email, password }), | ||
}); | ||
|
||
if (!response?.ok) { | ||
throw new Error('로그인을 실패했습니다.'); | ||
} | ||
|
||
const token = await response.json(); | ||
|
||
return token; | ||
}; | ||
|
||
export const postCheckEmail = async (email: string) => { | ||
const response = await fetch(`${BASE_URL}/users/check-email`, { | ||
method: 'POST', | ||
headers: { 'Content-Type': 'application/json' }, | ||
body: JSON.stringify({ email }), | ||
}); | ||
|
||
const body = await response.json(); | ||
|
||
if (!response?.ok) { | ||
throw new Error(body.message); | ||
} | ||
}; | ||
|
||
export const postSignUp = async ({ email, password }: { email: string; password: string }) => { | ||
const response = await fetch(`${BASE_URL}/auth/sign-up`, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ email, password }), | ||
}); | ||
|
||
if (!response?.ok) { | ||
throw new Error('회원가입을 실패했습니다.'); | ||
} | ||
|
||
const token = await response.json(); | ||
|
||
return token; | ||
}; | ||
|
||
export const getUser = async () => { | ||
const token = window.localStorage.getItem('accessToken'); | ||
|
||
const response = await fetch(`${BASE_URL}/users`, { | ||
headers: { | ||
Authorization: `Bearer ${token}`, | ||
}, | ||
}); | ||
|
||
const body = await response.json(); | ||
|
||
if (!response?.ok) { | ||
throw new Error(body.message); | ||
} | ||
|
||
return {...body[0], imageSource: body[0]['image_source']}; | ||
}; |
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,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
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,2 @@ | ||
export const BASE_URL_LEGACY = 'https://bootcamp-api.codeit.kr/api'; | ||
export const BASE_URL = 'https://bootcamp-api.codeit.kr/api/linkbrary/v1'; |