-
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.
Browse files
Browse the repository at this point in the history
[Feat/#16] 사용자 로그인 및 회원가입 기능 추가
- Loading branch information
Showing
20 changed files
with
393 additions
and
91 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 |
---|---|---|
@@ -1,8 +1,16 @@ | ||
// dependencies | ||
import { Router } from "@/routes" | ||
|
||
const App: React.FC = () => { | ||
return <Router></Router> | ||
import { QueryClient, QueryClientProvider } from "@tanstack/react-query" | ||
|
||
const queryClient = new QueryClient() | ||
|
||
const App = (): React.ReactElement => { | ||
return ( | ||
<QueryClientProvider client={queryClient}> | ||
<Router></Router> | ||
</QueryClientProvider> | ||
) | ||
} | ||
|
||
export default App |
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,87 @@ | ||
// src/api/auth.ts | ||
import axiosInstance, { setAccessToken } from "@/api/axiosInstance" | ||
import qs from "qs" | ||
|
||
const REST_API_KEY = import.meta.env.VITE_OAUTH_KAKAO_REST_API_KEY | ||
const CLIENT_SECRET = import.meta.env.VITE_OAUTH_KAKAO_CLIENT_SECRET_CODE | ||
const REDIRECT_URI = import.meta.env.VITE_OAUTH_KAKAO_REDIRECT_URI | ||
|
||
export interface authUser { | ||
uid: number | ||
nickname: string | ||
accessToken: string | ||
} | ||
|
||
export interface oauthUser { | ||
nickname: string | ||
} | ||
|
||
export const oauth = async (code: string): Promise<string> => { | ||
const formData = { | ||
grant_type: "authorization_code", | ||
client_id: REST_API_KEY, | ||
client_secret: CLIENT_SECRET, | ||
redirect_uri: REDIRECT_URI, | ||
code, | ||
} | ||
|
||
try { | ||
const res = await axiosInstance.post(`https://kauth.kakao.com/oauth/token?${qs.stringify(formData)}`, null, { | ||
headers: { "Content-type": "application/x-www-form-urlencoded" }, | ||
}) | ||
return res.data.access_token | ||
} catch (e) { | ||
throw e | ||
} | ||
} | ||
|
||
export const getOauthUser = async (accessToken: string): Promise<oauthUser> => { | ||
const kakaoUser = await axiosInstance.get(`https://kapi.kakao.com/v2/user/me`, { | ||
headers: { | ||
Authorization: `Bearer ${accessToken}`, | ||
}, | ||
}) | ||
const { nickname } = kakaoUser.data.kakao_account.profile | ||
|
||
return { nickname } | ||
} | ||
|
||
export const signIn = async (_accessToken: string): Promise<authUser> => { | ||
try { | ||
const res = await axiosInstance.post(`/oauth/kakao/sign-in`, { accessToken: _accessToken }) | ||
const { uid, nickname, accessToken } = res.data.data | ||
|
||
// 로그인 성공 후 엑세스 토큰을 설정 | ||
setAccessToken(accessToken) | ||
|
||
return { uid, nickname, accessToken } | ||
} catch (e) { | ||
throw e | ||
} | ||
} | ||
|
||
export const signUp = async (_accessToken: string): Promise<authUser> => { | ||
try { | ||
const res = await axiosInstance.post(`/oauth/kakao/sign-up`, { accessToken: _accessToken }) | ||
const { uid, nickname, accessToken } = res.data.data | ||
|
||
// 회원가입 성공 후 엑세스 토큰을 설정 | ||
setAccessToken(accessToken) | ||
|
||
return { uid, nickname, accessToken } | ||
} catch (e) { | ||
throw e | ||
} | ||
} | ||
|
||
export const getIsSignUp = async (accessToken: string): Promise<boolean> => { | ||
try { | ||
const res = await axiosInstance.get(`/oauth/kakao/sign-up/check`, { | ||
params: { accessToken }, | ||
}) | ||
console.log(res.data) | ||
return res.data.data.isExistsUser | ||
} catch (e) { | ||
throw e | ||
} | ||
} |
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,31 @@ | ||
// src/services/axiosInstance.ts | ||
import axios from "axios" | ||
|
||
const API_BASE_URL = import.meta.env.VITE_API_BASE_URL | ||
|
||
const axiosInstance = axios.create({ | ||
baseURL: API_BASE_URL, | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
}) | ||
|
||
// localStorage에서 토큰 가져오기 | ||
const token = localStorage.getItem("accessToken") | ||
if (token) { | ||
axiosInstance.defaults.headers.common["Authorization"] = `Bearer ${token}` | ||
} | ||
|
||
// 엑세스 토큰 설정 함수 | ||
export const setAccessToken = (token: string) => { | ||
axiosInstance.defaults.headers.common["Authorization"] = `Bearer ${token}` | ||
localStorage.setItem("accessToken", token) | ||
} | ||
|
||
// 엑세스 토큰 제거 함수 | ||
export const clearAccessToken = () => { | ||
delete axiosInstance.defaults.headers.common["Authorization"] | ||
localStorage.removeItem("accessToken") | ||
} | ||
|
||
export default axiosInstance |
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 @@ | ||
export * from "./auth" |
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 { authUser, getIsSignUp, getOauthUser, oauth, oauthUser, signIn, signUp } from "@/api" | ||
import { useMutation, UseMutationResult } from "@tanstack/react-query" | ||
|
||
export const useOauth = (): UseMutationResult<string, unknown, string, unknown> => { | ||
return useMutation({ | ||
mutationFn: (code: string) => { | ||
return oauth(code) | ||
}, | ||
onSuccess: (data) => { | ||
console.log(data) | ||
}, | ||
}) | ||
} | ||
|
||
export const useGetOauthUser = (): UseMutationResult<oauthUser, unknown, string, unknown> => { | ||
return useMutation({ | ||
mutationFn: (accessToken: string) => getOauthUser(accessToken), | ||
onSuccess: (data) => { | ||
console.log("Oauth user data:", data) | ||
}, | ||
onError: (error) => { | ||
console.error("Error fetching oauth user:", error) | ||
}, | ||
}) | ||
} | ||
|
||
export const useSignIn = (): UseMutationResult<authUser, unknown, string, unknown> => { | ||
return useMutation({ | ||
mutationFn: (_accessToken: string) => signIn(_accessToken), | ||
onSuccess: (data) => { | ||
console.log("User signed in:", data) | ||
}, | ||
onError: (error) => { | ||
console.error("Error signing in:", error) | ||
}, | ||
}) | ||
} | ||
|
||
export const useSignUp = (): UseMutationResult<authUser, unknown, string, unknown> => { | ||
return useMutation({ | ||
mutationFn: (_accessToken: string) => signUp(_accessToken), | ||
onSuccess: (data) => { | ||
console.log("User signed up:", data) | ||
}, | ||
onError: (error) => { | ||
console.error("Error signing up:", error) | ||
}, | ||
}) | ||
} | ||
|
||
export const useGetIsSignUp = (): UseMutationResult<boolean, unknown, string, unknown> => { | ||
return useMutation({ | ||
mutationFn: (accessToken: string) => getIsSignUp(accessToken), | ||
onSuccess: (data) => { | ||
console.log("User signup status:", data) | ||
}, | ||
onError: (error) => { | ||
console.error("Error checking signup status:", error) | ||
}, | ||
}) | ||
} |
Oops, something went wrong.