feat(auth): implement login page and authentication middleware feat(sidebar): create sidebar component with user info and navigation links feat(api): establish API client for interacting with backend services
28 lines
756 B
TypeScript
28 lines
756 B
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
||
|
||
export async function middleware(request: NextRequest) {
|
||
const { pathname } = request.nextUrl;
|
||
|
||
// 只保护 /dashboard 路由
|
||
if (!pathname.startsWith("/dashboard")) {
|
||
return NextResponse.next();
|
||
}
|
||
|
||
// 检查 better-auth session cookie(cookie 前缀是 helios_auth)
|
||
const sessionCookie =
|
||
request.cookies.get("helios_auth.session_token") ??
|
||
request.cookies.get("__Secure-helios_auth.session_token");
|
||
|
||
if (!sessionCookie) {
|
||
const loginUrl = new URL("/login", request.url);
|
||
loginUrl.searchParams.set("from", pathname);
|
||
return NextResponse.redirect(loginUrl);
|
||
}
|
||
|
||
return NextResponse.next();
|
||
}
|
||
|
||
export const config = {
|
||
matcher: ["/dashboard/:path*"],
|
||
};
|