jwt 로그인 구현 진행 : axios 로 api 호출하여 생성된 쿠키로 인증상태값 변경기능 구현
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import React from 'react'
|
||||
import { Link } from 'react-router-dom'
|
||||
import React, { useState } from 'react'
|
||||
import { Link, useNavigate } from 'react-router-dom'
|
||||
import {
|
||||
CButton,
|
||||
CCard,
|
||||
@@ -15,8 +15,53 @@ import {
|
||||
} from '@coreui/react'
|
||||
import CIcon from '@coreui/icons-react'
|
||||
import { cilLockLocked, cilUser } from '@coreui/icons'
|
||||
import { login as apiLogin, DecodedToken, getAccessTokenFromCookie } from 'src/axios/authService';
|
||||
import { useAuth } from 'src/hooks/useAuth';
|
||||
import { jwtDecode } from 'jwt-decode';
|
||||
|
||||
const Login = () => {
|
||||
const [memberId, setMemberId] = useState('');
|
||||
const [password, setPassword] = useState('');
|
||||
const [error, setError] = useState('');
|
||||
const [loading, setLoading] = useState(false);
|
||||
|
||||
const { login } = useAuth();
|
||||
const navigate = useNavigate();
|
||||
|
||||
const handleSubmit = async (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
setError('');
|
||||
setLoading(true);
|
||||
|
||||
try {
|
||||
const response = await apiLogin({ memberId, password });
|
||||
|
||||
if (response.resultCode === '200') {
|
||||
const accessToken = getAccessTokenFromCookie();
|
||||
|
||||
if (accessToken) {
|
||||
const decodedToken = jwtDecode<DecodedToken>(accessToken);
|
||||
const member = {
|
||||
memberId: decodedToken.memberId,
|
||||
memberName: decodedToken.memberName,
|
||||
};
|
||||
|
||||
login(accessToken, member);
|
||||
navigate('/dashboard');
|
||||
} else {
|
||||
setError('쿠키에서 accessToken을 찾을 수 없습니다.');
|
||||
}
|
||||
} else {
|
||||
setError(response.resultMessage || '로그인에 실패했습니다.');
|
||||
}
|
||||
} catch (err: any) {
|
||||
setError(err.response?.data?.message || '로그인 중 오류가 발생했습니다.');
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
return (
|
||||
<div className="bg-body-tertiary min-vh-100 d-flex flex-row align-items-center">
|
||||
<CContainer>
|
||||
@@ -25,14 +70,20 @@ const Login = () => {
|
||||
<CCardGroup>
|
||||
<CCard className="p-4">
|
||||
<CCardBody>
|
||||
<CForm>
|
||||
<CForm onSubmit={handleSubmit}>
|
||||
<h1>Login</h1>
|
||||
<p className="text-body-secondary">Sign In to your account</p>
|
||||
{error && <div className="text-danger mb-3">{error}</div>}
|
||||
<CInputGroup className="mb-3">
|
||||
<CInputGroupText>
|
||||
<CIcon icon={cilUser} />
|
||||
</CInputGroupText>
|
||||
<CFormInput placeholder="Username" autoComplete="username" />
|
||||
<CFormInput
|
||||
placeholder="Username"
|
||||
autoComplete="username"
|
||||
value={memberId}
|
||||
onChange={(e) => setMemberId(e.target.value)}
|
||||
/>
|
||||
</CInputGroup>
|
||||
<CInputGroup className="mb-4">
|
||||
<CInputGroupText>
|
||||
@@ -42,12 +93,14 @@ const Login = () => {
|
||||
type="password"
|
||||
placeholder="Password"
|
||||
autoComplete="current-password"
|
||||
value={password}
|
||||
onChange={(e) => setPassword(e.target.value)}
|
||||
/>
|
||||
</CInputGroup>
|
||||
<CRow>
|
||||
<CCol xs={6}>
|
||||
<CButton color="primary" className="px-4">
|
||||
Login
|
||||
<CButton color="primary" className="px-4" type="submit" disabled={loading}>
|
||||
{loading ? 'Logging in...' : 'Login'}
|
||||
</CButton>
|
||||
</CCol>
|
||||
<CCol xs={6} className="text-right">
|
||||
|
||||
Reference in New Issue
Block a user