|
1 | 1 | import { useState, FormEvent, ChangeEvent, MouseEvent, useEffect } from 'react'
|
2 | 2 | import { useRouter } from 'next/router'
|
3 |
| -import axios from 'axios' |
4 | 3 | import styled from '@emotion/styled'
|
5 | 4 | import { SecurityIcon } from '../Icons'
|
6 | 5 | import Input from '@/components/common/Input/Input'
|
7 | 6 | import Link from 'next/link'
|
8 | 7 | import BasicButton from '../common/Button/BasicButton'
|
9 | 8 | import Spacing from '../common/Spacing/Spacing'
|
| 9 | +import { axiosHttp } from '@/lib/utils/httpCore' |
| 10 | + |
| 11 | +const initialSignInValue = { |
| 12 | + email: '', |
| 13 | + password: '', |
| 14 | +} |
10 | 15 |
|
11 | 16 | export default function SigninForm() {
|
12 | 17 | const router = useRouter()
|
13 | 18 |
|
14 | 19 | useEffect(() => {
|
15 |
| - // 마운트 시 토큰 존재하면 업로드 페이지 이동 -> ( api 받으면 추후 세션 방식으로 바꿔야 한다 ) - 자동 로그인 되어있을 경우 업로드로 라우팅도 구현해야 |
| 20 | + // 마운트시 localStorage에 memberId 있으면 upload 페이지로 라우팅 |
16 | 21 | if (localStorage.getItem('memberId')) {
|
17 | 22 | router.push('/upload')
|
18 | 23 | }
|
19 | 24 | }, [router])
|
20 | 25 |
|
21 |
| - const [signinConditions, setSigninConditions] = useState({ |
22 |
| - email: '', |
23 |
| - password: '', |
24 |
| - }) |
| 26 | + const [signInValue, setSignInValue] = useState(initialSignInValue) |
25 | 27 |
|
26 |
| - const updateSigninConditions = (e: ChangeEvent<HTMLInputElement>) => { |
27 |
| - // input 값에 onChange 를 통해 Conditions 을 변화시키는 함수 |
| 28 | + const handleChangeSigninValue = (e: ChangeEvent<HTMLInputElement>) => { |
28 | 29 | const { value, name } = e.target
|
29 |
| - setSigninConditions((prevConditions) => ({ |
30 |
| - ...prevConditions, |
31 |
| - [name]: value, // 네임 가져와서 네임에 맞는 애로 변경 |
| 30 | + setSignInValue((prev) => ({ |
| 31 | + ...prev, |
| 32 | + [name]: value, |
32 | 33 | }))
|
33 | 34 | }
|
34 | 35 |
|
35 |
| - const signinFunction = async (e: FormEvent) => { |
36 |
| - // 로그인 버튼 클릭시 폼 제출 후 업로드 페이지로 라우팅 |
| 36 | + const handleSignIn = async (e: FormEvent) => { |
37 | 37 | e.preventDefault()
|
38 |
| - |
39 |
| - if (!signinConditions.email || !signinConditions.password) return alert('아이디나 비밀번호를 입력하세요!') |
| 38 | + if (!signInValue.email || !signInValue.password) return alert('아이디 혹은 비밀번호를 입력하세요!') |
40 | 39 |
|
41 | 40 | try {
|
42 |
| - const response = await axios.post(`${process.env.NEXT_PUBLIC_SERVER_DEFAULT_END_POINT}member/login`, { |
43 |
| - memberEmail: signinConditions.email, |
44 |
| - memberPassword: signinConditions.password, |
| 41 | + const response = await axiosHttp.post(`/member/login`, { |
| 42 | + memberEmail: signInValue.email, |
| 43 | + memberPassword: signInValue.password, |
45 | 44 | })
|
46 |
| - |
47 |
| - if (response.data === '로그인 실패') return alert(response.data) |
48 |
| - |
49 | 45 | localStorage.setItem('memberId', response.data.id)
|
50 | 46 | router.push('/upload')
|
51 | 47 | } catch (error) {
|
52 |
| - alert('아이디나 비밀번호를 확인하세요!') |
| 48 | + if (error instanceof Error) alert(error.message) |
53 | 49 | }
|
54 | 50 | }
|
55 | 51 |
|
56 |
| - const goToSignupPage = (e: MouseEvent<HTMLAnchorElement>) => { |
57 |
| - // Make an Account 클릭시 회원가입 페이지로 이동 |
58 |
| - e.preventDefault() |
59 |
| - |
60 |
| - router.push('/sign-up') |
61 |
| - } |
62 |
| - |
63 | 52 | return (
|
64 | 53 | <Wrapper>
|
65 |
| - <form onSubmit={signinFunction}> |
| 54 | + <form onSubmit={handleSignIn}> |
66 | 55 | <Spacing size={20} />
|
67 | 56 | <Input
|
68 |
| - value={signinConditions.email} |
| 57 | + value={signInValue.email} |
69 | 58 | inputLabel="Email"
|
70 | 59 | type="text"
|
71 | 60 | name="email"
|
72 |
| - onChange={updateSigninConditions} |
| 61 | + onChange={handleChangeSigninValue} |
73 | 62 | colorType="PENETRATED_BLACK"
|
74 | 63 | />
|
75 | 64 | <Spacing size={20} />
|
76 | 65 | <Input
|
77 |
| - value={signinConditions.password} |
| 66 | + value={signInValue.password} |
78 | 67 | inputLabel="Password"
|
79 | 68 | type="password"
|
80 | 69 | name="password"
|
81 |
| - onChange={updateSigninConditions} |
| 70 | + onChange={handleChangeSigninValue} |
82 | 71 | svgIcon={<SecurityIcon width="16" height="17" color="white" />}
|
83 | 72 | colorType="PENETRATED_BLACK"
|
84 | 73 | />
|
85 | 74 | <Spacing size={70} />
|
86 | 75 | <BasicButton>Log in</BasicButton>
|
87 | 76 | <Spacing size={20} />
|
88 |
| - <StyledLink href="#" onClick={goToSignupPage}> |
89 |
| - Make an account |
90 |
| - </StyledLink> |
| 77 | + <StyledLink href="/sign-up">Make an account</StyledLink> |
91 | 78 | </form>
|
92 | 79 | </Wrapper>
|
93 | 80 | )
|
|
0 commit comments