Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/app/(auth)/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export default function DashboardLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<div className="flex min-h-screen w-full items-center justify-center">
<div className="flex w-520 flex-col items-center justify-center gap-24 px-12 sm:px-0">
{children}
</div>
</div>
)
}
15 changes: 15 additions & 0 deletions src/app/(auth)/login/page.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
'use client'
import Link from 'next/link'
import { useEffect, useState } from 'react'

import AuthLogo from '@/app/features/auth/components/AuthLogo'
import LoginForm from '@/app/features/auth/components/LoginForm'

export default function Login() {
const [mounted, setMounted] = useState(false)
useEffect(() => setMounted(true), [])
if (!mounted) {
return null
}
return (
<>
<AuthLogo text="오늘도 만나서 반가워요!" />
<LoginForm />
<p className="text-16 font-normal">
회원이 아니신가요?{' '}
<Link className="Text-violet underline" href="/signup">
회원가입하기
</Link>
</p>
</>
)
}
Empty file removed src/app/features/.gitkeep
Empty file.
5 changes: 3 additions & 2 deletions src/app/features/auth/api/authApi.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import api from '@/app/shared/lib/axios'
import { AUTH_ENDPOINT } from './authEndpoint'
import { LoginRequest, SignupRequest, LoginResponse } from '../types/auth.type'
import { User as SignupResponse } from '@/app/shared/types/user.type'

import { LoginRequest, LoginResponse, SignupRequest } from '../types/auth.type'
import { AUTH_ENDPOINT } from './authEndpoint'

export const login = async (data: LoginRequest): Promise<LoginResponse> => {
const response = await api.post<LoginResponse>(AUTH_ENDPOINT.LOGIN, data)
return response.data
Expand Down
23 changes: 12 additions & 11 deletions src/app/features/auth/components/LoginForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import Input from '@components/Input'
import { cn } from '@lib/cn'
import { useForm } from 'react-hook-form'

import { useLoginSubmit } from '../hooks/useLoginSubmit'
import { useLoginMutation } from '../hooks/useLoginMutation'
import { loginValidation } from '../schemas/loginValidation'
import { LoginRequest } from '../types/auth.type'

export default function LoginForm() {
const {
register,
handleSubmit,
formState: { errors, isSubmitting, isValid },
formState: { errors, isValid },
} = useForm<LoginRequest>({
mode: 'onChange',
defaultValues: {
Expand All @@ -21,19 +21,20 @@ export default function LoginForm() {
},
})

const { submit } = useLoginSubmit()
const showEmailError = !!errors.email
const showPasswordError = !!errors.password
const { mutate: loginMutate, isPending } = useLoginMutation()

return (
<form className="flex flex-col gap-16" onSubmit={handleSubmit(submit)}>
<form
className="flex w-full flex-col gap-16"
onSubmit={handleSubmit(async (data) => await loginMutate(data))}
>
<Input
labelName="이메일"
type="email"
placeholder="이메일 입력"
autoComplete="email"
{...register('email', loginValidation.email)}
hasError={showEmailError}
hasError={!!errors.email}
errorMessage={errors.email?.message}
/>
<Input
Expand All @@ -42,18 +43,18 @@ export default function LoginForm() {
placeholder="비밀번호 입력"
autoComplete="off"
{...register('password', loginValidation.password)}
hasError={showPasswordError}
hasError={!!errors.password}
errorMessage={errors.password?.message}
/>
<button
type="submit"
className={cn(
'mt-8 h-50 w-full rounded-8 text-lg font-medium text-white',
isValid && !isSubmitting ? 'BG-blue' : 'BG-blue-disabled',
isValid && !isPending ? 'BG-blue' : 'BG-blue-disabled',
)}
disabled={!isValid || isSubmitting}
disabled={!isValid || isPending}
>
로그인
{isPending ? '로그인 중..' : '로그인'}
</button>
</form>
)
Expand Down
11 changes: 6 additions & 5 deletions src/app/features/auth/hooks/useAuth.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { login as loginApi, signup as signupApi } from '../api/authApi'
import { User } from '@/app/shared/types/user.type'

import { signup as signupApi } from '../api/authApi'
import { useAuthStore } from '../store/useAuthStore'
import { LoginRequest, SignupRequest } from '../types/auth.type'
import { SignupRequest } from '../types/auth.type'

export function useAuth() {
const { setAccessToken, setUser, clearAuthState } = useAuthStore()

async function login(data: LoginRequest) {
const response = await loginApi(data)
function updateAuthState(response: { accessToken: string; user: User }) {
const { accessToken, user } = response

if (!accessToken || !user) {
Expand All @@ -27,7 +28,7 @@ export function useAuth() {
}

return {
login,
updateAuthState,
signup,
logout,
}
Expand Down
35 changes: 35 additions & 0 deletions src/app/features/auth/hooks/useLoginMutation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { showError, showSuccess } from '@lib/toast'
import { useMutation } from '@tanstack/react-query'
import type { AxiosError } from 'axios'
import axios from 'axios'
import { useRouter } from 'next/navigation'

import { login } from '../api/authApi'
import { LoginRequest, LoginResponse } from '../types/auth.type'
import { useAuth } from './useAuth'

export function useLoginMutation() {
const router = useRouter()
const { updateAuthState } = useAuth()

return useMutation<LoginResponse, AxiosError | Error, LoginRequest>({
mutationFn: login,
onSuccess: async (response) => {
updateAuthState(response)
showSuccess('로그인에 성공하셨습니다!')
await new Promise((resolve) => setTimeout(resolve, 400))
router.push('/mydashboard')
},
onError: (error) => {
if (axios.isAxiosError(error)) {
const severMessage = (
error.response?.data as { message?: string } | undefined
)?.message
const fallback = error.message || '로그인 실패'
showError(severMessage ?? fallback)
} else {
showError('알 수 없는 에러 발생')
}
},
})
}
Comment on lines +1 to +35
Copy link
Contributor

Choose a reason for hiding this comment

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

react-query 사용하여 함수 작성해주셨군용!!
이전 코드 대비 어떤 장점이 있는 지 알 수 있을까용

Copy link
Author

Choose a reason for hiding this comment

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

우선 일관성의 목적으로 기존 try-catch에서 바꾸게 되었습니다! 현재 적용 해봤을 때 느낄 수 있는 장점은 우선 가독성 측면에서도 분리가 되어있어서 코드를 이해할 때 수월하고 react-qurey에서 쿼리의 실행 여부를 알 수 있는 isPending이 존재 하는데 그것을 이용해서 조건부 UI를 다룰 때 장점이 있었던 거 같습니다!

28 changes: 0 additions & 28 deletions src/app/features/auth/hooks/useLoginSubmit.ts

This file was deleted.

4 changes: 3 additions & 1 deletion src/app/globals.css
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ body {
.BG-ThemeToggleButton {
@apply bg-[#FFFFFF] dark:bg-[#D3D3D3];
}

.Text-violet {
@apply text-[#228DFF];
}
.BG-drag-hovered {
@apply bg-blue-100;
}