Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions frontend/src/librarian/LibrarianApp.jsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
import { useState, useEffect } from 'react'
import { useLocation, useNavigate } from 'react-router-dom'
import LibrarianLogin from './LibrarianLogin'
import LibrarianRegister from './LibrarianRegister'
import LibrarianDashboard from './LibrarianDashboard'
import { isAuthenticated, logout } from './api' // 改为新函数

function LibrarianApp() {
const location = useLocation()
const navigate = useNavigate()
const [isLoggedIn, setIsLoggedIn] = useState(false)
const [librarian, setLibrarian] = useState(null)
const [showRegister, setShowRegister] = useState(false)
const [showRegister, setShowRegister] = useState(() => new URLSearchParams(location.search).get('register') === 'true')
const [loading, setLoading] = useState(true)

useEffect(() => {
setShowRegister(new URLSearchParams(location.search).get('register') === 'true')
}, [location.search])

useEffect(() => {
// 检查登录状态 - 使用新函数
if (isAuthenticated()) {
Expand Down Expand Up @@ -44,6 +51,7 @@ function LibrarianApp() {
logout() // 使用新函数
setIsLoggedIn(false)
setLibrarian(null)
navigate('/login')
}

const handleRegisterSuccess = () => {
Expand All @@ -66,7 +74,7 @@ function LibrarianApp() {
return (
<LibrarianRegister
onRegister={handleRegisterSuccess}
onSwitchToLogin={() => setShowRegister(false)}
onSwitchToLogin={() => navigate('/login')}
/>
)
}
Expand Down
33 changes: 23 additions & 10 deletions frontend/src/pages/UnifiedLogin.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { useState, useEffect } from 'react';
import { useNavigate } from 'react-router-dom';

const UnifiedLogin = () => {
const navigate = useNavigate();
const [role, setRole] = useState('reader');
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
Expand Down Expand Up @@ -36,7 +38,6 @@ const UnifiedLogin = () => {
setLoading(true);

try {
let apiEndpoint = 'http://localhost:3001/api/auth/login';
let requestBody = {};

if (role === 'reader') {
Expand All @@ -46,10 +47,10 @@ const UnifiedLogin = () => {
type: 'student'
};
} else if (role === 'librarian') {
apiEndpoint = 'http://localhost:3001/api/auth/login-librarian';
requestBody = {
employeeId: employeeId || email,
password: password
email: employeeId || email,
password: password,
type: 'librarian'
};
} else if (role === 'admin') {
requestBody = {
Expand All @@ -59,7 +60,7 @@ const UnifiedLogin = () => {
};
}

const res = await fetch(apiEndpoint, {
const res = await fetch('http://localhost:3001/api/auth/login', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(requestBody)
Expand All @@ -81,21 +82,21 @@ const UnifiedLogin = () => {
setBannedInfo(null);

if (role === 'librarian') {
localStorage.setItem('librarianToken', data.token);
localStorage.setItem('librarianInfo', JSON.stringify(data.librarian));
localStorage.setItem('token', data.token);
localStorage.setItem('user', JSON.stringify(data.librarian));
if (rememberMe) {
localStorage.setItem('savedEmployeeId', employeeId || email);
} else {
localStorage.removeItem('savedEmployeeId');
}
window.location.href = '/librarian-login';
navigate('/librarian-login');
} else {
localStorage.setItem('token', data.token);
localStorage.setItem('user', JSON.stringify(data.user));
if (role === 'admin') {
window.location.href = '/admin-dashboard';
navigate('/admin-dashboard');
} else {
window.location.href = '/';
navigate('/');
}
}
} catch (err) {
Expand Down Expand Up @@ -289,6 +290,18 @@ const UnifiedLogin = () => {
</a>
</p>
)}
{role === 'librarian' && (
<p className="text-center text-gray-500 text-sm mt-6">
还没有馆员账号?{' '}
<button
type="button"
onClick={() => navigate('/librarian-login?register=true')}
className="text-blue-500 hover:text-blue-600 font-medium"
>
点击注册
</button>
</p>
)}
</div>

<div className="bg-gray-50 px-8 py-4 border-t border-gray-200">
Expand Down