Skip to content

Commit b73bae4

Browse files
夏一飞夏一飞
夏一飞
authored and
夏一飞
committed
refactor(auth): replace cookie with localStorage for token storage
1 parent ca6c98e commit b73bae4

File tree

5 files changed

+18
-23
lines changed

5 files changed

+18
-23
lines changed

app/api/v1/models/route.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export async function GET() {
7171
return {
7272
id: item.id,
7373
name: item.name,
74-
imageUrl: item.info?.meta?.profile_image_url || "/openwebui.png",
74+
imageUrl: item.info?.meta?.profile_image_url || "/static/favicon.png",
7575
input_price: priceInfo.input_price,
7676
output_price: priceInfo.output_price,
7777
};

app/components/Header.tsx

+1-5
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export default function Header() {
4040
};
4141

4242
useEffect(() => {
43-
const token = getAccessToken();
43+
const token = localStorage.getItem("access_token");
4444
setAccessToken(token);
4545

4646
if (!token) {
@@ -73,10 +73,6 @@ export default function Header() {
7373
};
7474

7575
const handleLogout = () => {
76-
document.cookie =
77-
"access_token=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT";
78-
document.cookie =
79-
"auth_token=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT";
8076
localStorage.removeItem("access_token");
8177
window.location.href = "/token";
8278
};

app/token/page.tsx

+4-8
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ export default function TokenPage() {
1212
const [showToken, setShowToken] = useState(false);
1313

1414
const handleSubmit = async (e: React.FormEvent) => {
15-
// 阻止表单默认提交行为
1615
e.preventDefault();
1716

1817
if (!token.trim()) {
@@ -22,8 +21,8 @@ export default function TokenPage() {
2221

2322
setLoading(true);
2423
try {
25-
// 将令牌存储在 cookie 中
26-
document.cookie = `access_token=${token}; path=/`;
24+
// 将令牌存储在 localStorage 中而不是 cookie
25+
localStorage.setItem("access_token", token);
2726

2827
// 尝试访问 API 验证令牌
2928
const res = await fetch("/api/config", {
@@ -34,20 +33,17 @@ export default function TokenPage() {
3433

3534
if (res.ok) {
3635
message.success("令牌验证成功");
37-
// 等待消息显示完成后再跳转
3836
setTimeout(() => {
3937
window.location.href = "/";
4038
}, 500);
4139
} else {
4240
message.error("无效的访问令牌");
43-
document.cookie =
44-
"access_token=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT";
41+
localStorage.removeItem("access_token");
4542
}
4643
} catch (error) {
4744
console.error("验证失败:", error);
4845
message.error("验证失败");
49-
document.cookie =
50-
"access_token=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT";
46+
localStorage.removeItem("access_token");
5147
} finally {
5248
setLoading(false);
5349
}

middleware.ts

+12-9
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ const API_KEY = process.env.API_KEY;
55
const ACCESS_TOKEN = process.env.ACCESS_TOKEN;
66

77
export async function middleware(request: NextRequest) {
8-
// console.log("中间件处理路径:", request.nextUrl.pathname);
98
const { pathname } = request.nextUrl;
109

1110
// 只验证 inlet/outlet/test API 请求
@@ -28,7 +27,6 @@ export async function middleware(request: NextRequest) {
2827
return NextResponse.json({ error: "无效的API密钥" }, { status: 401 });
2928
}
3029

31-
// API 密钥验证通过后直接返回
3230
return NextResponse.next();
3331
} else if (!pathname.startsWith("/api/")) {
3432
// 页面访问验证
@@ -37,22 +35,27 @@ export async function middleware(request: NextRequest) {
3735
return NextResponse.json({ error: "服务器配置错误" }, { status: 500 });
3836
}
3937

40-
const token = request.cookies.get("access_token")?.value;
41-
4238
// 如果是令牌验证页面,直接允许访问
4339
if (pathname === "/token") {
4440
return NextResponse.next();
4541
}
4642

47-
if (!token || token !== ACCESS_TOKEN) {
48-
console.log("访问令牌无效,重定向到令牌验证页");
49-
return NextResponse.redirect(new URL("/token", request.url));
50-
}
43+
// 添加 no-store 和 no-cache 头,防止 Cloudflare 缓存
44+
const response = NextResponse.next();
45+
response.headers.set(
46+
"Cache-Control",
47+
"no-store, no-cache, must-revalidate, proxy-revalidate"
48+
);
49+
response.headers.set("Pragma", "no-cache");
50+
response.headers.set("Expires", "0");
51+
52+
return response;
5153
}
5254

5355
return NextResponse.next();
5456
}
5557

58+
// 配置中间件匹配的路由
5659
export const config = {
57-
matcher: ["/((?!api/auth|_next/static|_next/image|favicon.ico).*)"],
60+
matcher: ["/((?!_next/static|_next/image|favicon.ico).*)"],
5861
};
File renamed without changes.

0 commit comments

Comments
 (0)