Skip to content

Commit 7b96a1b

Browse files
authored
Merge pull request #130 from imaginer-dev/9-회원가입-클릭시-회원가입을-할-수-있어야-한다
9 회원가입 클릭시 회원가입을 할 수 있어야 한다
2 parents 6bed916 + e3edbea commit 7b96a1b

File tree

4 files changed

+126
-12
lines changed

4 files changed

+126
-12
lines changed

src/apis/authApis.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,18 @@ interface SignInParams {
44
email: string;
55
password: string;
66
}
7+
8+
interface SignUpParams {
9+
name: string;
10+
nickName: string;
11+
phone: string;
12+
email: string;
13+
password: string;
14+
pwCheck: string;
15+
useTermsCheck: boolean;
16+
privacyTermsCheck: boolean;
17+
}
18+
719
export const signIn = async ({ email, password }: SignInParams) => {
820
const { data, error } = await supabase.auth.signInWithPassword({
921
email: email,
@@ -16,6 +28,22 @@ export const signIn = async ({ email, password }: SignInParams) => {
1628
return data;
1729
};
1830

31+
export const signUp = async ({ name, nickName, phone, email, password }: SignUpParams) => {
32+
const { data, error } = await supabase.auth.signUp({
33+
email: email,
34+
password: password,
35+
options: {
36+
data: {
37+
name: name,
38+
nickName: nickName,
39+
phone: phone,
40+
},
41+
},
42+
});
43+
if (error) {
44+
throw error;
45+
}
46+
1947
export const recoveryPasswd = async (email: string) => {
2048
const { data, error } = await supabase.auth.resetPasswordForEmail(email);
2149
if (error) {

src/apis/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export { signIn as signIn } from './authApis.ts';
1+
export { signIn as signIn, signUp as signUp } from './authApis.ts';

src/components/Join/JoinButton.tsx

Lines changed: 87 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,99 @@
11
import { useJoinState } from '../../stores/joinStore';
2+
import { LooseValidation, ValidateProcessor } from '../../utils/authUtils.ts';
3+
import { FormatPhone } from './FormatPhone.tsx';
4+
import { useRef, useState } from 'react';
5+
import useSignUp from '@/react-queries/useSignUp';
6+
import { isAuthError } from '@supabase/supabase-js';
7+
import { useNavigate } from 'react-router-dom';
8+
import { Loading } from '@/pages';
9+
import Dialog from '../Dialog';
10+
11+
interface DialogElement {
12+
openModal: () => void;
13+
closeModal: () => void;
14+
}
15+
16+
const messages = {
17+
NAME_ISVAILD_ERROR: '이름은 두글자 이상 입력 가능합니다.',
18+
PHONE_ISVAILD_ERROR: '휴대폰 번호를 정확히 입력해주세요.',
19+
PWCHECK_ISVAILD_ERROR: '비밀번호 확인을 진행해주세요.',
20+
EMAIL_PASSWORD_ISVAILD_ERROR: '이메일 또는 비밀번호 형식이 잘못되었습니다.',
21+
TERMS_ISVAILD_ERROR: '개인정보 수집 및 이용약관에 동의해주세요.',
22+
AUTH_ERROR: '이미 가입한 계정이 있습니다. 계정을 확인해주세요.',
23+
};
224

325
const JoinButton = () => {
426
const { nickName, name, phone, email, password, pwCheck, useTermsCheck, privacyTermsCheck } = useJoinState();
27+
const validator = new ValidateProcessor(new LooseValidation());
28+
const { mutate, isPending } = useSignUp();
29+
const navigate = useNavigate();
30+
const dialogRef = useRef<DialogElement | null>(null);
31+
const [dialogMessage, setDialogMessage] = useState('');
532

633
const onClick = () => {
7-
console.log('nickName: ', nickName);
8-
console.log('name: ', name);
9-
console.log('phone: ', phone);
10-
console.log('email: ', email);
11-
console.log('password: ', password);
12-
console.log('pwCheck: ', pwCheck);
13-
console.log('useTermsCheck: ', useTermsCheck);
14-
console.log('privacyTermsCheck: ', privacyTermsCheck);
34+
if (!validator.isValidEmail(email) || !validator.isValidPassword(password)) {
35+
setDialogMessage(messages.EMAIL_PASSWORD_ISVAILD_ERROR);
36+
dialogRef.current?.openModal();
37+
return;
38+
}
39+
40+
if (!validator.isValidName(name)) {
41+
setDialogMessage(messages.NAME_ISVAILD_ERROR);
42+
dialogRef.current?.openModal();
43+
return;
44+
}
45+
46+
if (!FormatPhone(phone)) {
47+
setDialogMessage(messages.PHONE_ISVAILD_ERROR);
48+
dialogRef.current?.openModal();
49+
return;
50+
}
51+
52+
if (!validator.isValidPwCheck(password, pwCheck)) {
53+
setDialogMessage(messages.PWCHECK_ISVAILD_ERROR);
54+
dialogRef.current?.openModal();
55+
return;
56+
}
57+
58+
if (!useTermsCheck || !privacyTermsCheck) {
59+
setDialogMessage(messages.TERMS_ISVAILD_ERROR);
60+
dialogRef.current?.openModal();
61+
return;
62+
}
63+
64+
mutate(
65+
{
66+
name,
67+
nickName,
68+
phone,
69+
email,
70+
password,
71+
pwCheck: '',
72+
useTermsCheck: false,
73+
privacyTermsCheck: false,
74+
},
75+
{
76+
onError: (error) => {
77+
if (isAuthError(error)) {
78+
setDialogMessage(messages.AUTH_ERROR);
79+
dialogRef.current?.openModal();
80+
}
81+
},
82+
onSuccess: () => {
83+
navigate('/login');
84+
},
85+
},
86+
);
1587
};
1688

1789
return (
18-
<button type={'submit'} onClick={onClick} className="btn btn-outline btn-primary w-full">
19-
회원가입 하기
20-
</button>
90+
<>
91+
<button type={'submit'} onClick={onClick} className="btn btn-outline btn-primary w-full">
92+
회원가입 하기
93+
</button>
94+
<Dialog ref={dialogRef} desc={dialogMessage}></Dialog>
95+
{isPending && <Loading size={'lg'} color={'primary'} display={'spinner'} />}
96+
</>
2197
);
2298
};
2399

src/react-queries/useSignUp.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { useMutation } from '@tanstack/react-query';
2+
import { signUp } from '@/apis';
3+
4+
const useSignUp = () =>
5+
useMutation({
6+
mutationKey: ['signUp'],
7+
mutationFn: signUp,
8+
});
9+
10+
export default useSignUp;

0 commit comments

Comments
 (0)