From d7807383ac17cc734f79eeecfd3e0a55dd343fa0 Mon Sep 17 00:00:00 2001 From: ylem76 Date: Sat, 17 Aug 2024 13:56:44 +0900 Subject: [PATCH] [#206] add server actions --- src/app/login/components/LoginForm.tsx | 42 +++++++++++++++----------- src/app/login/loginAction.tsx | 17 +++++++++++ 2 files changed, 41 insertions(+), 18 deletions(-) create mode 100644 src/app/login/loginAction.tsx diff --git a/src/app/login/components/LoginForm.tsx b/src/app/login/components/LoginForm.tsx index 21fcf01..5ad0a8a 100644 --- a/src/app/login/components/LoginForm.tsx +++ b/src/app/login/components/LoginForm.tsx @@ -13,6 +13,8 @@ import { getDashboardList } from '@/lib/dashboardsApi' import useDashboardStore from '@/store/useDashboardStore' import useModalStore from '@/store/useModalStore' +import loginAction from '../loginAction' + export interface LoginFormValue { email: string password: string @@ -29,6 +31,7 @@ export default function LoginForm() { } = useForm() const router = useRouter() + const { openModal } = useModalStore() const { setDashboards } = useDashboardStore() @@ -36,24 +39,27 @@ export default function LoginForm() { const passwordType = pwdVisible ? 'text' : 'password' const onSubmit = async (data: LoginFormValue) => { - 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() - } + // 로그인 액션 실행 + 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 isDisabled = !!(errors.email || errors.password || isLoading) diff --git a/src/app/login/loginAction.tsx b/src/app/login/loginAction.tsx new file mode 100644 index 0000000..7d7d208 --- /dev/null +++ b/src/app/login/loginAction.tsx @@ -0,0 +1,17 @@ +'use server' + +import { cookies } from 'next/headers' +import { redirect } from 'next/navigation' + +import api from '@/lib/axiosInstance' + +import { LoginFormValue } from './components/LoginForm' + +export default async function loginAction(data: LoginFormValue) { + // 폼에서 데이터 가져오기 = data + // 백엔드 요청 + // 가져온 json token 쿠키 설정 하기 + // 로그인 여부에 따라 redirect + + return { message: 'test' } +}