diff --git a/src/api/userApi.ts b/src/api/userApi.ts index cb6104a..60f3cc0 100644 --- a/src/api/userApi.ts +++ b/src/api/userApi.ts @@ -1,9 +1,11 @@ import { api } from './axiosInstance'; import type { User } from '../types'; -// API: GET /api/users/me -// (Authorization: Bearer ) -export async function fetchMyProfile(): Promise { - const { data } = await api.get('/api/users/me'); +export async function postUserMe(): Promise { + await api.post('users/me', null); +} + +export async function getMyProfile(): Promise { + const { data } = await api.get('users/me'); return data; } diff --git a/src/auth/callback.ts b/src/auth/callback.ts index 3f49e45..2b5df7f 100644 --- a/src/auth/callback.ts +++ b/src/auth/callback.ts @@ -1,4 +1,6 @@ import { COGNITO } from './config'; +import { useAuthStore } from '../store/useAuthStore'; +import { postUserMe, getMyProfile } from '../api/userApi'; export type TokenResponse = { id_token: string; @@ -45,11 +47,22 @@ export async function handleAuthCallback(): Promise { if (!res.ok) throw new Error(json.error_description ?? 'Token exchange failed'); - localStorage.setItem('id_token', json.id_token); - localStorage.setItem('access_token', json.access_token); - if (json.refresh_token) localStorage.setItem('refresh_token', json.refresh_token); + useAuthStore.getState().setTokens({ + idToken: json.id_token ?? null, + accessToken: json.access_token ?? null, + refreshToken: json.refresh_token ?? null, + }); sessionStorage.removeItem('pkce_verifier'); + + try { + await postUserMe(); + const me = await getMyProfile(); + useAuthStore.getState().setUser(me); + } catch (e) { + console.error('[callback] user sync failed', e); + } + // code 제거 (새로고침 시 재호출 방지) window.history.replaceState({}, '', COGNITO.redirectUri); diff --git a/src/auth/config.ts b/src/auth/config.ts index 4d88377..de6b964 100644 --- a/src/auth/config.ts +++ b/src/auth/config.ts @@ -17,5 +17,5 @@ export const COGNITO: CognitoConfig = { hostedUiDomain: requireEnv('VITE_COGNITO_DOMAIN'), clientId: requireEnv('VITE_COGNITO_CLIENT_ID'), redirectUri: requireEnv('VITE_COGNITO_REDIRECT_URI'), - scopes: ['openid', 'email'], + scopes: ['openid', 'email', 'profile'], }; diff --git a/src/store/useAuthStore.ts b/src/store/useAuthStore.ts index f21755a..506d158 100644 --- a/src/store/useAuthStore.ts +++ b/src/store/useAuthStore.ts @@ -24,6 +24,9 @@ interface AuthState { setTokens: (tokens: Tokens) => void; syncFromStorage: () => void; clearTokens: () => void; + + // [추가] /user/me 응답을 전역에 반영하기 위한 액션 + setUser: (_user: User | null) => void; // ← [추가] } // Mock users (기존 유지) @@ -96,6 +99,11 @@ export const useAuthStore = create((set, get) => ({ // 추가: 교환 결과를 직접 세팅하고 싶을 때 사용 가능 setTokens: (tokens: Tokens) => { + // [수정] 새로고침 유지 위해 로컬스토리지에도 반영 + localStorage.setItem('id_token', tokens.idToken ?? ''); + localStorage.setItem('access_token', tokens.accessToken ?? ''); + localStorage.setItem('refresh_token', tokens.refreshToken ?? ''); + set({ tokens, isAuthenticated: Boolean(tokens.accessToken), @@ -123,6 +131,9 @@ export const useAuthStore = create((set, get) => ({ isAuthenticated: false, }); }, + + // [추가] /user/me 결과 반영 + setUser: (_user) => set({ user: _user }), // ← [추가] })); // 기존 유지