Skip to content

Commit bd01a01

Browse files
authored
Feat: 회원가입 API 연결
Feat: 회원가입 API 연결
2 parents 28be0f6 + 9a36866 commit bd01a01

File tree

6 files changed

+170
-103
lines changed

6 files changed

+170
-103
lines changed

components/Layout/AuthLayout.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { ReactNode } from "react";
55
const AuthLayout = ({ children }: { children: ReactNode }) => {
66
const router = useRouter();
77
return (
8-
<div className="mx-auto bg-gray100 flex flex-col items-center justify-center h-screen py-32">
8+
<div className="mx-auto flex flex-col items-center justify-center h-sc py-16">
99
<div>
1010
<Image
1111
className="cursor-pointer"

hooks/useForm.tsx

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { useState, ChangeEvent, FormEvent } from "react";
22
import { useRouter } from "next/router";
33
import { postSignIn, postSignUp } from "@/lib/api/auth";
4+
import { TbWashDryP } from "react-icons/tb";
45

56
interface FormValues {
67
email: string;
@@ -16,7 +17,7 @@ const INITIAL_VALUES: FormValues = {
1617
passwordConfirm: "",
1718
};
1819

19-
export function useForm(isSignUp = false) {
20+
const useForm = (isSignUp = false) => {
2021
const [values, setValues] = useState<FormValues>(INITIAL_VALUES);
2122
const [errors, setErrors] = useState<FormValues>(INITIAL_VALUES);
2223
const router = useRouter();
@@ -52,7 +53,7 @@ export function useForm(isSignUp = false) {
5253
if (!value) {
5354
setErrors((prev) => ({
5455
...prev,
55-
nickname: "닉네임을 입력해주세요.",
56+
nickname: "이름을 입력해주세요.",
5657
}));
5758
}
5859
} else if (name === "password") {
@@ -77,17 +78,26 @@ export function useForm(isSignUp = false) {
7778
}
7879
};
7980

80-
const handleSubmit = (e: FormEvent) => {
81+
const handleSubmit = async (e: FormEvent) => {
8182
e.preventDefault();
8283
if (isFormInvalid()) return;
8384
const { email, password, nickname } = values;
8485

8586
if (isSignUp) {
86-
postSignUp({ email, password, name: nickname || "" });
87+
const data = await postSignUp({ email, password, name: nickname || "" });
88+
89+
if (data) {
90+
router.push("/login");
91+
} else {
92+
alert("회원가입 실패: 이메일 또는 비밀번호를 확인해주세요.");
93+
}
8794
} else {
88-
const data: any = postSignIn({ email, password });
95+
const data = await postSignIn({ email, password });
96+
8997
if (data) {
90-
router.push("/"); // 로그인 성공 후 대시보드로 리디렉션
98+
router.push("/");
99+
} else {
100+
alert("로그인 실패: 이메일 또는 비밀번호를 확인해주세요.");
91101
}
92102
}
93103

@@ -118,4 +128,6 @@ export function useForm(isSignUp = false) {
118128
handleSubmit,
119129
isFormInvalid,
120130
};
121-
}
131+
};
132+
133+
export default useForm;

lib/api/auth.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ interface easySignUpProps extends easySignInProps {
2121
// 회원가입
2222
export const postSignUp = async (body: signUpProps) => {
2323
try {
24-
const res = await axiosInstance.post("/auth/sign-up", body);
24+
const res = await proxy.post("api/auth/sign-up", body);
2525
if (res.status >= 200 && res.status < 300) return res.data;
2626
} catch (err) {
2727
console.error("에러 메시지: ", err instanceof Error ? err.message : err);

pages/api/auth/sign-up.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import axiosInstance from "@/lib/api/axiosInstanceApi";
2+
import { NextApiRequest, NextApiResponse } from "next";
3+
4+
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
5+
if (req.method === "POST") {
6+
try {
7+
const { email, password, name } = req.body;
8+
9+
await axiosInstance.post("/auth/sign-up", {
10+
email,
11+
password,
12+
name,
13+
});
14+
return res.status(200).json({ message: "회원가입 성공" });
15+
} catch (error) {
16+
return res.status(400).json({ message: "서버 오류" });
17+
}
18+
} else {
19+
res.status(405).json({ message: "허용되지 않은 접근 방법" });
20+
}
21+
};
22+
23+
export default handler;

pages/login/index.tsx

Lines changed: 59 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,71 @@
1-
import { useForm } from "@/hooks/useForm";
1+
import useForm from "@/hooks/useForm";
22
import AuthInput from "@/components/Auth/AuthInput";
33
import SnsLogin from "@/components/Auth/SnsLogin";
44
import SubmitButton from "@/components/SubMitButton";
55
import AuthLayout from "@/components/Layout/AuthLayout";
66
import Link from "next/link";
77

8-
const Login = () => {
9-
const { values, errors, handleChange, handleBlur, handleSubmit } =
10-
useForm(false);
8+
const LoginPage = () => {
9+
const {
10+
values,
11+
errors,
12+
handleChange,
13+
handleBlur,
14+
handleSubmit,
15+
isFormInvalid,
16+
} = useForm(false);
1117

1218
return (
13-
<AuthLayout>
14-
<p className="mt-[16px] text-base font-normal">
15-
회원이 아니신가요?{" "}
16-
<Link
17-
href="/signup"
18-
className="cursor-pointer text-purple100 underline font-semibold"
19+
<div className="bg-gray100 min-h-screen">
20+
<AuthLayout>
21+
<p className="mt-[16px] text-base font-normal">
22+
회원이 아니신가요?{" "}
23+
<Link
24+
href="/signup"
25+
className="cursor-pointer text-purple100 underline font-semibold"
26+
>
27+
회원 가입하기
28+
</Link>
29+
</p>
30+
<form
31+
className="w-full sm:max-w-[325px] md:max-w-[400px] lg:max-w-[400px] mt-[30px]"
32+
aria-labelledby="login-form"
33+
onSubmit={handleSubmit}
1934
>
20-
회원 가입하기
21-
</Link>
22-
</p>
23-
<form
24-
className="w-full sm:max-w-[325px] md:max-w-[400px] lg:max-w-[400px] mt-[30px]"
25-
aria-labelledby="login-form"
26-
onSubmit={handleSubmit}
27-
>
28-
<AuthInput
29-
text="이메일"
30-
type="text"
31-
name="email"
32-
placeholder="이메일을 입력해주세요."
33-
value={values.email}
34-
onChange={handleChange}
35-
onBlur={handleBlur}
36-
error={errors.email}
37-
/>
38-
<AuthInput
39-
text="비밀번호"
40-
type="password"
41-
name="password"
42-
placeholder="비밀번호를 입력해주세요."
43-
value={values.password}
44-
onChange={handleChange}
45-
onBlur={handleBlur}
46-
error={errors.password}
47-
/>
48-
<SubmitButton
49-
type="submit"
50-
width="w-full"
51-
height="h-[53px]"
52-
className="mt-[30px]"
53-
>
54-
로그인
55-
</SubmitButton>
56-
<SnsLogin />
57-
</form>
58-
</AuthLayout>
35+
<AuthInput
36+
text="이메일"
37+
type="text"
38+
name="email"
39+
placeholder="이메일을 입력해주세요."
40+
value={values.email}
41+
onChange={handleChange}
42+
onBlur={handleBlur}
43+
error={errors.email}
44+
/>
45+
<AuthInput
46+
text="비밀번호"
47+
type="password"
48+
name="password"
49+
placeholder="비밀번호를 입력해주세요."
50+
value={values.password}
51+
onChange={handleChange}
52+
onBlur={handleBlur}
53+
error={errors.password}
54+
/>
55+
<SubmitButton
56+
type="submit"
57+
width="w-full"
58+
height="h-[53px]"
59+
className="mt-[30px]"
60+
disabled={isFormInvalid()}
61+
>
62+
로그인
63+
</SubmitButton>
64+
<SnsLogin />
65+
</form>
66+
</AuthLayout>
67+
</div>
5968
);
6069
};
6170

62-
export default Login;
71+
export default LoginPage;

pages/signup/index.tsx

Lines changed: 67 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,53 +2,76 @@ import AuthInput from "@/components/Auth/AuthInput";
22
import SubmitButton from "@/components/SubMitButton";
33
import AuthLayout from "@/components/Layout/AuthLayout";
44
import Link from "next/link";
5+
import useForm from "@/hooks/useForm";
6+
7+
const SignupPage = () => {
8+
const { values, errors, handleChange, handleBlur, handleSubmit } =
9+
useForm(true);
510

6-
const signup = () => {
711
return (
8-
<AuthLayout>
9-
<p className="mt-[16px] text-base font-normal">
10-
이미 회원이신가요?{" "}
11-
<Link
12-
href="/login"
13-
className="cursor-pointer text-purple100 underline font-semibold"
12+
<div className="bg-gray100 min-h-screen">
13+
<AuthLayout>
14+
<p className="mt-[16px] text-base font-normal">
15+
이미 회원이신가요?{" "}
16+
<Link
17+
href="/login"
18+
className="cursor-pointer text-purple100 underline font-semibold"
19+
>
20+
로그인하기
21+
</Link>
22+
</p>
23+
<form
24+
className="w-full sm:max-w-[325px] md:max-w-[400px] lg:max-w-[400px] mt-[30px] h-full"
25+
aria-labelledby="login-form"
26+
onSubmit={handleSubmit}
1427
>
15-
로그인하기
16-
</Link>
17-
</p>
18-
<form
19-
className="w-full sm:max-w-[325px] md:max-w-[400px] lg:max-w-[400px] mt-[30px]"
20-
aria-labelledby="login-form"
21-
>
22-
<AuthInput
23-
text="이메일"
24-
type="text"
25-
name="email"
26-
placeholder="이메일을 입력해주세요."
27-
/>
28-
<AuthInput
29-
text="이름"
30-
type="text"
31-
name="nickname"
32-
placeholder="이름을 입력해주세요."
33-
/>
34-
<AuthInput
35-
text="비밀번호"
36-
type="password"
37-
name="password"
38-
placeholder="비밀번호를 입력해주세요."
39-
/>
40-
<AuthInput
41-
text="비밀번호 확인"
42-
type="password"
43-
name="passwordConfirm"
44-
placeholder="비밀번호를 다시 입력해주세요."
45-
/>
46-
<SubmitButton width="w-full" height="h-[53px]" className="mt-[30px]">
47-
회원가입
48-
</SubmitButton>
49-
</form>
50-
</AuthLayout>
28+
<AuthInput
29+
text="이메일"
30+
type="text"
31+
name="email"
32+
placeholder="이메일을 입력해주세요."
33+
value={values.email}
34+
onChange={handleChange}
35+
onBlur={handleBlur}
36+
error={errors.email}
37+
/>
38+
<AuthInput
39+
text="이름"
40+
type="text"
41+
name="nickname"
42+
placeholder="이름을 입력해주세요."
43+
value={values.nickname}
44+
onChange={handleChange}
45+
onBlur={handleBlur}
46+
error={errors.nickname}
47+
/>
48+
<AuthInput
49+
text="비밀번호"
50+
type="password"
51+
name="password"
52+
placeholder="비밀번호를 입력해주세요."
53+
value={values.password}
54+
onChange={handleChange}
55+
onBlur={handleBlur}
56+
error={errors.password}
57+
/>
58+
<AuthInput
59+
text="비밀번호"
60+
type="password"
61+
name="passwordConfirm"
62+
placeholder="비밀번호를 입력해주세요."
63+
value={values.passwordConfirm}
64+
onChange={handleChange}
65+
onBlur={handleBlur}
66+
error={errors.passwordConfirm}
67+
/>
68+
<SubmitButton width="w-full" height="h-[53px]" className="mt-[30px]">
69+
회원가입
70+
</SubmitButton>
71+
</form>
72+
</AuthLayout>
73+
</div>
5174
);
5275
};
5376

54-
export default signup;
77+
export default SignupPage;

0 commit comments

Comments
 (0)