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.

🐛 fix(auth): throw Unauthorized when no valid auth method found (#13368)

YuTengjing966f94317577
diff --git a/src/app/(backend)/middleware/auth/utils.test.ts b/src/app/(backend)/middleware/auth/utils.test.ts
index bd2c89e8c03..199fcf107bb 100644
--- a/src/app/(backend)/middleware/auth/utils.test.ts
+++ b/src/app/(backend)/middleware/auth/utils.test.ts
@@ -23,7 +23,15 @@ describe('checkAuthMethod', () => {
).not.toThrow();
});
- it('should pass with no auth params', () => {
- expect(() => checkAuthMethod({})).not.toThrow();
+ it('should throw Unauthorized with no auth params', () => {
+ expect(() => checkAuthMethod({})).toThrow();
+ });
+
+ it('should throw Unauthorized when betterAuthAuthorized is false and no apiKey', () => {
+ expect(() =>
+ checkAuthMethod({
+ betterAuthAuthorized: false,
+ }),
+ ).toThrow();
});
});
diff --git a/src/app/(backend)/middleware/auth/utils.ts b/src/app/(backend)/middleware/auth/utils.ts
index b599a0c189e..bbcea18c61f 100644
--- a/src/app/(backend)/middleware/auth/utils.ts
+++ b/src/app/(backend)/middleware/auth/utils.ts
@@ -1,3 +1,6 @@
+import { AgentRuntimeError } from '@lobechat/model-runtime';
+import { ChatErrorType } from '@lobechat/types';
+
interface CheckAuthParams {
apiKey?: string;
betterAuthAuthorized?: boolean;
@@ -10,6 +13,7 @@ interface CheckAuthParams {
* @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) => {
const { apiKey, betterAuthAuthorized } = params;
@@ -19,4 +23,6 @@ export const checkAuthMethod = (params: CheckAuthParams) => {
// if apiKey exist
if (apiKey) return;
+
+ throw AgentRuntimeError.createError(ChatErrorType.Unauthorized);
};