11import { useState } from "react" ;
2- import Input from "@/components/input/Input" ;
2+ import { useRouter } from "next/router" ;
3+ import { getUserInfo } from "@/api/user" ;
4+ import useUserStore from "@/store/useUserStore" ;
5+ import axiosInstance from "@/api/axiosInstance" ;
36import Link from "next/link" ;
7+ import Input from "@/components/input/Input" ;
8+ import { TEAM_ID } from "@/constants/team" ;
49
510export default function LoginPage ( ) {
6- const [ email , setEmail ] = useState ( "" ) ;
7- const [ password , setPassword ] = useState ( "" ) ;
11+ const router = useRouter ( ) ;
12+ const [ values , setValues ] = useState ( {
13+ email : "" ,
14+ password : "" ,
15+ } ) ;
16+ const isFormValid = Object . values ( values ) . every ( ( v ) => v . trim ( ) !== "" ) ;
17+
18+ const handleChange = ( name : string ) => ( value : string ) => {
19+ setValues ( ( prev ) => ( {
20+ ...prev ,
21+ [ name ] : value ,
22+ } ) ) ;
23+ } ;
24+
25+ const handleSubmit = async ( e : React . FormEvent < HTMLFormElement > ) => {
26+ e . preventDefault ( ) ;
27+ const { email, password } = values ;
28+ try {
29+ const response = await axiosInstance . post ( `${ TEAM_ID } /auth/login` , {
30+ email,
31+ password,
32+ } ) ;
33+
34+ const token = response . data . accessToken ;
35+ localStorage . setItem ( "accessToken" , token ) ;
36+
37+ const userData = await getUserInfo ( { teamId : TEAM_ID } ) ; // 로그인 성공 후 사용자 정보 요청
38+ useUserStore . getState ( ) . setUser ( userData ) ; // Zustand에 저장
839
9- const isFormValid = email . trim ( ) !== "" && password . trim ( ) !== "" ;
40+ router . push ( "/mydashboard" ) ;
41+ } catch ( error ) {
42+ console . error ( "로그인 실패:" , error ) ;
43+ alert ( "로그인에 실패했습니다." ) ;
44+ }
45+ } ;
1046
1147 return (
1248 < div className = "flex flex-col items-center justify-center h-screen bg-[var(--color-gray5)] py-10" >
1349 < div className = "text-center mb-[40px]" >
1450 < img
1551 src = "/svgs/main-logo.svg"
1652 alt = "태스키파이 로고 이미지"
17- className = "w-[200px] h-[280px] relative "
53+ className = "w-[200px] h-[280px]"
1854 />
1955 < p className = "font-20m text-black3" > 오늘도 만나서 반가워요!</ p >
2056 </ div >
2157
22- < form className = "flex flex-col w-[350px] md:w-[520px] gap-[20px] font-16r text-black3" >
58+ < form
59+ onSubmit = { handleSubmit }
60+ className = "flex flex-col w-[350px] md:w-[520px] gap-[20px] font-16r text-black3"
61+ >
2362 < Input
2463 type = "email"
2564 name = "email"
2665 label = "이메일"
2766 placeholder = "이메일을 입력해 주세요"
28- onChange = { setEmail }
29- pattern = "^[\w.-]+@[\w.-]+\.\w{2,}$"
67+ onChange = { handleChange ( "email" ) }
3068 invalidMessage = "올바른 이메일 주소를 입력해 주세요"
3169 />
3270
@@ -35,7 +73,7 @@ export default function LoginPage() {
3573 name = "password"
3674 label = "비밀번호"
3775 placeholder = "비밀번호를 입력해 주세요"
38- onChange = { setPassword }
76+ onChange = { handleChange ( "password" ) }
3977 pattern = ".{8,}"
4078 invalidMessage = "비밀번호는 8자 이상이어야 해요"
4179 />
0 commit comments