1+ /* eslint-disable @typescript-eslint/no-unused-expressions */
12import { useEffect } from 'react' ;
23import { useNavigate } from 'react-router' ;
34
4- import { getUserToken } from '@/apis/auth' ;
5+ import { getUserToken , getMydata , deleteUserInfo , getZipCode } from '@/apis/auth' ;
56import useAuthStore from '@/stores/authStore' ;
67
78const AuthCallbackPage = ( ) => {
89 const stateToken = new URLSearchParams ( window . location . search ) . get ( 'state' ) ;
910 const redirectURL = new URLSearchParams ( window . location . search ) . get ( 'redirect' ) ;
1011
11- const { setUserId , setZipCode, setAccessToken, zipCode } = useAuthStore ( ) ;
12+ const { setZipCode, setAccessToken, login } = useAuthStore ( ) ;
1213
1314 const navigate = useNavigate ( ) ;
1415
1516 const setUserInfo = async ( stateToken : string ) => {
1617 try {
17- const userInfo = await getUserToken ( stateToken ) ;
18- if ( ! userInfo ) throw new Error ( 'Error Fetching userInfo' ) ;
18+ const response = await getUserToken ( stateToken ) ;
19+ if ( ! response ) throw new Error ( 'Error Fetching userInfo' ) ;
1920
20- const accessToken = userInfo . match ( / B e a r e r \s + ( \S + ) / ) ;
21- if ( accessToken ) setAccessToken ( accessToken [ 1 ] ) ;
22- console . log ( 'token' , accessToken ) ;
21+ const userInfo = response . data ;
22+ if ( userInfo ) {
23+ login ( ) ;
24+ userInfo . accessToken && setAccessToken ( userInfo . accessToken ) ;
2325
24- const userId = userInfo . match ( / U s e r I d = ( \d + ) / ) ;
25- if ( userId ) setUserId ( userId [ 1 ] ) ;
26+ if ( redirectURL == 'home' ) {
27+ const zipCodeResponse = await getMydata ( ) ;
28+ if ( ! zipCodeResponse ) throw new Error ( 'Error Fetching userInfo' ) ;
29+ const zipCode = zipCodeResponse . data . data . zipCode ;
30+ zipCode && setZipCode ( zipCode ) ;
2631
27- const zipCode = userInfo . match ( / Z i p C o d e = ( [ A - Z a - z 0 - 9 ] + ) / ) ;
28- if ( zipCode ) setZipCode ( zipCode [ 1 ] ) ;
32+ console . log (
33+ 'isLoggedIn' ,
34+ useAuthStore . getState ( ) . isLoggedIn ,
35+ 'access' ,
36+ useAuthStore . getState ( ) . accessToken ,
37+ 'zipCode' ,
38+ useAuthStore . getState ( ) . zipCode ,
39+ ) ;
40+ } else if ( redirectURL === 'onboarding' ) {
41+ const createZipCodeResponse = await getZipCode ( ) ;
42+ if ( ! createZipCodeResponse ) throw new Error ( 'Error creating ZipCode' ) ;
43+ const zipCode = createZipCodeResponse . data . data . zipCode ;
44+ const newAccessToken = createZipCodeResponse . headers [ 'authorizazion' ] ;
45+
46+ setZipCode ( zipCode ) ;
47+ setAccessToken ( newAccessToken ) ;
48+ }
49+ } else {
50+ navigate ( '/login' ) ;
51+ }
2952 } catch ( error ) {
3053 console . error ( error ) ;
3154 }
3255 } ;
3356
3457 // const redirection = () => {
35- // if(redirectURL === 'onboarding') navigate('/onboarding');
36- // else if(redirectURL === 'home') navigate('/');
58+ // if (redirectURL === 'onboarding') navigate('/onboarding');
59+ // else if (redirectURL === 'home') navigate('/');
3760 // else navigate('/notFound');
3861 // };
3962
@@ -44,7 +67,16 @@ const AuthCallbackPage = () => {
4467 }
4568 // else navigate('/notFound');
4669 } , [ ] ) ;
47- return < div > </ div > ;
70+
71+ const handleLeave = async ( ) => {
72+ try {
73+ const response = await deleteUserInfo ( ) ;
74+ console . log ( response ) ;
75+ } catch ( error ) {
76+ console . error ( error ) ;
77+ }
78+ } ;
79+ return < button onClick = { ( ) => handleLeave ( ) } > 탈퇴</ button > ;
4880} ;
4981
5082export default AuthCallbackPage ;
0 commit comments