@@ -3,13 +3,26 @@ import axios from "axios";
33import { useMutation } from "@tanstack/react-query" ;
44import { useAuthStore } from "../../stores/useAuthStore" ;
55import { useTokenStore } from "../../stores/useTokenStore" ;
6+
7+ const getRedirectUri = ( ) : string => {
8+ const currentOrigin = window . location . origin ;
9+ const envRedirectUri = import . meta. env . VITE_KAKAO_REDIRECT_URI ;
10+ const isLocalhost = currentOrigin . includes ( 'localhost' ) ;
11+
12+ if ( isLocalhost && envRedirectUri ) {
13+ return envRedirectUri ;
14+ }
15+
16+ return envRedirectUri || `${ currentOrigin } /oauth/kakao/redirect` ;
17+ } ;
18+
619export const useKakaoLogin = ( ) => {
720 const { setLoading, setAuthError, setAuthenticated } = useAuthStore ( ) ;
821 const { setTokens } = useTokenStore ( ) ;
922
1023 const mutation = useMutation ( {
1124 mutationFn : async ( code : string ) => {
12- const redirect_uri = ` ${ window . location . origin } /oauth/kakao/redirect` ;
25+ const redirect_uri = getRedirectUri ( ) ;
1326
1427 const response = await axios . post (
1528 `${ import . meta. env . VITE_API_URL } /oauth/login/kakao` ,
@@ -51,16 +64,8 @@ export const useKakaoLogin = () => {
5164 const initiateKakaoLogin = ( ) => {
5265 setAuthError ( null ) ;
5366
54- const currentOrigin = window . location . origin ;
55- const envRedirectUri = import . meta. env . VITE_KAKAO_REDIRECT_URI ;
56- const isLocalhost = currentOrigin . includes ( 'localhost' ) ;
57-
58- let redirectUri : string ;
59- if ( isLocalhost ) {
60- redirectUri = envRedirectUri || 'http://localhost:3000/oauth/kakao/redirect' ;
61- } else {
62- redirectUri = envRedirectUri || `${ currentOrigin } /oauth/kakao/redirect` ;
63- }
67+ // 동일한 redirect_uri 계산 로직 사용
68+ const redirectUri = getRedirectUri ( ) ;
6469
6570 const nonce = Date . now ( ) ;
6671 const kakaoAuthUrl = `https://kauth.kakao.com/oauth/authorize?client_id=${ import . meta. env . VITE_KAKAO_CLIENT_ID } &redirect_uri=${ encodeURIComponent ( redirectUri ) } &response_type=code&prompt=login&nonce=${ nonce } ` ;
0 commit comments