Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🔥 refactor(auth): remove NextAuth dead code from auth middleware (#13370)

YuTengjing70091935ba75
diff --git a/src/app/(backend)/middleware/auth/index.ts b/src/app/(backend)/middleware/auth/index.ts
index 4b69bfaac65..9f178f8eb5d 100644
--- a/src/app/(backend)/middleware/auth/index.ts
+++ b/src/app/(backend)/middleware/auth/index.ts
@@ -8,7 +8,7 @@ import { getXorPayload } from '@lobechat/utils/server';
import { auth } from '@/auth';
import { getServerDB } from '@/database/core/db-adaptor';
import { type LobeChatDatabase } from '@/database/type';
-import { LOBE_CHAT_AUTH_HEADER, LOBE_CHAT_OIDC_AUTH_HEADER, OAUTH_AUTHORIZED } from '@/envs/auth';
+import { LOBE_CHAT_AUTH_HEADER, LOBE_CHAT_OIDC_AUTH_HEADER } from '@/envs/auth';
import { extractTraceContext, injectActiveTraceHeaders } from '@/libs/observability/traceparent';
import { validateOIDCJWT } from '@/libs/oidc-provider/jwt';
import { createErrorResponse } from '@/utils/errorResponse';
@@ -53,7 +53,6 @@ export const checkAuth =
try {
// get Authorization from header
const authorization = req.headers.get(LOBE_CHAT_AUTH_HEADER);
- const oauthAuthorized = !!req.headers.get(OAUTH_AUTHORIZED);
// better auth handler
const session = await auth.api.getSession({
@@ -83,7 +82,6 @@ export const checkAuth =
checkAuthMethod({
apiKey: jwtPayload.apiKey,
betterAuthAuthorized,
- nextAuthAuthorized: oauthAuthorized,
});
} catch (e) {
const params = await options.params;
diff --git a/src/app/(backend)/middleware/auth/utils.ts b/src/app/(backend)/middleware/auth/utils.ts
index bbcea18c61f..81606607cbe 100644
--- a/src/app/(backend)/middleware/auth/utils.ts
+++ b/src/app/(backend)/middleware/auth/utils.ts
@@ -4,7 +4,6 @@ import { ChatErrorType } from '@lobechat/types';
interface CheckAuthParams {
apiKey?: string;
betterAuthAuthorized?: boolean;
- nextAuthAuthorized?: boolean;
}
/**
* Check if authentication is valid based on various auth methods.
@@ -12,7 +11,6 @@ interface CheckAuthParams {
* @param {CheckAuthParams} params - Authentication parameters extracted from headers.
* @param {string} [params.apiKey] - The user API key.
* @param {boolean} [params.betterAuthAuthorized] - Whether the Better Auth session exists.
- * @param {boolean} [params.nextAuthAuthorized] - Whether the OAuth 2 header is provided (legacy, kept for compatibility).
* @throws {AgentRuntimeError} If no valid authentication method is found.
*/
export const checkAuthMethod = (params: CheckAuthParams) => {
diff --git a/src/app/(backend)/webapi/chat/[provider]/route.test.ts b/src/app/(backend)/webapi/chat/[provider]/route.test.ts
index b050f932490..660f2cd0caf 100644
--- a/src/app/(backend)/webapi/chat/[provider]/route.test.ts
+++ b/src/app/(backend)/webapi/chat/[provider]/route.test.ts
@@ -6,7 +6,7 @@ import { getXorPayload } from '@lobechat/utils/server';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import type * as EnvsAuthModule from '@/envs/auth';
-import { LOBE_CHAT_AUTH_HEADER, OAUTH_AUTHORIZED } from '@/envs/auth';
+import { LOBE_CHAT_AUTH_HEADER } from '@/envs/auth';
import { initModelRuntimeFromDB } from '@/server/modules/ModelRuntime';
import { POST } from './route';
@@ -45,7 +45,6 @@ beforeEach(() => {
request = new Request(new URL('https://test.com'), {
headers: {
[LOBE_CHAT_AUTH_HEADER]: 'Bearer some-valid-token',
- [OAUTH_AUTHORIZED]: 'true',
},
method: 'POST',
body: JSON.stringify({ model: 'test-model' }),
diff --git a/src/envs/auth.ts b/src/envs/auth.ts
index 85361520c2b..2865cfc7f1a 100644
--- a/src/envs/auth.ts
+++ b/src/envs/auth.ts
@@ -298,5 +298,4 @@ export const authEnv = getAuthConfig();
// Auth headers and constants
export const LOBE_CHAT_AUTH_HEADER = 'X-lobe-chat-auth';
export const LOBE_CHAT_OIDC_AUTH_HEADER = 'Oidc-Auth';
-export const OAUTH_AUTHORIZED = 'X-oauth-authorized';
export const SECRET_XOR_KEY = 'LobeHub · LobeHub';
diff --git a/src/libs/better-auth/define-config.ts b/src/libs/better-auth/define-config.ts
index 45eb171744d..cf278a1040e 100644
--- a/src/libs/better-auth/define-config.ts
+++ b/src/libs/better-auth/define-config.ts
@@ -177,7 +177,7 @@ export function defineConfig(customOptions: CustomBetterAuthOptions) {
session: {
cookieCache: {
enabled: true,
- maxAge: 10 * 60, // Cache duration in seconds
+ maxAge: 2 * 60, // Cache duration in seconds
},
// Keep a DB-backed fallback when Redis secondary storage entries are unexpectedly missing.
storeSessionInDatabase: true,