From d6ebee0c90f31c15ef88346d4a2b3022bbbeec96 Mon Sep 17 00:00:00 2001 From: MinCheolS Date: Thu, 2 May 2024 09:40:42 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20try,=20catch=EB=AC=B8=20=EC=A0=81?= =?UTF-8?q?=EC=9A=A9=20=EB=B0=8F=20=EA=B2=BD=EB=A1=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/api/api.ts | 27 +++++++++++++++++++++++++++ pages/signin.tsx | 45 ++++++++++++++++++--------------------------- pages/signup.tsx | 14 +++++++------- tsconfig.json | 5 ++++- 4 files changed, 56 insertions(+), 35 deletions(-) diff --git a/pages/api/api.ts b/pages/api/api.ts index e94f5eafe2..199efcc7c6 100644 --- a/pages/api/api.ts +++ b/pages/api/api.ts @@ -31,3 +31,30 @@ export const getSelectLinksInfo = async (folderId: number) => { const result = response.json(); return result; }; + +export const signInUser = async (email: string, password: string) => { + const userInfo = { + email: email, + password: password, + }; + + try { + const response = await fetch('https://bootcamp-api.codeit.kr/api/sign-in', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(userInfo), + }); + + if (response.status === 200) { + const result = await response.json(); + localStorage.setItem('accessToken', result.data.accessToken); + return true; + } else { + return false; + } + } catch (error) { + console.log(error); + } +}; diff --git a/pages/signin.tsx b/pages/signin.tsx index 3cf35f4382..346f72d525 100644 --- a/pages/signin.tsx +++ b/pages/signin.tsx @@ -1,12 +1,13 @@ import React, { useState } from 'react'; import Link from 'next/link'; -import styles from '@/styles/Signin.module.css'; -import { emailRegex, passwordRegex } from '@/components/Regex'; -import logo from '@/public/logo.svg'; -import kakao from '@/public/kakao logo.svg'; -import google from '@/public/google.svg'; -import passwordOff from '@/public/passwordOff.svg'; -import passwordOn from '@/public/passwordOn.svg'; +import styles from '@styles/Signin.module.css'; +import { emailRegex, passwordRegex } from '@components/Regex'; +import logo from '@public/logo.svg'; +import kakao from '@public/kakao logo.svg'; +import google from '@public/google.svg'; +import passwordOff from '@public/passwordOff.svg'; +import passwordOn from '@public/passwordOn.svg'; +import { signInUser } from './api/api'; export default function Signin() { const [email, setEmail] = useState(''); @@ -59,27 +60,17 @@ export default function Signin() { const handleSubmit = async (e: React.ChangeEvent) => { e.preventDefault(); - const userInfo = { - email: email, - password: password, - }; + try { + const signIn = await signInUser(email, password); - const response = await fetch('https://bootcamp-api.codeit.kr/api/sign-in', { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify(userInfo), - }); - - if (response.status === 200) { - const result = await response.json(); - localStorage.setItem('accessToken', result.data.accessToken); - window.location.assign('folder.html'); - return; - } else { - setEmailError('이메일을 확인해 주세요.'); - setPasswordError('비밀번호를 확인해 주세요.'); + if (signIn) { + window.location.assign('folder.html'); + } else { + setEmailError('이메일을 확인해 주세요.'); + setPasswordError('비밀번호를 확인해 주세요.'); + } + } catch (error) { + console.log(error); } }; diff --git a/pages/signup.tsx b/pages/signup.tsx index 4f1eb88cd9..1e34d90807 100644 --- a/pages/signup.tsx +++ b/pages/signup.tsx @@ -1,12 +1,12 @@ import React, { useState } from 'react'; import Link from 'next/link'; -import styles from '@/styles/Signup.module.css'; -import logo from '@/public/logo.svg'; -import kakao from '@/public/kakao logo.svg'; -import google from '@/public/google.svg'; -import passwordOff from '@/public/passwordOff.svg'; -import passwordOn from '@/public/passwordOn.svg'; -import { emailRegex, passwordRegex } from '@/components/Regex'; +import styles from '@styles/Signup.module.css'; +import logo from '@public/logo.svg'; +import kakao from '@public/kakao logo.svg'; +import google from '@public/google.svg'; +import passwordOff from '@public/passwordOff.svg'; +import passwordOn from '@public/passwordOn.svg'; +import { emailRegex, passwordRegex } from '@components/Regex'; export default function Signup() { const [email, setEmail] = useState(''); diff --git a/tsconfig.json b/tsconfig.json index 670224f3e9..4949acdc9c 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -14,7 +14,10 @@ "jsx": "preserve", "incremental": true, "paths": { - "@/*": ["./*"] + "@*": ["./*"], + "@components": ["./*/components"], + "@public": ["./*/public"], + "@styles": ["./*/styles"] } }, "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],