Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion components/Layout/AuthLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ReactNode } from "react";
const AuthLayout = ({ children }: { children: ReactNode }) => {
const router = useRouter();
return (
<div className="mx-auto bg-gray100 flex flex-col items-center justify-center h-screen py-32">
<div className="mx-auto flex flex-col items-center justify-center h-sc py-16">
<div>
<Image
className="cursor-pointer"
Expand Down
26 changes: 19 additions & 7 deletions hooks/useForm.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useState, ChangeEvent, FormEvent } from "react";
import { useRouter } from "next/router";
import { postSignIn, postSignUp } from "@/lib/api/auth";
import { TbWashDryP } from "react-icons/tb";

interface FormValues {
email: string;
Expand All @@ -16,7 +17,7 @@ const INITIAL_VALUES: FormValues = {
passwordConfirm: "",
};

export function useForm(isSignUp = false) {
const useForm = (isSignUp = false) => {
const [values, setValues] = useState<FormValues>(INITIAL_VALUES);
const [errors, setErrors] = useState<FormValues>(INITIAL_VALUES);
const router = useRouter();
Expand Down Expand Up @@ -52,7 +53,7 @@ export function useForm(isSignUp = false) {
if (!value) {
setErrors((prev) => ({
...prev,
nickname: "닉네임을 입력해주세요.",
nickname: "이름을 입력해주세요.",
}));
}
} else if (name === "password") {
Expand All @@ -77,17 +78,26 @@ export function useForm(isSignUp = false) {
}
};

const handleSubmit = (e: FormEvent) => {
const handleSubmit = async (e: FormEvent) => {
e.preventDefault();
if (isFormInvalid()) return;
const { email, password, nickname } = values;

if (isSignUp) {
postSignUp({ email, password, name: nickname || "" });
const data = await postSignUp({ email, password, name: nickname || "" });

if (data) {
router.push("/login");
} else {
alert("회원가입 실패: 이메일 또는 비밀번호를 확인해주세요.");
}
} else {
const data: any = postSignIn({ email, password });
const data = await postSignIn({ email, password });

if (data) {
router.push("/"); // 로그인 성공 후 대시보드로 리디렉션
router.push("/");
} else {
alert("로그인 실패: 이메일 또는 비밀번호를 확인해주세요.");
}
}

Expand Down Expand Up @@ -118,4 +128,6 @@ export function useForm(isSignUp = false) {
handleSubmit,
isFormInvalid,
};
}
};

export default useForm;
2 changes: 1 addition & 1 deletion lib/api/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ interface easySignUpProps extends easySignInProps {
// 회원가입
export const postSignUp = async (body: signUpProps) => {
try {
const res = await axiosInstance.post("/auth/sign-up", body);
const res = await proxy.post("api/auth/sign-up", body);
if (res.status >= 200 && res.status < 300) return res.data;
} catch (err) {
console.error("에러 메시지: ", err instanceof Error ? err.message : err);
Expand Down
23 changes: 23 additions & 0 deletions pages/api/auth/sign-up.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import axiosInstance from "@/lib/api/axiosInstanceApi";
import { NextApiRequest, NextApiResponse } from "next";

const handler = async (req: NextApiRequest, res: NextApiResponse) => {
if (req.method === "POST") {
try {
const { email, password, name } = req.body;

await axiosInstance.post("/auth/sign-up", {
email,
password,
name,
});
return res.status(200).json({ message: "회원가입 성공" });
} catch (error) {
return res.status(400).json({ message: "서버 오류" });
}
} else {
res.status(405).json({ message: "허용되지 않은 접근 방법" });
}
};

export default handler;
105 changes: 57 additions & 48 deletions pages/login/index.tsx
Original file line number Diff line number Diff line change
@@ -1,61 +1,70 @@
import { useForm } from "@/hooks/useForm";
import useForm from "@/hooks/useForm";
import AuthInput from "@/components/Auth/AuthInput";
import SnsLogin from "@/components/Auth/SnsLogin";
import SubmitButton from "@/components/SubMitButton";
import AuthLayout from "@/components/Layout/AuthLayout";
import Link from "next/link";

const Login = () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

페이지 컴포넌트는 접미사에 Page 붙히기로 얘기했던 것 같아요!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

넵! 반영할게여

const { values, errors, handleChange, handleBlur, handleSubmit } =
useForm(false);
const {
values,
errors,
handleChange,
handleBlur,
handleSubmit,
isFormInvalid,
} = useForm(false);

return (
<AuthLayout>
<p className="mt-[16px] text-base font-normal">
회원이 아니신가요?{" "}
<Link
href="/signup"
className="cursor-pointer text-purple100 underline font-semibold"
<div className="bg-gray100 min-h-screen">
<AuthLayout>
<p className="mt-[16px] text-base font-normal">
회원이 아니신가요?{" "}
<Link
href="/signup"
className="cursor-pointer text-purple100 underline font-semibold"
>
회원 가입하기
</Link>
</p>
<form
className="w-full sm:max-w-[325px] md:max-w-[400px] lg:max-w-[400px] mt-[30px]"
aria-labelledby="login-form"
onSubmit={handleSubmit}
>
회원 가입하기
</Link>
</p>
<form
className="w-full sm:max-w-[325px] md:max-w-[400px] lg:max-w-[400px] mt-[30px]"
aria-labelledby="login-form"
onSubmit={handleSubmit}
>
<AuthInput
text="이메일"
type="text"
name="email"
placeholder="이메일을 입력해주세요."
value={values.email}
onChange={handleChange}
onBlur={handleBlur}
error={errors.email}
/>
<AuthInput
text="비밀번호"
type="password"
name="password"
placeholder="비밀번호를 입력해주세요."
value={values.password}
onChange={handleChange}
onBlur={handleBlur}
error={errors.password}
/>
<SubmitButton
type="submit"
width="w-full"
height="h-[53px]"
className="mt-[30px]"
>
로그인
</SubmitButton>
<SnsLogin />
</form>
</AuthLayout>
<AuthInput
text="이메일"
type="text"
name="email"
placeholder="이메일을 입력해주세요."
value={values.email}
onChange={handleChange}
onBlur={handleBlur}
error={errors.email}
/>
<AuthInput
text="비밀번호"
type="password"
name="password"
placeholder="비밀번호를 입력해주세요."
value={values.password}
onChange={handleChange}
onBlur={handleBlur}
error={errors.password}
/>
<SubmitButton
type="submit"
width="w-full"
height="h-[53px]"
className="mt-[30px]"
disabled={isFormInvalid()}
>
로그인
</SubmitButton>
<SnsLogin />
</form>
</AuthLayout>
</div>
);
};

Expand Down
111 changes: 67 additions & 44 deletions pages/signup/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,76 @@ import AuthInput from "@/components/Auth/AuthInput";
import SubmitButton from "@/components/SubMitButton";
import AuthLayout from "@/components/Layout/AuthLayout";
import Link from "next/link";
import useForm from "@/hooks/useForm";

const Signup = () => {
const { values, errors, handleChange, handleBlur, handleSubmit } =
useForm(true);

const signup = () => {
return (
<AuthLayout>
<p className="mt-[16px] text-base font-normal">
이미 회원이신가요?{" "}
<Link
href="/login"
className="cursor-pointer text-purple100 underline font-semibold"
<div className="bg-gray100 min-h-screen">
<AuthLayout>
<p className="mt-[16px] text-base font-normal">
이미 회원이신가요?{" "}
<Link
href="/login"
className="cursor-pointer text-purple100 underline font-semibold"
>
로그인하기
</Link>
</p>
<form
className="w-full sm:max-w-[325px] md:max-w-[400px] lg:max-w-[400px] mt-[30px] h-full"
aria-labelledby="login-form"
onSubmit={handleSubmit}
>
로그인하기
</Link>
</p>
<form
className="w-full sm:max-w-[325px] md:max-w-[400px] lg:max-w-[400px] mt-[30px]"
aria-labelledby="login-form"
>
<AuthInput
text="이메일"
type="text"
name="email"
placeholder="이메일을 입력해주세요."
/>
<AuthInput
text="이름"
type="text"
name="nickname"
placeholder="이름을 입력해주세요."
/>
<AuthInput
text="비밀번호"
type="password"
name="password"
placeholder="비밀번호를 입력해주세요."
/>
<AuthInput
text="비밀번호 확인"
type="password"
name="passwordConfirm"
placeholder="비밀번호를 다시 입력해주세요."
/>
<SubmitButton width="w-full" height="h-[53px]" className="mt-[30px]">
회원가입
</SubmitButton>
</form>
</AuthLayout>
<AuthInput
text="이메일"
type="text"
name="email"
placeholder="이메일을 입력해주세요."
value={values.email}
onChange={handleChange}
onBlur={handleBlur}
error={errors.email}
/>
<AuthInput
text="이름"
type="text"
name="nickname"
placeholder="이름을 입력해주세요."
value={values.nickname}
onChange={handleChange}
onBlur={handleBlur}
error={errors.nickname}
/>
<AuthInput
text="비밀번호"
type="password"
name="password"
placeholder="비밀번호를 입력해주세요."
value={values.password}
onChange={handleChange}
onBlur={handleBlur}
error={errors.password}
/>
<AuthInput
text="비밀번호"
type="password"
name="passwordConfirm"
placeholder="비밀번호를 입력해주세요."
value={values.passwordConfirm}
onChange={handleChange}
onBlur={handleBlur}
error={errors.passwordConfirm}
/>
<SubmitButton width="w-full" height="h-[53px]" className="mt-[30px]">
회원가입
</SubmitButton>
</form>
</AuthLayout>
</div>
);
};

export default signup;
export default Signup;
Loading