diff --git a/src/App.tsx b/src/App.tsx index e9736e2..60310cc 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -14,6 +14,7 @@ import { checkToken, getUserInfo } from './api/auth.ts' import userStore from './store/User.ts' import Home from './pages/Home' import Admin from './pages/Admin.tsx' +import { NotFound } from './pages/error/NotFound.tsx' function App() { const [location] = useLocation() @@ -46,6 +47,7 @@ function App() { + ) } diff --git a/src/Auth/Login.tsx b/src/Auth/Login.tsx index 165e436..73ca5a5 100644 --- a/src/Auth/Login.tsx +++ b/src/Auth/Login.tsx @@ -10,7 +10,6 @@ import verticalBar from '../assets/verticalBar.png' import { Link } from 'wouter' import { z } from 'zod' import { useLogin } from './auth.tsx' -import { StudentId } from './User.ts' interface Props { onClose: () => void @@ -79,13 +78,16 @@ export const LoginModal: FC = ({ context, ...props }) => { try { const loginForm = new FormData(event.currentTarget) - const studentId = StudentId.parse(loginForm.get('schoolId')) - const password = z.string().parse(loginForm.get('password')) + const studentId = loginForm.get('schoolId')?.toString() + const password = loginForm.get('password')?.toString() - await login(studentId, password) + const parsedPassword = z.string().parse(password) + + console.log('login try') + + await login(studentId!, parsedPassword) props.onClose() } catch { - //@TODO inform user that login has failed console.log('Failed to login') } }} @@ -97,7 +99,7 @@ export const LoginModal: FC = ({ context, ...props }) => { name='schoolId' label='학번' placeholder='학번을 입력해주세요' - pattern={/\d{10}/} + pattern={/^admin$|^\d{10}$/} errorMessage='학번은 숫자 열 자리여야 합니다' required css={css` diff --git a/src/Auth/auth.tsx b/src/Auth/auth.tsx index 2fe4c75..e8b8c1b 100644 --- a/src/Auth/auth.tsx +++ b/src/Auth/auth.tsx @@ -3,6 +3,7 @@ import { createContext, useContext, FC, ReactNode, useState } from 'react' import { StudentId } from './User.ts' import { addAccessTokenToServer } from '../api/index.ts' +import { useLocation } from 'wouter' //@TODO: Refactor this when React 19 release. const Auth = createContext< @@ -28,35 +29,50 @@ export function useCredential() { //@TODO: Discriminate login failures. export function useLogin(): ( - studentId: StudentId, + studentId: StudentId | string, password: string ) => Promise { const [, setCredential] = useContext(Auth) + const [, navigate] = useLocation() return async (studentId, password) => { const loginInfo = new FormData() - loginInfo.set('username', studentId) loginInfo.set('password', password) - const response = await fetch('/api/login', { - method: 'POST', - body: loginInfo, - }) + try { + const response = await fetch('/api/login', { + method: 'POST', + body: loginInfo, + }) - if (response.ok) { - const token = response.headers.get('Authorization')?.split(' ')[1] - if (token) { - addAccessTokenToServer(token) - } - const credential = await response.text() + const data = await response.json() - setCredential(credential) - window.location.reload() + if (data) { + // isAdmin 값을 sessionStorage에 저장 + sessionStorage.setItem('isAdmin', data.isAdmin.toString()) + console.log('어드민인가요?', data.isAdmin) - return true - } else { - return false + // isAdmin이 true일 경우 /admin 페이지로 이동 + if (data.isAdmin) { + navigate('/admin') + } + } + + if (response.ok) { + const token = response.headers.get('Authorization')?.split(' ')[1] + if (token) { + addAccessTokenToServer(token) + } + setCredential(data.credential) + window.location.reload() + return true + } else { + return false + } + } catch (error) { + console.error('Login request failed:', error) + throw error } } } diff --git a/src/api/auth.ts b/src/api/auth.ts index c32bc35..8945339 100644 --- a/src/api/auth.ts +++ b/src/api/auth.ts @@ -21,6 +21,7 @@ export const getUserInfo = async (): Promise => { export const logout = async () => { const res = await Server.post('logout') localStorage.removeItem('accessToken') + sessionStorage.removeItem('isAdmin') return res } diff --git a/src/common/Header.tsx b/src/common/Header.tsx index 3859776..e18c4aa 100644 --- a/src/common/Header.tsx +++ b/src/common/Header.tsx @@ -90,7 +90,9 @@ export const Header: FC = ({ num }) => { <>
- + + +