diff --git a/app/[locale]/(back-nav)/login/actions.ts b/app/[locale]/(back-nav)/login/actions.ts deleted file mode 100644 index ce4bdbb..0000000 --- a/app/[locale]/(back-nav)/login/actions.ts +++ /dev/null @@ -1,27 +0,0 @@ -'use server'; - -import { post } from '@/api'; -import { API_ROUTES, COOKIE_KEYS } from '@/constants'; -import { redirect } from 'next/navigation'; -import { cookies } from 'next/headers'; -import { getLocale } from 'next-intl/server'; -import { AuthInfoSchema } from './schema'; - -export type AuthRes = { - accessToken: string; - refreshToken: string; -}; - -export async function authenticate(data: AuthInfoSchema) { - const { accessToken, refreshToken } = await post( - API_ROUTES.user.login, - data - ); - - cookies().set(COOKIE_KEYS.accessToken, accessToken); - - cookies().set(COOKIE_KEYS.refreshToken, refreshToken); - - const locale = await getLocale(); - redirect(`/${locale}`); -} diff --git a/app/[locale]/(back-nav)/login/page.tsx b/app/[locale]/(back-nav)/login/page.tsx index aa1b85a..9aa5fa4 100644 --- a/app/[locale]/(back-nav)/login/page.tsx +++ b/app/[locale]/(back-nav)/login/page.tsx @@ -8,27 +8,20 @@ import { useState } from 'react'; import APIError from '@/lib/utils/error/api-error'; import { toast } from 'sonner'; import { useAuth } from '@/hooks'; -import { API_ROUTES, API_URL } from '@/constants'; import { useCookies } from 'next-client-cookies'; +import { useRouter } from 'next/navigation'; export default function LoginPage() { const [isLoading, setIsLoading] = useState(false); - const cookies = useCookies(); const locale = useLocale(); const { login } = useAuth(); + const router = useRouter(); const onSubmit = async (data: AuthInfoSchema) => { try { setIsLoading(true); - const res = await fetch(`${API_URL}${API_ROUTES.user.login}`, { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify(data), - }).then((res) => res.json()); - - cookies.set('accessToken', res.accessToken); + login(data); + router.push(`/${locale}/`); } catch (error) { const e = error as APIError; toast.error(e.message); diff --git a/hooks/useAuth.ts b/hooks/useAuth.ts index 1a4f301..46ddfb4 100644 --- a/hooks/useAuth.ts +++ b/hooks/useAuth.ts @@ -1,8 +1,6 @@ 'use client'; -import { get } from '@/api'; import { User } from '@/api/response'; -import { authenticate } from '@/app/[locale]/(back-nav)/login/actions'; import { AuthInfoSchema } from '@/app/[locale]/(back-nav)/login/schema'; import { API_ROUTES, API_URL, COOKIE_KEYS } from '@/constants'; import { useCookies } from 'next-client-cookies'; @@ -52,11 +50,24 @@ export default function useAuth() { const login = async (req: AuthInfoSchema) => { try { - await authenticate(req); + const res = await fetch(`${API_URL}${API_ROUTES.user.login}`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(req), + }).then((res) => res.json()); + + if (!res.accessToken || !res.refreshToken) + throw new Error('토큰이 없습니다.'); + cookies.set(COOKIE_KEYS.accessToken, res.accessToken); + cookies.set(COOKIE_KEYS.refreshToken, res.refreshToken); + + setIsLoggedIn(true); } catch (error) { const e = error as Error; - toast.error(e.message); setIsLoggedIn(false); + throw e; } };