Skip to content

Commit

Permalink
Feat: signin, singup페이지 이전 미션 미개발 부분 진행
Browse files Browse the repository at this point in the history
  • Loading branch information
김보민 authored and 김보민 committed Jun 29, 2024
1 parent d7c5cb9 commit fcecbef
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 36 deletions.
25 changes: 25 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
},
"dependencies": {
"@hookform/error-message": "^2.0.1",
"@tanstack/react-query": "^5.49.0",
"axios": "^1.7.2",
"classnames": "^2.5.1",
"next": "^13.5.6",
Expand Down
8 changes: 5 additions & 3 deletions pages/signup.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import Link from "next/link";
import styles from "../src/styles/sign.module.scss";
import classNames from "classnames/bind";
import { SocialLogin, SignUpForm } from "../src/components";
import { useEffect } from "react";
import { checkAccessToken } from "../src/utils";

const cx = classNames.bind(styles);

Expand All @@ -14,12 +16,12 @@ function signUpPage() {
<div className={cx("page-container")}>
<div className={cx("contents")}>
<div className={cx("title")}>
<Link href="/">
<img className={cx("logo")} src="/images/logo.svg" alt="로고" />
<Link href='/'>
<img className={cx("logo")} src='/images/logo.svg' alt='로고' />
</Link>
<h2>
이미 회원이신가요?
<Link className={cx("signIn-link")} href="./signin">
<Link className={cx("signIn-link")} href='./signin'>
로그인하기
</Link>
</h2>
Expand Down
25 changes: 16 additions & 9 deletions src/components/SignInForm/SignInForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ import styles from "./SingInForm.module.scss";
import classNames from "classnames/bind";
import { useForm } from "react-hook-form";
import { InputBox } from "../InputBox";
import { postIdPwd, regexData, ApiUrl, checkAccessToken } from "../../utils";
import {
postIdPwd,
regexData,
ApiUrl,
checkAccessToken,
postSignin,
} from "../../utils";

const cx = classNames.bind(styles);

Expand Down Expand Up @@ -30,26 +36,27 @@ export function SignInForm() {
});

const onSubmit = (data) => {
postIdPwd(ApiUrl.signIn, data, setError, "signInToken");
console.log(data);
postSignin(data, setError);
};

return (
<form className={cx("form-wrapper")} onSubmit={handleSubmit(onSubmit)}>
<InputBox
label="이메일"
name="email"
placeholder="이메일을 입력해 주세요"
label='이메일'
name='email'
placeholder='이메일을 입력해 주세요'
errors={errors}
register={{ ...register("email", ValidData.email) }}
/>
<InputBox
label="비밀번호"
name="password"
placeholder="비밀번호를 입력해 주세요"
label='비밀번호'
name='password'
placeholder='비밀번호를 입력해 주세요'
errors={errors}
register={{ ...register("password", ValidData.pwd) }}
/>
<button type="submit" className={cx("submit-button")}>
<button type='submit' className={cx("submit-button")}>
로그인
</button>
</form>
Expand Down
29 changes: 16 additions & 13 deletions src/components/SignUpForm/SignUpForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import styles from "./SignUpForm.module.scss";
import classNames from "classnames/bind";
import { useForm } from "react-hook-form";
import { InputBox } from "../InputBox";
import { checkDuplicateEmail, regexData } from "../../utils";
import { checkDuplicateEmail, postSignUp, regexData } from "../../utils";

const cx = classNames.bind(styles);

Expand Down Expand Up @@ -38,34 +38,37 @@ export function SignUpForm() {
mode: "onBlur",
});

const onSubmit = () => {
location.href = "folder";
const onSubmit = (data) => {
const { email, password } = data;
const selectedData = { email, password };
console.log(selectedData);
postSignUp(selectedData);
};

return (
<form className={cx("form-wrapper")} onSubmit={handleSubmit(onSubmit)}>
<InputBox
label="이메일"
name="email"
placeholder="이메일을 입력해 주세요"
label='이메일'
name='email'
placeholder='이메일을 입력해 주세요'
errors={errors}
register={{ ...register("email", ValidData.email) }}
/>
<InputBox
label="비밀번호"
name="password"
placeholder="영문, 숫자를 조합해 8자 이상 입력해 주세요."
label='비밀번호'
name='password'
placeholder='영문, 숫자를 조합해 8자 이상 입력해 주세요.'
errors={errors}
register={{ ...register("password", ValidData.pwd) }}
/>
<InputBox
label="비밀번호 확인"
name="passwordCheck"
placeholder="비밀번호와 일치하는 값을 입력해주세요"
label='비밀번호 확인'
name='passwordCheck'
placeholder='비밀번호와 일치하는 값을 입력해주세요'
errors={errors}
register={{ ...register("passwordCheck", ValidData.pwdCheck) }}
/>
<button type="submit" className={cx("submit-button")}>
<button type='submit' className={cx("submit-button")}>
회원가입
</button>
</form>
Expand Down
34 changes: 27 additions & 7 deletions src/utils/api.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,28 @@
import { ApiUrl } from "./url";
import axiosInstance from "./axios";

export async function postIdPwd(url, inputData, setError, tokenName) {
export async function postSignUp(inputData) {
try {
const res = await fetch(url, {
const res = await fetch(ApiUrl.signUp, {
method: "POST",
headers: {
"Content-type": "application/json",
},
body: JSON.stringify(inputData),
});

if (!res.ok) {
throw new Error("bad request");
}
await postSignin(inputData);
} catch (error) {
console.log(error);
}
}

export async function postSignin(inputData, setError = "") {
try {
const res = await fetch(ApiUrl.signIn, {
method: "POST",
headers: {
"Content-type": "application/json",
Expand All @@ -16,10 +35,12 @@ export async function postIdPwd(url, inputData, setError, tokenName) {
}

const result = await res.json();
const accessToken = result.data.accessToken;
saveAccessTokenToLocalStorage(accessToken, tokenName);
console.log(result.accessToken);
const accessToken = result.accessToken;
saveAccessTokenToLocalStorage(accessToken, "signInToken");
location.href = "folder";
} catch {
} catch (error) {
console.error("SignIn Error:", error);
setError("password", {
type: "server",
message: "비밀번호를 확인해 주세요",
Expand Down Expand Up @@ -48,8 +69,7 @@ export async function checkDuplicateEmail(value) {
if (!res.ok) {
throw new Error("bad request");
}

return (result = await res.json());
return true;
} catch {
return false;
}
Expand Down
8 changes: 4 additions & 4 deletions src/utils/url.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
export const baseUrl = "https://bootcamp-api.codeit.kr/api/";
export const baseUrl = "https://bootcamp-api.codeit.kr/api/linkbrary/v1/";

export const ApiUrl = {
sampleUser: `${baseUrl}sample/user`,
usersFolders: `${baseUrl}users/1/folders`,
usersLinks: `${baseUrl}users/1/links`,
signIn: `${baseUrl}sign-in`,
signUp: `${baseUrl}sign-up`,
checkEmail: `${baseUrl}check-email`,
signIn: `${baseUrl}auth/sign-in`,
signUp: `${baseUrl}auth/sign-up`,
checkEmail: `${baseUrl}users/check-email`,
};

0 comments on commit fcecbef

Please sign in to comment.