Skip to content

Commit

Permalink
feat: 로그인 로직 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
ho991217 committed Apr 4, 2024
1 parent c2608d9 commit f1f98b9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 42 deletions.
27 changes: 0 additions & 27 deletions app/[locale]/(back-nav)/login/actions.ts

This file was deleted.

15 changes: 4 additions & 11 deletions app/[locale]/(back-nav)/login/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
19 changes: 15 additions & 4 deletions hooks/useAuth.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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;
}
};

Expand Down

1 comment on commit f1f98b9

@vercel
Copy link

@vercel vercel bot commented on f1f98b9 Apr 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

danfesta2024 – ./

danfesta2024.vercel.app
danfesta2024-git-main-ho991217.vercel.app
danfesta2024-ho991217.vercel.app

Please sign in to comment.