Skip to content

Commit

Permalink
[#206] login action 에러 처리
Browse files Browse the repository at this point in the history
  • Loading branch information
ylem76 committed Aug 17, 2024
1 parent d780738 commit 1996a70
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 24 deletions.
26 changes: 6 additions & 20 deletions src/app/login/components/LoginForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(<ConfirmModalContent message={loginErrorMessage} />)
// }
const errMsg = await loginAction(data)

// 에러 메시지 팝업
if (errMsg) {
openModal(<ConfirmModalContent message={errMsg} />)
}
}

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

0 comments on commit 1996a70

Please sign in to comment.