Skip to content

Commit

Permalink
[#206] add server actions
Browse files Browse the repository at this point in the history
  • Loading branch information
ylem76 committed Aug 17, 2024
1 parent e977704 commit d780738
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 18 deletions.
42 changes: 24 additions & 18 deletions src/app/login/components/LoginForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -29,31 +31,35 @@ export default function LoginForm() {
} = useForm<LoginFormValue>()

const router = useRouter()

const { openModal } = useModalStore()
const { setDashboards } = useDashboardStore()

const [pwdVisible, togglePwd] = useToggle(false)
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(<ConfirmModalContent message={loginErrorMessage} />)
}
// 로그인 액션 실행
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(<ConfirmModalContent message={loginErrorMessage} />)
// }
}

const isDisabled = !!(errors.email || errors.password || isLoading)
Expand Down
17 changes: 17 additions & 0 deletions src/app/login/loginAction.tsx
Original file line number Diff line number Diff line change
@@ -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' }
}

0 comments on commit d780738

Please sign in to comment.