@@ -8,14 +8,18 @@ import { useRouter } from "next/navigation";
88import AuthButton from "@/components/auth/AuthButton" ;
99import AuthLayout from "@/components/auth/AuthLayout" ;
1010import RoleSelect from "@/components/auth/RoleSelect" ;
11+ import { useOnboardingGuard } from "@/hooks/useOnboardingGuard" ;
1112import { ApiError } from "@/services/apiClient" ;
1213import { confirmImageUpload , issueImageUploadUrl , uploadImageToStorage } from "@/services/imageApi" ;
13- import { completeWelcome } from "@/services/userApi" ;
14+ import { completeWelcome , updateProfile } from "@/services/userApi" ;
1415import { useAuthSignupStore } from "@/stores/authSignupStore" ;
16+ import { useAuthStore } from "@/stores/useAuthStore" ;
1517
1618export default function RolePage ( ) {
1719 const router = useRouter ( ) ;
18- const { nickname, bio, role, profileImage, setRole, reset } = useAuthSignupStore ( ) ;
20+ const { canRender } = useOnboardingGuard ( ) ;
21+ const { nickname, bio, role, profileImage, signupToken, setRole, reset } = useAuthSignupStore ( ) ;
22+ const setAuth = useAuthStore ( s => s . setAuth ) ;
1923 const [ errorMessage , setErrorMessage ] = useState < string | null > ( null ) ;
2024
2125 const welcomeMutation = useMutation ( {
@@ -24,31 +28,43 @@ export default function RolePage() {
2428 throw new Error ( "역할을 선택해주세요." ) ;
2529 }
2630
27- let profileImageId : number | null = null ;
28-
29- if ( profileImage ) {
30- const uploadInfo = await issueImageUploadUrl ( {
31- filename : profileImage . name ,
32- contentType : profileImage . type ,
33- fileSize : profileImage . size ,
34- } ) ;
35- const uploadResponse = await uploadImageToStorage ( uploadInfo . uploadUrl , profileImage ) ;
36-
37- if ( ! uploadResponse . ok ) {
38- throw new Error ( "프로필 이미지 업로드 중 오류가 발생했습니다." ) ;
39- }
40-
41- await confirmImageUpload ( uploadInfo . imageId ) ;
42-
43- profileImageId = uploadInfo . imageId ;
31+ if ( ! signupToken ) {
32+ throw new Error ( "회원가입 인증 정보가 만료되었습니다. 다시 로그인해주세요." ) ;
4433 }
4534
46- return completeWelcome ( {
35+ const welcomeResponse = await completeWelcome ( {
36+ signupToken,
4737 nickname : nickname . trim ( ) ,
4838 bio : bio . trim ( ) ,
4939 role,
50- profileImageId,
40+ profileImageId : null ,
5141 } ) ;
42+
43+ setAuth ( { accessToken : welcomeResponse . accessToken , userId : welcomeResponse . userId } ) ;
44+
45+ if ( profileImage ) {
46+ try {
47+ const uploadInfo = await issueImageUploadUrl ( {
48+ filename : profileImage . name ,
49+ contentType : profileImage . type ,
50+ fileSize : profileImage . size ,
51+ } ) ;
52+ const uploadResponse = await uploadImageToStorage ( uploadInfo . uploadUrl , profileImage ) ;
53+
54+ if ( uploadResponse . ok ) {
55+ const confirmedImage = await confirmImageUpload ( uploadInfo . imageId ) ;
56+ await updateProfile ( {
57+ bio : bio . trim ( ) ,
58+ snsUrl : null ,
59+ profileImageUrl : confirmedImage . imageUrl ,
60+ } ) ;
61+ }
62+ } catch {
63+ // 프로필 이미지는 선택 항목이므로 계정 생성 성공 후 업로드 실패가 가입 완료를 막지 않는다.
64+ }
65+ }
66+
67+ return welcomeResponse ;
5268 } ,
5369 onSuccess : ( ) => {
5470 reset ( ) ;
@@ -81,6 +97,8 @@ export default function RolePage() {
8197
8298 const isSubmitDisabled = ! role || welcomeMutation . isPending ;
8399
100+ if ( ! canRender ) return null ;
101+
84102 return (
85103 < AuthLayout
86104 title = "역할 선택하기"
0 commit comments