diff --git a/src/App.tsx b/src/App.tsx index 873fcc8..9ced9b0 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,10 +1,8 @@ -import { Routes, Route, BrowserRouter } from 'react-router-dom' +import { Routes, Route, BrowserRouter, Navigate } from 'react-router-dom' import Login from './router/Login' import User from './router/User' import Manager from './router/Manager' -import { Toaster } from 'react-hot-toast' import { useEffect } from 'react' -import AdminIndex from './router/admin/Index' import BusList from './router/admin/bus/List' import UserList from './router/admin/user/List' import UserCreate from './router/admin/user/Create' @@ -34,14 +32,13 @@ function App() { return ( - } /> } /> } /> - } /> + } /> } /> } /> diff --git a/src/main.tsx b/src/main.tsx index 3d7150d..f5e4588 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -2,9 +2,29 @@ import React from 'react' import ReactDOM from 'react-dom/client' import App from './App.tsx' import './index.css' +import { Toaster } from 'react-hot-toast' ReactDOM.createRoot(document.getElementById('root')!).render( + , ) diff --git a/src/router/Login.tsx b/src/router/Login.tsx index 0f542a1..fdde83e 100644 --- a/src/router/Login.tsx +++ b/src/router/Login.tsx @@ -2,6 +2,7 @@ import { FormEvent, useEffect, useState } from 'react' import Container from '../components/Container' import logo from './../assets/logo.png' import toast from 'react-hot-toast' +import { useNavigate } from 'react-router-dom' function Login() { const [type, setType] = useState<'student' | 'teacher'>('student') @@ -9,6 +10,7 @@ function Login() { const [name, setName] = useState('') const [phone, setPhone] = useState('') const [password, setPassword] = useState('') + const navigation = useNavigate() const switchType = () => { setType(type === 'student' ? 'teacher' : 'student') } @@ -22,56 +24,25 @@ function Login() { if (type === 'student') { if (!studentId || !name || !phone) { - toast.error('λͺ¨λ“  ν•­λͺ©μ„ μž…λ ₯ν•΄μ£Όμ„Έμš”.', - { - duration: 3000, - icon: '❌', - style: { - borderRadius: '10px', - background: '#300', - color: '#fff', - } - } - ) + toast.error('λͺ¨λ“  ν•­λͺ©μ„ μž…λ ₯ν•΄μ£Όμ„Έμš”.') return } - const res = await fetch('/api/user/login', { + const res = await fetch('/api/auth/user', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ student_id: studentId, phone_number: phone, name, - password: 'dummy' }) }) if (res.status !== 200) { try { const responseBody = await res.json() - toast.error(responseBody.detail, - { - duration: 3000, - icon: '❌', - style: { - borderRadius: '10px', - background: '#300', - color: '#fff', - } - } - ) + toast.error(responseBody.detail) } catch { - toast.error('μ„œλ²„ 였λ₯˜ λ°œμƒ', - { - duration: 3000, - icon: '❌', - style: { - borderRadius: '10px', - background: '#300', - color: '#fff', - } - } - ) + toast.error('μ„œλ²„ 였λ₯˜ λ°œμƒ') } return } @@ -81,59 +52,26 @@ function Login() { localStorage.setItem('access_token', access_token) localStorage.setItem('totp_secret', totp_secret) - window.location.href = type === 'USER' ? '/user' : type === 'BUS_ADMIN' ? '/manager' : '/admin' + return navigation(type === 'USER' ? '/user' : type === 'BUS_ADMIN' ? '/manager' : '/admin') } else { if (!password) { - toast.error('λͺ¨λ“  ν•­λͺ©μ„ μž…λ ₯ν•΄μ£Όμ„Έμš”.', - { - duration: 3000, - icon: '❌', - style: { - borderRadius: '10px', - background: '#300', - color: '#fff', - } - } - ) + toast.error('λͺ¨λ“  ν•­λͺ©μ„ μž…λ ₯ν•΄μ£Όμ„Έμš”.') return } - const res = await fetch('/api/user/login', { + const res = await fetch('/api/auth/admin', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ - password, - phone_number: 'dummy', - name: 'dummy', - student_id: 'dummy' + password }) }) if (res.status !== 200) { try { const responseBody = await res.json() - toast.error(responseBody.detail, - { - duration: 3000, - icon: '❌', - style: { - borderRadius: '10px', - background: '#300', - color: '#fff', - } - } - ) + toast.error(responseBody.detail) } catch { - toast.error('μ„œλ²„ 였λ₯˜ λ°œμƒ', - { - duration: 3000, - icon: '❌', - style: { - borderRadius: '10px', - background: '#300', - color: '#fff', - } - } - ) + toast.error('μ„œλ²„ 였λ₯˜ λ°œμƒ') } // window.location.reload() @@ -144,7 +82,7 @@ function Login() { toast.success('둜그인 성곡!') localStorage.setItem('access_token', access_token) - window.location.href = type === 'USER' ? '/user' : type === 'BUS_ADMIN' ? '/manager' : '/admin' + return navigation(type === 'USER' ? '/user' : type === 'BUS_ADMIN' ? '/manager' : '/admin') } } diff --git a/src/router/Manager.tsx b/src/router/Manager.tsx index 8760970..cb3a7bd 100644 --- a/src/router/Manager.tsx +++ b/src/router/Manager.tsx @@ -7,6 +7,7 @@ import { faBus, faHome, faUser, faXmark } from '@fortawesome/free-solid-svg-icon import Modal from 'react-modal' import useSWR from 'swr' import { fetcher } from '../common/fetcher' +import { redirect } from 'react-router-dom' function Manager() { if (!localStorage.getItem('access_token')) window.location.href = '/' @@ -98,7 +99,6 @@ function Manager() { // ) }, [qrData]) - // if data is loaded fetch /api/bus/id useEffect(() => { if (!data) return console.log(data) @@ -111,18 +111,8 @@ function Manager() { }, [data]) function errorHandling () { - toast.error('μ„œλ²„ 였λ₯˜ λ°œμƒ', - { - duration: 3000, - icon: '❌', - style: { - borderRadius: '10px', - background: '#300', - color: '#fff', - } - } - ) - window.location.href = '/' + toast.error('μ„œλ²„ 였λ₯˜ λ°œμƒ') + redirect('/') } if (isLoading) return Loading... @@ -134,17 +124,7 @@ function Manager() { const onBusStart = () => { if (!confirm('정말 λ²„μŠ€λ₯Ό μΆœλ°œν• κΉŒμš”?')) return - toast.success('μˆ˜κ³ ν•˜μ…¨μŠ΅λ‹ˆλ‹€!', - { - duration: 3000, - icon: '🌸', - style: { - borderRadius: '10px', - background: '#393', - color: '#fff', - } - } - ) + toast.success('μˆ˜κ³ ν•˜μ…¨μŠ΅λ‹ˆλ‹€!', { duration: 3000, icon: '🌸'}) return }