diff --git a/src/apis/auth/.keep b/src/apis/auth/.keep deleted file mode 100644 index e69de29b..00000000 diff --git a/src/apis/auth/usePostKakaoLogin.ts b/src/apis/auth/usePostKakaoLogin.ts new file mode 100644 index 00000000..d7fe42fe --- /dev/null +++ b/src/apis/auth/usePostKakaoLogin.ts @@ -0,0 +1,51 @@ +import { useMutation, useQueryClient } from '@tanstack/react-query'; +import { AxiosResponse } from 'axios'; + +import { post } from '@apis/instance'; + +import { END_POINT, queryKey } from '@constants'; +import { useEasyNavigate } from '@hooks'; + +import { ApiResponseType, LoginSuccessResponse } from '@types'; + +const postKakaoLogin = async ( + authCode: string +): Promise>> => { + const response = await post>( + END_POINT.KAKAO_LOGIN, + { + socialType: 'KAKAO', + redirectUri: import.meta.env.VITE_REDIRECT_URI, + }, + { + headers: { + Authorization: authCode, + }, + } + ); + + return response; +}; + +export const usePostKakaoLogin = () => { + const queryClient = useQueryClient(); + const { goHomePage } = useEasyNavigate(); + + return useMutation({ + mutationFn: (authCode: string) => postKakaoLogin(authCode), + onSuccess: (response) => { + const resData = response.data.data; + if (resData) { + const { userId, userName } = resData; + const user = { userId, userName }; + localStorage.setItem('user', JSON.stringify(user)); + + queryClient.invalidateQueries({ queryKey: [queryKey.KAKAO_LOGIN] }); + goHomePage(); + } + }, + onError: (error) => { + console.log(error); + }, + }); +}; diff --git a/src/apis/instance.ts b/src/apis/instance.ts index 8168c7e6..a1e6216f 100644 --- a/src/apis/instance.ts +++ b/src/apis/instance.ts @@ -6,10 +6,6 @@ export const instance = axios.create({ baseURL: BASE_URL, withCredentials: false, - - // headers: { - // // Authorization: `Bearer 엑세스 토큰`, - // }, 서버에서 set-cookie로 줘서 만약 필요 없으면 지울 예정 }); export function get(...args: Parameters) { diff --git a/src/components/common/SocialLoginButton/SocialLoginButton.tsx b/src/components/common/SocialLoginButton/SocialLoginButton.tsx index ae539a07..e520b86d 100644 --- a/src/components/common/SocialLoginButton/SocialLoginButton.tsx +++ b/src/components/common/SocialLoginButton/SocialLoginButton.tsx @@ -13,12 +13,17 @@ const platformIcon = { kakao: , }; -const SocialLoginButton = ({ platform, children }: SocialLoginButtonProps) => { +const SocialLoginButton = ({ + platform, + children, + onClick, +}: SocialLoginButtonProps) => { return (