1 Star 0 Fork 30

15001886364/MP_WeiXin_ChatGPT

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
middleware.ts 1.32 KB
一键复制 编辑 原始数据 按行查看 历史
CyrusZhou 提交于 2023-05-20 09:47 . 取消 NEXTAUTH 验证
//middleware.ts
import { getIronSession } from "iron-session/edge";
import { sessionOptions } from "./lib/session";
import { NextRequest, NextResponse } from "next/server";
export const middleware = async (req: NextRequest) => {
// Get the pathname of the request (e.g. /, /protected)
const path = req.nextUrl.pathname;
const locale = typeof localStorage !== 'undefined' ? localStorage.getItem('lang') : "cn";
const res = NextResponse.next();
if (path === "/") {
return res;
}
console.log('sessionOptions:',sessionOptions);
const session = await getIronSession(req, res, sessionOptions);
const { user } = session;
console.log('path:',path);
console.log('session:',session);
if (!user?.isLoggedIn && /^\/admin(?!\/login)/.test(path)) {
return NextResponse.redirect(new URL(`/${locale}/admin/login`, req.url));
} else if (user?.isLoggedIn && /^\/admin\/?(?:login\/?)?$/.test(path)) {
return NextResponse.redirect(new URL(`/${locale}/admin/dashboard`, req.url));
}
return res;
}
export const config = {
matcher: [
/*
* Match all request paths except for the ones starting with:
* - api (API routes)
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
*/
'/((?!api|_next/static|_next/image|favicon.ico).*)',
],
};
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
TypeScript
1
https://gitee.com/xjy888/MP_WeiXin_ChatGPT.git
git@gitee.com:xjy888/MP_WeiXin_ChatGPT.git
xjy888
MP_WeiXin_ChatGPT
MP_WeiXin_ChatGPT
master

搜索帮助