@@ -9,33 +9,54 @@ import { useFormStore } from '../../store/formStore';
99import { useRouter } from 'next/navigation' ;
1010
1111export default function Page1 ( ) {
12- const { email, setEmail, setId } = useFormStore ( ) ;
12+ const { email, setEmail, setId, setAccessToken } = useFormStore ( ) ;
1313 const router = useRouter ( ) ;
1414
1515 const handleSubmit = async ( e : React . FormEvent ) => {
1616 e . preventDefault ( ) ;
1717 console . log ( 'Form Submitted:' , { email } ) ;
1818
1919 try {
20- const response = await fetch ( '/api/v1/users /auth' , {
20+ const authResponse = await fetch ( '/api/v1/auth' , {
2121 method : 'POST' ,
2222 headers : {
2323 'Content-Type' : 'application/json' ,
2424 } ,
2525 body : JSON . stringify ( { email } ) ,
2626 } ) ;
2727
28- if ( ! response . ok ) {
29- throw new Error ( `HTTP error! status: ${ response . status } ` ) ;
28+ if ( ! authResponse . ok ) {
29+ throw new Error ( `HTTP error! status: ${ authResponse . status } ` ) ;
3030 }
3131
32- const result = await response . json ( ) ;
33- console . log ( 'Auth API Response:' , result ) ;
34- if ( result . id ) {
35- setId ( result . id ) ; // Store the ID in the form store
32+ const authResult = await authResponse . json ( ) ;
33+ const newAccessToken = authResult . accessToken ;
34+
35+ if ( ! newAccessToken ) {
36+ throw new Error ( 'Access token not received from auth API.' ) ;
37+ }
38+ setAccessToken ( newAccessToken ) ; // Store the new access token
39+
40+ const userMeResponse = await fetch ( '/api/v1/users/me' , {
41+ method : 'GET' ,
42+ headers : {
43+ 'Content-Type' : 'application/json' ,
44+ 'Authorization' : `Bearer ${ newAccessToken } ` , // Use the new access token
45+ } ,
46+ } ) ;
47+
48+ if ( ! userMeResponse . ok ) {
49+ throw new Error ( `HTTP error! status: ${ userMeResponse . status } ` ) ;
50+ }
51+
52+ const userMeResult = await userMeResponse . json ( ) ;
53+ console . log ( 'User Me API Response:' , userMeResult ) ;
54+
55+ if ( userMeResult . sub ) {
56+ setId ( userMeResult . sub ) ;
3657 }
3758 alert ( '정보가 제출되었습니다!' ) ;
38- router . push ( '/page2' ) ; // Navigate to page2 after successful submission
59+ router . push ( '/page2' ) ;
3960 } catch ( error ) {
4061 console . error ( 'Error submitting form:' , error ) ;
4162 alert ( '정보 제출에 실패했습니다.' ) ;
0 commit comments