Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,5 @@ jobs:

- name: Build test
run: npm run build
env:
NEXT_PUBLIC_API_BASE_URL: ${{secrets.NEXT_PUBLIC_API_BASE_URL}}
8 changes: 8 additions & 0 deletions next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,13 @@ const nextConfig = {
},
];
},
async rewrites() {
return [
{
source: '/:path*',
destination: `${process.env.NEXT_PUBLIC_API_BASE_URL}/:path*`,
},
];
},
};
export default nextConfig;
12 changes: 9 additions & 3 deletions src/_apis/auth/auth-apis.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,33 @@ import { fetchApi } from '@/src/utils/api';
import { LoginRequest, LoginResponse, SignupRequest, SignupResponse, User } from '@/src/types/auth';

export function signupUser(data: SignupRequest): Promise<{ data: SignupResponse }> {
return fetchApi<{ data: SignupResponse }>('/signup', {
return fetchApi<{ data: SignupResponse; headers: Headers }>('/auths/signup', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
}).then((response) => {
const token = response.headers.get('Authorization');
return { data: { token } };
Comment on lines +5 to +13
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

토큰 μΆ”μΆœ μ‹œ 였λ₯˜ 처리 둜직이 ν•„μš”ν•©λ‹ˆλ‹€

ν˜„μž¬ κ΅¬ν˜„μ—μ„œλŠ” λ‹€μŒκ³Ό 같은 잠재적인 λ¬Έμ œκ°€ μžˆμŠ΅λ‹ˆλ‹€:

  1. Authorization 헀더가 μ—†λŠ” κ²½μš°μ— λŒ€ν•œ μ²˜λ¦¬κ°€ μ—†μŠ΅λ‹ˆλ‹€
  2. 응닡 λ°μ΄ν„°μ˜ μœ νš¨μ„± 검증이 μ—†μŠ΅λ‹ˆλ‹€

λ‹€μŒκ³Ό 같이 κ°œμ„ ν•˜λŠ” 것을 μ œμ•ˆν•©λ‹ˆλ‹€:

  }).then((response) => {
    const token = response.headers.get('Authorization');
+   if (!token) {
+     throw new Error('인증 토큰이 μ—†μŠ΅λ‹ˆλ‹€');
+   }
    return { data: { token } };
  });
πŸ“ Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
return fetchApi<{ data: SignupResponse; headers: Headers }>('/auths/signup', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
}).then((response) => {
const token = response.headers.get('Authorization');
return { data: { token } };
return fetchApi<{ data: SignupResponse; headers: Headers }>('/auths/signup', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
}).then((response) => {
const token = response.headers.get('Authorization');
if (!token) {
throw new Error('인증 토큰이 μ—†μŠ΅λ‹ˆλ‹€');
}
return { data: { token } };

});
}

export function loginUser(data: LoginRequest): Promise<{ data: LoginResponse }> {
return fetchApi<{ data: LoginResponse }>('/login', {
return fetchApi<{ data: LoginResponse; headers: Headers }>('/auths/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
}).then((response) => {
const token = response.headers.get('Authorization');
return { data: { token } };
Comment on lines +18 to +26
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

πŸ› οΈ Refactor suggestion

μ½”λ“œ 쀑볡을 μ œκ±°ν•˜κ³  였λ₯˜ 처리λ₯Ό κ°œμ„ ν•΄μ•Ό ν•©λ‹ˆλ‹€

loginUser와 signupUser ν•¨μˆ˜κ°€ 맀우 μœ μ‚¬ν•œ λ‘œμ§μ„ κ°€μ§€κ³  μžˆμŠ΅λ‹ˆλ‹€. λ˜ν•œ 토큰 μΆ”μΆœ μ‹œ 였λ₯˜ μ²˜λ¦¬κ°€ λˆ„λ½λ˜μ–΄ μžˆμŠ΅λ‹ˆλ‹€.

λ‹€μŒκ³Ό 같은 κ°œμ„ μ„ μ œμ•ˆν•©λ‹ˆλ‹€:

  1. 토큰 μΆ”μΆœ λ‘œμ§μ„ 곡톡 ν•¨μˆ˜λ‘œ 뢄리:
const extractAuthToken = (headers: Headers) => {
  const token = headers.get('Authorization');
  if (!token) {
    throw new Error('인증 토큰이 μ—†μŠ΅λ‹ˆλ‹€');
  }
  return token;
};
  1. 각 ν•¨μˆ˜μ—μ„œ 곡톡 ν•¨μˆ˜ μ‚¬μš©:
  }).then((response) => {
-   const token = response.headers.get('Authorization');
+   const token = extractAuthToken(response.headers);
    return { data: { token } };
  });

});
}

