Skip to content

Commit

Permalink
Refactor: 로그인, 회원가입 api 요청 리팩토링
Browse files Browse the repository at this point in the history
- page에서 api 보내기 전에 비밀번호 유효성 검사를 하지 않고 넥스트 서버 측에서 api 콜 하기 전에 비밀번호 일치 여부를 확인하도록 했습니다
- axios에서 중복된 header application/json을 인스턴스 정의할 때 관리하도록 했습니다.
  • Loading branch information
soulchicken committed Oct 2, 2023
1 parent 1d362f9 commit 50af011
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 22 deletions.
13 changes: 2 additions & 11 deletions src/pages/signup/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,18 @@ const Signup = () => {

const handleSubmit = async (e: FormEvent<HTMLFormElement>) => {
e.preventDefault();
console.log(password, confirmPassword);

if (password !== confirmPassword) {
// TODO: 비밀번호 일치 여부 확인 후 실패시 알려주는 UI가 필요함
alert('비밀번호를 다시 확인해주세요!');
return;
}
const signupData = await credentialsSignupAPI({
email, name: username, password, provider: 'credential',
email, username, password, confirmPassword,
});

console.log(signupData);

if ('error' in signupData) {
// TODO: 회원가입 실패시 알려주는 UI가 필요함
alert('회원가입 실패');
return;
}
// 회원가입 성공!
router.push({
pathname: '/user/login',
pathname: '/login',
});
};

Expand Down
26 changes: 15 additions & 11 deletions src/utils/api/clientServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,32 @@ interface Credentials {
}

interface SignupForm {
email: string, name: string, password: string, provider: string
email: string, username: string, password: string, confirmPassword: string
}

interface Socials {
email: string, provider: string,
}

export const credentialsLoginAPI = async (credentials : Credentials) => {
const result = await clientServerInstance.post('members/login', JSON.stringify(credentials), {
headers: {
'Content-Type': 'application/json',
},
});
const result = await clientServerInstance.post('members/login', JSON.stringify(credentials));
return result.data;
};

export const credentialsSignupAPI = async (signupForm : SignupForm) => {
const result = await clientServerInstance.post('members/signup', JSON.stringify(signupForm), {
headers: {
'Content-Type': 'application/json',
},
});
if (signupForm.password !== signupForm.confirmPassword) {
// TODO: 비밀번호 일치 여부 확인 후 실패시 알려주는 UI가 필요함
return { error: '비밀번호를 다시 확인해주세요.' };
}

const sendSignupData = {
email: signupForm.email,
name: signupForm.username,
password: signupForm.password,
provider: 'Credential',
};

const result = await clientServerInstance.post('members/signup', JSON.stringify(sendSignupData));
return result.data;
};

Expand Down
1 change: 1 addition & 0 deletions src/utils/axiosInstance/defaultInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import axios, { AxiosError } from 'axios';

const defaultInstance = axios.create({
baseURL: process.env.NEXT_PUBLIC_SERVER_URL || 'http://localhost:8080/',
headers: { 'Content-Type': 'application/json' },
});

defaultInstance.interceptors.response.use((response) => response, (error: AxiosError) => {
Expand Down

0 comments on commit 50af011

Please sign in to comment.