0.3 增加登录模块
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
import { convexAuthNextjsMiddleware, createRouteMatcher, nextjsMiddlewareRedirect } from "@convex-dev/auth/nextjs/server";
|
||||
|
||||
// 说明:
|
||||
// - 这里启用 Convex Auth 的 Next.js 中间件,负责:
|
||||
// 1) 代理 /api/auth 到 Convex(用于 signIn / signOut 等动作)
|
||||
// 2) 刷新/同步 auth cookies
|
||||
// 3) 对需要登录的页面做统一拦截
|
||||
// - 默认将未登录用户重定向到 /auth(Convex Auth 登录页)。
|
||||
|
||||
const isPublicRoute = createRouteMatcher([
|
||||
"/auth",
|
||||
"/login",
|
||||
"/api/auth(.*)",
|
||||
"/api/health(.*)",
|
||||
"/_next(.*)",
|
||||
"/favicon.ico",
|
||||
]);
|
||||
|
||||
export default convexAuthNextjsMiddleware(async (request, ctx) => {
|
||||
// 公共路由不做拦截(但 middleware 仍会处理 token 刷新/代理等)。
|
||||
if (isPublicRoute(request)) return;
|
||||
|
||||
// 只在启用 Convex 模式时做鉴权拦截;否则走 Supabase 逻辑(页面内部自行处理)。
|
||||
// 注意:middleware 运行在 Edge/Node 环境中,读取到的是运行期环境变量。
|
||||
if (process.env.NEXT_PUBLIC_USE_CONVEX !== "1") return;
|
||||
|
||||
const authed = await ctx.convexAuth.isAuthenticated();
|
||||
if (!authed) {
|
||||
return nextjsMiddlewareRedirect(request, "/auth");
|
||||
}
|
||||
});
|
||||
|
||||
export const config = {
|
||||
// 说明:排除静态资源,避免无意义的中间件开销。
|
||||
matcher: ["/((?!_next/static|_next/image|favicon.ico).*)"],
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user