export function getUser(): Promise<{ data: User }> {
return fetchApi<{ data: User }>('/user/1', {
return fetchApi<{ data: User }>('/auths/user', {
method: 'GET',
headers: {
'Content-Type': 'application/json',
Expand Down
38 changes: 25 additions & 13 deletions src/_queries/auth/auth-queries.tsx
Original file line number Diff line number Diff line change
@@ -1,36 +1,28 @@
import { ApiError } from 'next/dist/server/api-utils';
import { useMutation, useQueryClient } from '@tanstack/react-query';
import { getUser, loginUser, signupUser } from '@/src/_apis/auth/auth-apis';
import { useAuthStore } from '@/src/store/use-auth-store';
import { ApiError } from '@/src/utils/api';
import { transformKeysToCamel } from '@/src/utils/transform-keys';
import { LoginRequest, LoginResponse, SignupRequest, SignupResponse, User } from '@/src/types/auth';

export function usePostSignupQuery() {
const queryClient = useQueryClient();
const { login } = useAuthStore();
const handleAuthSuccess = useHandleAuthSuccess();

return useMutation<{ data: SignupResponse }, ApiError, SignupRequest>({
mutationFn: signupUser,
onSuccess: async (response) => {
// TODO: tokenκ°’μœΌλ‘œ μˆ˜μ •, const { token } = response.data;
const token = 'dummyToken123';
const user: User = await queryClient.fetchQuery(getUserQuery());
login(user, token);
await handleAuthSuccess(response.data.token);
},
});
}

export function usePostLoginQuery() {
const queryClient = useQueryClient();
const { login } = useAuthStore();
const handleAuthSuccess = useHandleAuthSuccess();

return useMutation<{ data: LoginResponse }, ApiError, LoginRequest>({
mutationFn: loginUser,
onSuccess: async (response) => {
// TODO: tokenκ°’μœΌλ‘œ μˆ˜μ •, const { token } = response.data;
const token = 'dummyToken123';
const user: User = await queryClient.fetchQuery(getUserQuery());
login(user, token);
await handleAuthSuccess(response.data.token);
},
});
}
Expand All @@ -42,3 +34,23 @@ export function getUserQuery() {
select: (data: User) => transformKeysToCamel(data),
};
}

function useHandleAuthSuccess() {
const queryClient = useQueryClient();
const { login, setUser } = useAuthStore();

return async function handleAuthSuccess(token: string | null) {
if (!token) {
throw new Error('토큰이 μ—†μŠ΅λ‹ˆλ‹€');
}

try {
const accessToken = token.replace(/^Bearer\s/, '');
login(accessToken);
const user: User = await queryClient.fetchQuery(getUserQuery());
setUser(user);
} catch (error) {
throw new Error('μ‚¬μš©μž μƒνƒœ μ—…λ°μ΄νŠΈ μ‹€νŒ¨');
}
};
}
6 changes: 3 additions & 3 deletions src/app/(auth)/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ export default function AuthLayout({
<div className="flex h-screen flex-col bg-gray-50">
<Header />
<div className="flex flex-1">
<div className="hidden h-full items-center md:flex md:w-1/2 lg:w-2/3">
<div className="hidden h-full items-center md:flex md:w-1/2">
<Image src={Auth} alt="auth" />
</div>
<div className="flex h-full w-full flex-col items-center bg-white p-6 md:w-1/2 md:justify-center md:p-8 lg:w-1/3 lg:p-20">
<div className="flex h-full w-full flex-col items-center bg-white p-6 md:w-1/2 md:justify-center md:p-8 lg:p-20">
<div className="text-xl font-semibold md:text-2xl lg:text-3xl">Welcome,</div>
<div className="text-center text-xl font-semibold md:text-2xl lg:text-3xl">
크루에 μ˜€μ‹  것을 ν™˜μ˜ν•©λ‹ˆλ‹€ πŸ™Œ
</div>
<div className="mt-4 text-center text-sm font-semibold lg:text-base">
ν•¨κ»˜ν•  μ‚¬λžŒμ΄μ—†λ‚˜μš”? μ§€κΈˆ 크루에 μ°Έμ—¬ν•΄λ³΄μ„Έμš”
</div>
<div className="mt-6 w-full md:mt-12 md:w-2/3">{children}</div>
<div className="mt-6 w-full md:mt-12 md:w-2/3 lg:w-1/2">{children}</div>
</div>
</div>
</div>
Expand Down
7 changes: 3 additions & 4 deletions src/app/(auth)/login/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@ export default function LoginPage() {
router.push('/');
},
onError: (error) => {
if (error.statusCode === 404) {
if (error.status === 401) {
setError('email', {
type: 'manual',
message: '이메일 계정이 μ‘΄μž¬ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€',
message: '이메일 λ˜λŠ” λΉ„λ°€λ²ˆν˜Έκ°€ μΌμΉ˜ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.',
});
} else if (error.statusCode === 401) {
setError('password', {
type: 'manual',
message: '잘λͺ»λœ λΉ„λ°€λ²ˆν˜Έμž…λ‹ˆλ‹€',
message: '이메일 λ˜λŠ” λΉ„λ°€λ²ˆν˜Έκ°€ μΌμΉ˜ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.',
});
}
},
Expand Down
19 changes: 11 additions & 8 deletions src/app/(auth)/signup/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,21 @@ export default function LoginPage() {
const { mutate: postSignup } = usePostSignupQuery();

const handleSubmit = async (data: SignupFormValues) => {
postSignup(data, {
const { confirmPassword, ...requestData } = data;

postSignup(requestData, {
onSuccess: () => {
router.push('/');
},
onError: (error) => {
if (error.statusCode === 400) {
// TODO: parameter 처리 ν›„ message 처리 확인
// const { parameter } = error.parameter;
// setError(parameter, {
// type: 'manual',
// message: error.message,
// });
if (error.status === 400) {
const { validationErrors } = error.detail;
Object.keys(validationErrors).forEach((key) => {
setError(key as 'email', {
type: 'manual',
message: validationErrors[key],
});
});
Comment on lines +24 to +31
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

πŸ› οΈ Refactor suggestion

μ—λŸ¬ 처리 둜직 κ°œμ„ μ΄ ν•„μš”ν•©λ‹ˆλ‹€

ν˜„μž¬ κ΅¬ν˜„λœ μ—λŸ¬ μ²˜λ¦¬μ— λͺ‡ κ°€μ§€ κ°œμ„ μ΄ ν•„μš”ν•©λ‹ˆλ‹€:

  1. νƒ€μž… μ•ˆμ „μ„±: key as 'email' νƒ€μž… 단언은 μœ„ν—˜ν•  수 μžˆμŠ΅λ‹ˆλ‹€
  2. μ—λŸ¬ λ©”μ‹œμ§€ 처리: μ„œλ²„ 응닡이 μ˜ˆμƒκ³Ό λ‹€λ₯Ό 경우의 μ²˜λ¦¬κ°€ μ—†μŠ΅λ‹ˆλ‹€
  3. μ‚¬μš©μž ν”Όλ“œλ°±: 일반적인 μ„œλ²„ 였λ₯˜μ— λŒ€ν•œ μ²˜λ¦¬κ°€ λˆ„λ½λ˜μ—ˆμŠ΅λ‹ˆλ‹€

λ‹€μŒκ³Ό 같이 κ°œμ„ ν•˜λŠ” 것을 μ œμ•ˆν•©λ‹ˆλ‹€:

 if (error.status === 400) {
   const { validationErrors } = error.detail;
-  Object.keys(validationErrors).forEach((key) => {
-    setError(key as 'email', {
-      type: 'manual',
-      message: validationErrors[key],
-    });
-  });
+  try {
+    Object.entries(validationErrors).forEach(([key, message]) => {
+      if (key in formMethods.getValues()) {
+        setError(key as keyof SignupFormValues, {
+          type: 'manual',
+          message: String(message),
+        });
+      }
+    });
+  } catch (e) {
+    console.error('μœ νš¨μ„± 검사 μ—λŸ¬ 처리 쀑 λ¬Έμ œκ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€:', e);
+    setError('root', {
+      type: 'manual',
+      message: 'νšŒμ›κ°€μž… 처리 쀑 λ¬Έμ œκ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€. λ‹€μ‹œ μ‹œλ„ν•΄ μ£Όμ„Έμš”.',
+    });
+  }
+} else {
+  setError('root', {
+    type: 'manual',
+    message: 'μ„œλ²„ 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€. μž μ‹œ ν›„ λ‹€μ‹œ μ‹œλ„ν•΄ μ£Όμ„Έμš”.',
+  });
 }
πŸ“ Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (error.status === 400) {
const { validationErrors } = error.detail;
Object.keys(validationErrors).forEach((key) => {
setError(key as 'email', {
type: 'manual',
message: validationErrors[key],
});
});
if (error.status === 400) {
const { validationErrors } = error.detail;
try {
Object.entries(validationErrors).forEach(([key, message]) => {
if (key in formMethods.getValues()) {
setError(key as keyof SignupFormValues, {
type: 'manual',
message: String(message),
});
}
});
} catch (e) {
console.error('μœ νš¨μ„± 검사 μ—λŸ¬ 처리 쀑 λ¬Έμ œκ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€:', e);
setError('root', {
type: 'manual',
message: 'νšŒμ›κ°€μž… 처리 쀑 λ¬Έμ œκ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€. λ‹€μ‹œ μ‹œλ„ν•΄ μ£Όμ„Έμš”.',
});
}
} else {
setError('root', {
type: 'manual',
message: 'μ„œλ²„ 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€. μž μ‹œ ν›„ λ‹€μ‹œ μ‹œλ„ν•΄ μ£Όμ„Έμš”.',
});
}

}
},
});
Expand Down
5 changes: 3 additions & 2 deletions src/components/common/header/header.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const meta: Meta = {

export default meta;
function Template() {
const { isAuth, login, logout } = useAuthStore();
const { isAuth, login, logout, setUser } = useAuthStore();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

인증 둜직 κ°œμ„ μ΄ ν•„μš”ν•©λ‹ˆλ‹€.

둜그인 ν”„λ‘œμ„ΈμŠ€κ°€ 두 λ‹¨κ³„λ‘œ λΆ„λ¦¬λ˜μ–΄ μžˆμ–΄ 잠재적인 λ¬Έμ œκ°€ λ°œμƒν•  수 μžˆμŠ΅λ‹ˆλ‹€. loginκ³Ό setUser 호좜 사이에 경쟁 μƒνƒœκ°€ λ°œμƒν•  수 있으며, μ΄λŠ” μ‚¬μš©μž μƒνƒœκ°€ μΌμ‹œμ μœΌλ‘œ λΆˆμΌμΉ˜ν•˜λŠ” 상황을 μ΄ˆλž˜ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

λ‹€μŒκ³Ό 같이 κ°œμ„ ν•˜λŠ” 것을 μ œμ•ˆλ“œλ¦½λ‹ˆλ‹€:

-      login(testToken);
-      setUser(testUser);
+      await login(testToken);
+      await setUser(testUser);

λ˜λŠ” 더 λ‚˜μ€ λ°©λ²•μœΌλ‘œ, 인증 λ‘œμ§μ„ ν•˜λ‚˜μ˜ ν•¨μˆ˜λ‘œ ν†΅ν•©ν•˜λŠ” 것을 κ³ λ €ν•΄λ³΄μ„Έμš”:

-      login(testToken);
-      setUser(testUser);
+      await loginWithUser(testToken, testUser);

Also applies to: 37-38

const testToken = 'test token';
const testUser = {
id: 1,
Expand All @@ -34,7 +34,8 @@ function Template() {
if (isAuth) {
logout();
} else {
login(testUser, testToken);
login(testToken);
setUser(testUser);
}
};

Expand Down
7 changes: 4 additions & 3 deletions src/store/use-auth-store.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ interface AuthState {
isAuth: boolean;
user: User | null;
token: string | null;
login: (userData: User, token: string) => void;
login: (token: string) => void;
logout: () => void;
setUser: (user: User) => void;
}

export const useAuthStore = create<AuthState>()(
Expand All @@ -16,10 +17,9 @@ export const useAuthStore = create<AuthState>()(
isAuth: false,
user: null,
token: null,
login: (userData, token) =>
login: (token) =>
set({
isAuth: true,
user: userData,
token,
}),
logout: () =>
Expand All @@ -28,6 +28,7 @@ export const useAuthStore = create<AuthState>()(
user: null,
token: null,
}),
setUser: (user: User) => set((state) => ({ ...state, user })),
}),
{
name: 'auth-storage',
Expand Down
4 changes: 2 additions & 2 deletions src/types/auth.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export interface SignupResponse {
token: string;
token: string | null;
}

export interface SignupRequest {
Expand All @@ -9,7 +9,7 @@ export interface SignupRequest {
}

export interface LoginResponse {
token: string;
token: string | null;
}

export interface LoginRequest {
Expand Down
22 changes: 12 additions & 10 deletions src/utils/api.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { useAuthStore } from '@/src/store/use-auth-store';

// TODO: μΆ”ν›„ API URL μˆ˜μ •
const API_BASE_URL = process.env.NEXT_PUBLIC_API_BASE_URL || 'http://localhost:3009';

export class ApiError extends Error {
detail: { validationErrors: Record<string, string> } = { validationErrors: {} };

constructor(
public status: number,
message: string,
detail?: { validationErrors: Record<string, string> }, // νƒ€μž…μ„ 맞좀
) {
super(message);
this.name = 'ApiError';
if (detail) this.detail = detail;
}
}

Expand All @@ -23,7 +24,6 @@ export async function fetchApi<T>(
const { signal } = controller;
const id = setTimeout(() => controller.abort(), timeout);
const { token } = useAuthStore.getState();

const fetchOptions: RequestInit = {
...options,
signal,
Expand All @@ -35,21 +35,23 @@ export async function fetchApi<T>(
};

try {
const response = await fetch(`${API_BASE_URL}${url}`, fetchOptions); // API μš”μ²­ μ‹€ν–‰
const response = await fetch(`${url}`, fetchOptions); // API μš”μ²­ μ‹€ν–‰
if (!response.ok) {
let errorDetail;
let errorMessage;
try {
const errorData = await response.json();
errorMessage = errorData.message || `HTTP error! status: ${response.status}`;
const { status, message, ...detail } = await response.json();
errorMessage = message || `HTTP error! status: ${response.status}`;
errorDetail = detail;
} catch {
errorMessage = `HTTP error! status: ${response.status}`;
}

throw new ApiError(response.status, errorMessage);
throw new ApiError(response.status, errorMessage, errorDetail);
}

// 응닡 데이터λ₯Ό JSON ν˜•νƒœλ‘œ λ°˜ν™˜
return (await response.json()) as T;
const data = await response.json();
return { ...data, headers: response.headers } as T;
} catch (error) {
if (error instanceof Error) {
if (error.name === 'AbortError') throw new ApiError(408, 'Request timeout');
Expand Down