-
Notifications
You must be signed in to change notification settings - Fork 1
feat: 로그인&회원가입 api 구현 #11
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 all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
6da68d6
chore(signup): react-hook-form 및 zod 설치
hwanseok1014 c402c32
feat(signup): 이메일 중복확인 api구현
hwanseok1014 ecb30c3
feat(signup): 회원가입 api구현
hwanseok1014 fd0e6c6
feat(signup): 이메일 인증하기 api 구현
hwanseok1014 195bd8e
feat(signup): 이메일 인증 다시 보내기 api 구현
hwanseok1014 4eaf462
fix(signup): 회원가입 완료 시 전역 이메일 상태 초기화
hwanseok1014 e176f2c
feat(login): 로그인 api 구현
hwanseok1014 2e3f1d2
feat(axios): axios interceptors 구현
hwanseok1014 3ba3494
fix(login): 로그인 실패 시 유효성 오류 메시지 추가
hwanseok1014 2dc55b7
fix(electron): api 연동을 위한 api도메인 추가
hwanseok1014 5fa0a5a
fix(login): 버튼 컴포넌트 스타일 수정 및 로그인 버튼 조건부 활성화 적용
hwanseok1014 0fb966c
fix: build 오류 수정
hwanseok1014 48c7891
fix: .gitignore수정
hwanseok1014 5430d74
fix: Vite임시 캐시 파일(.timestamp.mjs) 삭제
hwanseok1014 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
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
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,67 @@ | ||
| import axios, { AxiosError, AxiosInstance, AxiosRequestConfig } from 'axios'; | ||
|
|
||
| const api: AxiosInstance = axios.create({ | ||
| baseURL: import.meta.env.VITE_BASE_URL as string, | ||
| withCredentials: true, | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| }, | ||
| }); | ||
|
|
||
| api.interceptors.request.use( | ||
| (config) => { | ||
| const accessToken = localStorage.getItem('accessToken'); | ||
| if (accessToken) { | ||
| config.headers.Authorization = `Bearer ${accessToken}`; | ||
| } | ||
| return config; | ||
| }, | ||
| (error) => { | ||
| return Promise.reject(error); | ||
| }, | ||
| ); | ||
|
|
||
| api.interceptors.response.use( | ||
| (response) => response, | ||
| async (error: AxiosError) => { | ||
| const originalRequest = error.config as AxiosRequestConfig & { | ||
| _retry?: boolean; | ||
| }; // 무한 요청 방지 | ||
|
|
||
| if (error.response?.status === 401 && !originalRequest._retry) { | ||
| originalRequest._retry = true; | ||
| try { | ||
| const refreshToken = localStorage.getItem('refreshToken'); | ||
|
|
||
| const { data: newToken } = await axios.post<{ | ||
| accessToken: string; | ||
| refreshToken: string; | ||
| }>( | ||
| `${import.meta.env.VITE_BASE_URL}/auth/refresh`, | ||
| { refreshToken }, | ||
| { withCredentials: true }, | ||
| ); | ||
|
|
||
| localStorage.setItem('accessToken', newToken.accessToken); | ||
| localStorage.setItem('refreshToken', newToken.refreshToken); | ||
|
|
||
| api.defaults.headers.common['Authorization'] = | ||
| `Bearer ${newToken.accessToken}`; | ||
| if (originalRequest.headers) { | ||
| originalRequest.headers['Authorization'] = | ||
| `Bearer ${newToken.accessToken}`; | ||
| } | ||
|
|
||
| return api(originalRequest); | ||
| } catch (_err) { | ||
| localStorage.clear(); | ||
| window.location.href = '/auth/login'; | ||
| return Promise.reject(_err); | ||
| } | ||
| } | ||
|
|
||
| return Promise.reject(error); | ||
| }, | ||
| ); | ||
|
|
||
| export default api; |
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,37 @@ | ||
| import { useMutation } from '@tanstack/react-query'; | ||
| import { useNavigate } from 'react-router-dom'; | ||
| import api from '../api'; | ||
| import { LoginInput, LoginResponse } from '../../types/login/mutation'; | ||
|
|
||
| /*로그인 api */ | ||
| const login = async (data: LoginInput): Promise<LoginResponse> => { | ||
| const response = await api.post<LoginResponse>('/auth/login', data); | ||
| const result = response.data; | ||
|
|
||
| if (!result.success) { | ||
| throw new Error(result.message || '로그인 실패'); | ||
| } | ||
|
|
||
| return result; | ||
| }; | ||
|
|
||
| export const useLoginMutation = () => { | ||
| const navigate = useNavigate(); | ||
|
|
||
| return useMutation({ | ||
| mutationFn: login, | ||
| onSuccess: async (res) => { | ||
| console.log('로그인 성공', res); | ||
|
|
||
| /*access Token, refresh Token 저장 */ | ||
| localStorage.setItem('accessToken', res.data.accessToken); | ||
| localStorage.setItem('refreshToken', res.data.refreshToken); | ||
|
|
||
| navigate('/onboarding'); | ||
| }, | ||
| onError: (error) => { | ||
| console.error('로그인 오류:', error); | ||
| alert('로그인 실패'); | ||
| }, | ||
| }); | ||
| }; |
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,56 @@ | ||
| import { useMutation } from '@tanstack/react-query'; | ||
| import api from '../api'; | ||
| import { useNavigate } from 'react-router-dom'; | ||
|
|
||
| export interface SignupRequest { | ||
| email: string; | ||
| password: string; | ||
| name: string; | ||
| callbackUrl: string; | ||
| avatar?: string; | ||
| } | ||
|
|
||
| /* 이메일 중복 확인 api */ | ||
| const duplicatedEmail = async (email: string) => { | ||
| const response = await api.post('/auth/check-email', { email }); | ||
| return response.data; | ||
| }; | ||
|
|
||
| /* 회원가입 api */ | ||
| const signupUser = async (data: SignupRequest) => { | ||
| const response = await api.post(`/auth/sign-up`, { | ||
| ...data, | ||
| callbackUrl: `${window.location.origin}/auth/verify`, | ||
| }); | ||
| const result = response.data; | ||
|
|
||
| /* 회원가입 실패 시 예외 처리 */ | ||
| if (!result.success) { | ||
| throw new Error(result || '회원가입 실패'); | ||
| } | ||
|
|
||
| return response.data; | ||
| }; | ||
|
|
||
| export const useDuplicatedEmailMutation = () => { | ||
| return useMutation({ | ||
| mutationFn: duplicatedEmail, | ||
| }); | ||
| }; | ||
|
|
||
| export const useSignupMutation = () => { | ||
| const navigate = useNavigate(); | ||
|
|
||
| return useMutation({ | ||
| mutationFn: signupUser, | ||
| onSuccess: (data) => { | ||
| // 회원가입 성공 시, 인증 안내 페이지로 이동 | ||
| navigate('/auth/verify'); | ||
| console.log('회원가입 성공:', data); | ||
| }, | ||
| onError: (error: unknown) => { | ||
| console.error('회원가입 실패:', error); | ||
| alert('회원가입에 실패했습니다. 잠시 후 다시 시도해주세요.'); | ||
| }, | ||
| }); | ||
| }; |
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,69 @@ | ||
| import { useMutation } from '@tanstack/react-query'; | ||
| import api from '../api'; | ||
| import { useNavigate } from 'react-router-dom'; | ||
|
|
||
| export interface ResendVerifyEmailRequest { | ||
| email: string; | ||
| callbackUrl: string; | ||
| } | ||
|
|
||
| /*이메일 인증 api*/ | ||
| const verifyEmail = async (token: string) => { | ||
| const response = await api.post('/auth/verify-email', { token }); | ||
| const result = response.data; | ||
|
|
||
| if (!result.success) { | ||
| throw new Error(result || '인증 실패'); | ||
| } | ||
|
|
||
| return response.data; | ||
| }; | ||
|
|
||
| /*이메일 인증 다시 보내기 api*/ | ||
| const resendVerifyEmail = async (data: ResendVerifyEmailRequest) => { | ||
| const response = await api.post('/auth/resend-verification-email', { | ||
| ...data, | ||
| callbackUrl: `${window.location.origin}/auth/resend`, | ||
| }); | ||
|
|
||
| const result = response.data; | ||
|
|
||
| if (!result.success) { | ||
| console.log(result); | ||
| throw new Error(result || '다시 보내기 실패'); | ||
| } | ||
|
|
||
| return response.data; | ||
| }; | ||
|
|
||
| export const useVerifyEmailMutation = () => { | ||
| return useMutation({ | ||
| mutationFn: verifyEmail, | ||
|
|
||
| onSuccess: (data) => { | ||
| console.log('이메일 인증 성공:', data); | ||
| alert('인증 성공!'); | ||
| localStorage.clear(); | ||
| }, | ||
| onError: (error: unknown) => { | ||
| console.error('인증 실패:', error); | ||
| alert('인증 실패! 다시 시도해주세요'); | ||
| }, | ||
| }); | ||
| }; | ||
|
|
||
| export const useResendVerifyEmailMuation = () => { | ||
| const navigate = useNavigate(); | ||
|
|
||
| return useMutation({ | ||
| mutationFn: resendVerifyEmail, | ||
|
|
||
| onSuccess: (data) => { | ||
| navigate('/auth/resend'); | ||
| console.log('인증 다시 보내기 성공:', data); | ||
| }, | ||
| onError: (error: unknown) => { | ||
| console.error('인증 다시 보내기 실패:', error); | ||
| }, | ||
| }); | ||
| }; |
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
Oops, something went wrong.
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.
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.
저 지금하는 작업이랑 충돌 날거 같은데 제가 고칠게요 pr 날리고 나서