-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: 로그인, 회원가입 레이아웃 및 로그인 로직 구현 * feat: axios 에러 커스텀 * chore: 주석 추가 * feat: axios request interceptors 추가
- Loading branch information
Showing
8 changed files
with
394 additions
and
134 deletions.
There are no files selected for viewing
This file contains 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 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,17 +1,26 @@ | ||
import axios from 'axios'; | ||
|
||
const axiosClient = axios.create({ | ||
baseURL: 'http://fteam-env-1.eba-ciibaeid.ap-northeast-2.elasticbeanstalk.com/api', | ||
}); | ||
import { axiosClient } from './client'; | ||
|
||
export type LoginResponse = { | ||
memberId: number; | ||
accessToken: string; | ||
refreshToken: string; | ||
}; | ||
//signup and login apis | ||
export async function postEmail(email: string) { | ||
return await axiosClient.post('/v1/auth/email/send', { email }); | ||
return await axiosClient.post('/auth/email/send', { email }); | ||
} | ||
|
||
export async function postEmailAndCode(email: string, verificationCode: number) { | ||
return await axiosClient.post('/v1/auth/email/authenticate', { email, verificationCode }); | ||
return await axiosClient.post('/auth/email/authenticate', { email, verificationCode }); | ||
} | ||
|
||
export async function postSignUp(email: string, password: string, confirmPassword: string) { | ||
return await axiosClient.post('/v1/signup', { email, password, confirmPassword }); | ||
return await axiosClient.post('/signup', { email, password, confirmPassword }); | ||
} | ||
|
||
export async function postLogin(email: string, password: string) { | ||
const { | ||
data: { memberId, accessToken, refreshToken }, | ||
} = await axiosClient.post<LoginResponse>('/auth/login', { email, password }); | ||
return { memberId, accessToken, refreshToken }; | ||
} |
This file contains 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 { useCallback } from 'react'; | ||
import { useMutation } from '@tanstack/react-query'; | ||
import { useForm } from 'react-hook-form'; | ||
|
||
import { postLogin } from '~/apis'; | ||
import { isEffError } from '~/apis/client'; | ||
import { emailPattern, passwordPattern } from '~/constants/validationPattern'; | ||
|
||
interface CheckLoginForm { | ||
email: string; | ||
password: string; | ||
} | ||
|
||
export const useCheckLoginForm = () => { | ||
const { | ||
register, | ||
getValues, | ||
formState: { isDirty, isValid }, | ||
} = useForm<CheckLoginForm>({ mode: 'onBlur' }); | ||
|
||
const isEmailPattern = useCallback(() => { | ||
return { | ||
value: emailPattern, | ||
message: '올바른 이메일을 입력해주세요', | ||
}; | ||
}, []); | ||
|
||
const isPasswordPattern = useCallback(() => { | ||
return { | ||
value: passwordPattern, | ||
message: '숫자와 한글만 입력해주세요.', | ||
}; | ||
}, []); | ||
|
||
const isDisabled = !isDirty || !isValid; | ||
|
||
const isRequiredText = useCallback( | ||
(text: string) => (text === '비밀번호' ? `${text}를 입력해주세요.` : `${text}을 입력해주세요.`), | ||
[] | ||
); | ||
|
||
const { mutate: loginMutation } = useMutation(async () => { | ||
const { email, password } = getValues(); | ||
try { | ||
const data = await postLogin(email, password); | ||
if (data) { | ||
const { accessToken, refreshToken } = data; | ||
localStorage.setItem('accessToken', accessToken); | ||
localStorage.setItem('refreshToken', refreshToken); | ||
} | ||
// 로컬스토리지에 제대로 aT, rT 들어갔으면 카테고리 선택 페이지로 라우팅 | ||
} catch (error: unknown) { | ||
if (isEffError(error)) { | ||
// TODO: toast 추가 | ||
console.log(error.message, error.errorCode); | ||
} | ||
} | ||
}); | ||
|
||
return { register, isDisabled, isEmailPattern, isPasswordPattern, loginMutation, isRequiredText }; | ||
}; |
This file contains 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 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.