-
Notifications
You must be signed in to change notification settings - Fork 0
[Feat] 로그인, 회원가입 페이지 api 함수, msw 연결 #111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
81c931a
Merge remote-tracking branch 'origin/main' into yoolri-feat/api
yoorli f9f1da7
feat: 회원가입/로그인 요청·응답 타입 정의
yoorli 51bdea2
feat: Auth 서비스 구현 및 API 모듈 연결
yoorli 71e5a3b
feat: Auth 목 유저 DB와 로그인·회원가입·로그아웃 핸들러 구현
yoorli 004b12b
feat: 회원가입·로그인 폼에 Auth API 연동 및 에러 처리 추가
yoorli 826418e
Merge remote-tracking branch 'origin/main' into yoolri-feat/api
yoorli c67e9fb
fix: 리뷰 사항 반영
yoorli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import type { AxiosError } from 'axios'; | ||
|
|
||
| import { baseAPI } from '@/api/core'; | ||
| import { LoginRequest, LoginResponse, SignupRequest, SignupResponse } from '@/types/service/auth'; | ||
| import { CommonErrorResponse } from '@/types/service/common'; | ||
|
|
||
| export const isProblemDetailError = (error: unknown): error is AxiosError<CommonErrorResponse> => { | ||
| return ( | ||
| typeof error === 'object' && | ||
| error !== null && | ||
| 'isAxiosError' in error && | ||
| (error as AxiosError).isAxiosError === true | ||
| ); | ||
| }; | ||
|
|
||
| export const authServiceRemote = () => ({ | ||
| // 로그인 | ||
| login: async (payload: LoginRequest): Promise<LoginResponse> => { | ||
| const { data } = await baseAPI.post<LoginResponse>('/api/v1/auth/login', payload); | ||
| return data; | ||
| }, | ||
|
|
||
| // 회원가입 | ||
| signup: async (payload: SignupRequest): Promise<SignupResponse> => { | ||
| const { data } = await baseAPI.post<SignupResponse>('/api/v1/auth/signup', payload); | ||
| return data; | ||
| }, | ||
|
|
||
| // 로그아웃 | ||
| logout: async (): Promise<void> => { | ||
| await baseAPI.post('/api/v1/auth/logout'); | ||
| }, | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| export * from './auth-service'; | ||
| export * from './user-service'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,7 +1,10 @@ | ||||||
| 'use client'; | ||||||
|
|
||||||
| import { useRouter } from 'next/navigation'; | ||||||
|
|
||||||
| import { type AnyFieldApi, useForm } from '@tanstack/react-form'; | ||||||
|
|
||||||
| import { authServiceRemote, isProblemDetailError } from '@/api/service'; | ||||||
| import { FormInput } from '@/components/shared'; | ||||||
| import { Button } from '@/components/ui'; | ||||||
| import { loginSchema } from '@/lib/schema/auth'; | ||||||
|
|
@@ -19,6 +22,9 @@ const getHintMessage = (field: AnyFieldApi) => { | |||||
| }; | ||||||
|
|
||||||
| export const LoginForm = () => { | ||||||
| const router = useRouter(); | ||||||
| const { login } = authServiceRemote(); | ||||||
|
|
||||||
| const form = useForm({ | ||||||
| defaultValues: { | ||||||
| email: '', | ||||||
|
|
@@ -28,9 +34,29 @@ export const LoginForm = () => { | |||||
| onSubmit: loginSchema, | ||||||
| onChange: loginSchema, | ||||||
| }, | ||||||
| onSubmit: async ({ value }) => { | ||||||
| // API 호출 | ||||||
| alert('login:' + value.email); | ||||||
| onSubmit: async ({ value, formApi }) => { | ||||||
| try { | ||||||
| const payload = { | ||||||
| email: value.email, | ||||||
| password: value.password, | ||||||
| }; | ||||||
|
|
||||||
| const result = await login(payload); | ||||||
|
||||||
| const result = await login(payload); | |
| const result = API.authService.login(payload); |
이렇게 사용하시면 됩니다!
그러면 아래 구문이 필요없어지거든요
const { login } = authServiceRemote();
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| import { authHandlers } from './service/auth/auth-handlers'; | ||
| import { userHandlers } from './service/user/users-handler'; | ||
|
|
||
| export const handlers = [...userHandlers]; | ||
| export const handlers = [...userHandlers, ...authHandlers]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import { http, HttpResponse } from 'msw'; | ||
|
|
||
| import { LoginRequest, LoginResponse, SignupRequest } from '@/types/service/auth'; | ||
| import { CommonErrorResponse } from '@/types/service/common'; | ||
|
|
||
| import { | ||
| createLoginResponse, | ||
| createSignupResponse, | ||
| isEmailTaken, | ||
| isNicknameTaken, | ||
| } from './auth-utils'; | ||
|
|
||
| const signupMock = http.post('*/api/v1/auth/signup', async ({ request }) => { | ||
| const body = (await request.json()) as SignupRequest; | ||
|
|
||
| if (isEmailTaken(body.email)) { | ||
| const errorBody: CommonErrorResponse = { | ||
| type: 'https://example.com/errors/email-duplicate', | ||
| title: 'EMAIL_DUPLICATE', | ||
| status: 400, | ||
| detail: '이미 존재하는 이메일입니다.', | ||
| instance: '/api/v1/auth/signup', | ||
| errorCode: 'A002', | ||
| }; | ||
| return HttpResponse.json<CommonErrorResponse>(errorBody, { status: 400 }); | ||
| } | ||
|
|
||
| if (isNicknameTaken(body.nickName)) { | ||
| const errorBody: CommonErrorResponse = { | ||
| type: 'https://example.com/errors/nickname-duplicate', | ||
| title: 'NICKNAME_DUPLICATE', | ||
| status: 400, | ||
| detail: '이미 존재하는 닉네임입니다.', | ||
| instance: '/api/v1/auth/signup', | ||
| errorCode: 'A003', | ||
| }; | ||
| return HttpResponse.json<CommonErrorResponse>(errorBody, { status: 400 }); | ||
| } | ||
| const response = createSignupResponse(body.email, body.nickName, body.password); | ||
| return HttpResponse.json(response, { status: 201 }); | ||
| }); | ||
|
|
||
| const loginMock = http.post('*/api/v1/auth/login', async ({ request }) => { | ||
| const body = (await request.json()) as LoginRequest; | ||
|
|
||
| try { | ||
| const response = createLoginResponse(body.email, body.password); | ||
| return HttpResponse.json<LoginResponse>(response, { status: 200 }); | ||
| } catch { | ||
| const errorBody: CommonErrorResponse = { | ||
| type: 'https://example.com/errors/invalid-credentials', | ||
| title: 'INVALID_CREDENTIALS', | ||
| status: 400, | ||
| detail: '이메일 또는 비밀번호가 올바르지 않습니다.', | ||
| instance: '/api/v1/auth/login', | ||
| errorCode: 'A001', | ||
| }; | ||
| return HttpResponse.json(errorBody, { status: 400 }); | ||
| } | ||
| }); | ||
|
|
||
| const logoutMock = http.post('*/api/v1/auth/logout', async ({}) => { | ||
| return new HttpResponse(null, { status: 204 }); | ||
| }); | ||
|
|
||
| export const authHandlers = [signupMock, loginMock, logoutMock]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| export interface AuthMockUser { | ||
| id: number; | ||
| email: string; | ||
| nickName: string; | ||
| password: string; | ||
| createdAt: string; | ||
| } | ||
|
|
||
| export const authMockUsers: AuthMockUser[] = [ | ||
| { | ||
| id: 1, | ||
| email: '[email protected]', | ||
| nickName: '리오넬 메시', | ||
| password: 'test9876', | ||
| createdAt: '2025-01-30T12:00:00', | ||
| }, | ||
| { | ||
| id: 2, | ||
| email: '[email protected]', | ||
| nickName: '크리스티아누 호날두', | ||
| password: 'test1234', | ||
| createdAt: '2025-01-30T12:00:00', | ||
| }, | ||
| { | ||
| id: 3, | ||
| email: '[email protected]', | ||
| nickName: '페르난도 토레스', | ||
| password: '123456789', | ||
| createdAt: '2025-01-30T12:00:00', | ||
| }, | ||
| ]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| import type { LoginResponse, SignupResponse } from '@/types/service/auth'; | ||
|
|
||
| import { AuthMockUser, authMockUsers } from './auth-mock'; | ||
|
|
||
| const findUserByEmail = (email: string) => authMockUsers.find((user) => user.email === email); | ||
| const findUserByNickname = (nickName: string) => | ||
| authMockUsers.find((user) => user.nickName === nickName); | ||
|
|
||
| const findUserByCredentials = (email: string, password: string) => | ||
| authMockUsers.find((user) => user.email === email && user.password === password); | ||
|
|
||
| const createMockTokens = () => ({ | ||
| accessToken: 'mock-access-token', | ||
| refreshToken: 'mock-refresh-token', | ||
| tokenType: 'Bearer' as const, | ||
| expiresIn: 3600, | ||
| }); | ||
|
|
||
| export const createLoginResponse = (email: string, password: string): LoginResponse => { | ||
| const user = findUserByCredentials(email, password); | ||
| if (!user) { | ||
| throw new Error('INVALID_CREDENTIALS'); | ||
| } | ||
|
|
||
| const tokens = createMockTokens(); | ||
|
|
||
| return { | ||
| ...tokens, | ||
| user: { | ||
| id: user.id, | ||
| email: user.email, | ||
| nickName: user.nickName, | ||
| }, | ||
| }; | ||
| }; | ||
|
|
||
| export const createSignupResponse = ( | ||
| email: string, | ||
| nickName: string, | ||
| password: string, | ||
| ): SignupResponse => { | ||
| const newUser: AuthMockUser = { | ||
| id: authMockUsers.length + 1, | ||
| email, | ||
| nickName, | ||
| password, | ||
| createdAt: new Date().toISOString(), | ||
| }; | ||
|
|
||
| authMockUsers.push(newUser); | ||
|
|
||
| return { | ||
| id: newUser.id, | ||
| email: newUser.email, | ||
| nickName: newUser.nickName, | ||
| createdAt: newUser.createdAt, | ||
| }; | ||
| }; | ||
|
|
||
| export const isEmailTaken = (email: string) => !!findUserByEmail(email); | ||
| export const isNicknameTaken = (nickName: string) => !!findUserByNickname(nickName); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| export interface SignupRequest { | ||
| email: string; | ||
| password: string; | ||
| nickName: string; | ||
| } | ||
|
|
||
| export interface SignupResponse { | ||
| id: number; | ||
| email: string; | ||
| nickName: string; | ||
| createdAt: string; | ||
| } | ||
|
|
||
| export interface LoginRequest { | ||
| email: string; | ||
| password: string; | ||
| } | ||
|
|
||
| export interface LoginResponse { | ||
| accessToken: string; | ||
| refreshToken: string; | ||
| tokenType: 'Bearer'; | ||
| expiresIn: number; | ||
| user: { | ||
| id: number; | ||
| email: string; | ||
| nickName: string; | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| export interface CommonErrorResponse { | ||
| type: string; | ||
| title: string; | ||
| status: number; | ||
| detail: string; | ||
| instance: string; | ||
| errorCode?: string; | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
지금보니까 제가 작업을 잘못했네요 😭
authService 키와 값이 동일하므로 아래와 같이 변경하면 되겠네용
userService는 추후에 제가 수정하겠습니다!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
넵