mirror of
https://github.com/MoonTechLab/LunaTV.git
synced 2026-05-21 05:27:29 +08:00
feat: use middleware to auth
This commit is contained in:
@@ -46,7 +46,7 @@ function LoginPageClient() {
|
||||
});
|
||||
|
||||
if (res.ok) {
|
||||
// 保存密码和用户名以供后续请求使用
|
||||
// API 已经设置了认证cookie,这里只保存到localStorage用于向后兼容
|
||||
if (typeof window !== 'undefined') {
|
||||
localStorage.setItem('password', password);
|
||||
if (shouldAskUsername) {
|
||||
@@ -62,12 +62,14 @@ function LoginPageClient() {
|
||||
const data = await res.json().catch(() => ({}));
|
||||
setError(data.error ?? '服务器错误');
|
||||
}
|
||||
} catch (error) {
|
||||
setError('网络错误,请稍后重试');
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
// 新增:处理注册逻辑
|
||||
// 处理注册逻辑
|
||||
const handleRegister = async () => {
|
||||
setError(null);
|
||||
if (!password || !username) return;
|
||||
@@ -81,16 +83,20 @@ function LoginPageClient() {
|
||||
});
|
||||
|
||||
if (res.ok) {
|
||||
// API 已经设置了认证cookie,这里只保存到localStorage用于向后兼容
|
||||
if (typeof window !== 'undefined') {
|
||||
localStorage.setItem('password', password);
|
||||
localStorage.setItem('username', username);
|
||||
}
|
||||
|
||||
const redirect = searchParams.get('redirect') || '/';
|
||||
router.replace(redirect);
|
||||
} else {
|
||||
const data = await res.json().catch(() => ({}));
|
||||
setError(data.error ?? '服务器错误');
|
||||
}
|
||||
} catch (error) {
|
||||
setError('网络错误,请稍后重试');
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
@@ -182,7 +188,7 @@ function LoginPageClient() {
|
||||
|
||||
export default function LoginPage() {
|
||||
return (
|
||||
<Suspense>
|
||||
<Suspense fallback={<div>Loading...</div>}>
|
||||
<LoginPageClient />
|
||||
</Suspense>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user