feat: 🎸 Fix TOTP not load correctly

This commit is contained in:
Jin
2025-03-26 00:42:19 +01:00
parent 8f157e04d4
commit 083ee5d23b
4 changed files with 9 additions and 19 deletions

View File

@@ -25,9 +25,7 @@ const sessionParser = session({
})
});
// 创建一个检查认证状态的中间件
const checkAuthState = (req: Request, res: Response, next: NextFunction) => {
// 如果用户未登录或者是登录页面,直接继续
if (!req.session.loggedIn || req.path === '/login') {
return next();
}
@@ -35,23 +33,17 @@ const checkAuthState = (req: Request, res: Response, next: NextFunction) => {
const currentTotpStatus = totp.isTotpEnabled();
const currentSsoStatus = open_id.isOpenIDEnabled();
// 从 session 中获取上次登录时的认证状态
const lastAuthState = req.session.lastAuthState || {
totpEnabled: false,
ssoEnabled: false
};
// 检查认证状态是否发生变化
if (lastAuthState.totpEnabled !== currentTotpStatus ||
lastAuthState.ssoEnabled !== currentSsoStatus) {
// 如果认证状态发生变化,先销毁当前 session
req.session.destroy((err) => {
if (err) {
console.error('Error destroying session:', err);
}
// 清除 cookie
res.clearCookie('trilium.sid');
// 重定向到登录页面
res.redirect('/login');
});
return;
@@ -60,7 +52,6 @@ const checkAuthState = (req: Request, res: Response, next: NextFunction) => {
next();
};
// 导出一个组合的中间件
export default function (req: Request, res: Response, next: NextFunction) {
sessionParser(req, res, () => {
checkAuthState(req, res, next);