From 1996a70506ef201360aa7ad692723ff84069801a Mon Sep 17 00:00:00 2001 From: ylem76 Date: Sat, 17 Aug 2024 14:46:47 +0900 Subject: [PATCH] =?UTF-8?q?[#206]=20login=20action=20=EC=97=90=EB=9F=AC=20?= =?UTF-8?q?=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app/login/components/LoginForm.tsx | 26 +++++-------------- src/app/login/loginAction.tsx | 35 +++++++++++++++++++++++--- 2 files changed, 37 insertions(+), 24 deletions(-) diff --git a/src/app/login/components/LoginForm.tsx b/src/app/login/components/LoginForm.tsx index 5ad0a8a..696d77f 100644 --- a/src/app/login/components/LoginForm.tsx +++ b/src/app/login/components/LoginForm.tsx @@ -40,26 +40,12 @@ export default function LoginForm() { const onSubmit = async (data: LoginFormValue) => { // 로그인 액션 실행 - console.log(data) - loginAction(data) - // try { - // const response = await api.post('auth/login', data) - // const { accessToken, user } = response.data - // sessionStorage.setItem('accessToken', accessToken) - // sessionStorage.setItem('user', JSON.stringify(user)) - // const dashboards = await getDashboardList() - // setDashboards(dashboards) - // router.push('/mydashboard') - // } catch (error) { - // let loginErrorMessage = '' - // if (axios.isAxiosError(error)) { - // loginErrorMessage = error.response?.data.message - // } else { - // loginErrorMessage = - // '서버에 문제가 있는거 같아요. 잠시 후에 다시 시도해보시겠어요?' - // } - // openModal() - // } + const errMsg = await loginAction(data) + + // 에러 메시지 팝업 + if (errMsg) { + openModal() + } } const isDisabled = !!(errors.email || errors.password || isLoading) diff --git a/src/app/login/loginAction.tsx b/src/app/login/loginAction.tsx index 7d7d208..0976c66 100644 --- a/src/app/login/loginAction.tsx +++ b/src/app/login/loginAction.tsx @@ -8,10 +8,37 @@ import api from '@/lib/axiosInstance' import { LoginFormValue } from './components/LoginForm' export default async function loginAction(data: LoginFormValue) { - // 폼에서 데이터 가져오기 = data // 백엔드 요청 - // 가져온 json token 쿠키 설정 하기 - // 로그인 여부에 따라 redirect + try { + const response = await fetch( + 'https://sp-taskify-api.vercel.app/7-2/auth/login', + { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(data), + } + ) + + const responsedData = await response.json() + if (responsedData.message) { + throw new Error(responsedData.message) + } + const { accessToken, user } = responsedData - return { message: 'test' } + // 가져온 json token 쿠키 설정 하기 + cookies().set('Authorization', accessToken, { + secure: true, + httpOnly: true, + expires: Date.now() + 24 * 60 * 60 * 1000 * 3, + path: '/', + sameSite: 'strict', + }) + } catch (error: any) { + return error.message + } + + // 로그인 여부에 따라 redirect + redirect('/mydashboard') }