From 8b054e6aacb971816b2ebcdc4f2bd350763ee680 Mon Sep 17 00:00:00 2001 From: YANG QIA <2013xile@gmail.com> Date: Wed, 23 Aug 2023 18:38:57 +0800 Subject: [PATCH] fix: basic-auth compitibility issue (#2515) --- .../auth/src/server/__tests__/actions.test.ts | 21 +++++++++++++++++++ .../plugins/auth/src/server/basic-auth.ts | 9 +++++--- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/packages/plugins/auth/src/server/__tests__/actions.test.ts b/packages/plugins/auth/src/server/__tests__/actions.test.ts index 43b9ee0bb..6d3f6b20e 100644 --- a/packages/plugins/auth/src/server/__tests__/actions.test.ts +++ b/packages/plugins/auth/src/server/__tests__/actions.test.ts @@ -147,5 +147,26 @@ describe('actions', () => { }); expect(res.statusCode).toEqual(403); }); + + it('should compitible with old api', async () => { + // Create a user without username + const userRepo = db.getRepository('users'); + const email = 'test2@nocobase.com'; + const password = '1234567'; + await userRepo.create({ + values: { + email, + password, + }, + }); + const res = await agent.post('/auth:signIn').set({ 'X-Authenticator': 'basic' }).send({ + email: 'test@nocobase.com', + password: '123456', + }); + expect(res.statusCode).toEqual(200); + const data = res.body.data; + const token = data.token; + expect(token).toBeDefined(); + }); }); }); diff --git a/packages/plugins/auth/src/server/basic-auth.ts b/packages/plugins/auth/src/server/basic-auth.ts index 68502ee89..40c745368 100644 --- a/packages/plugins/auth/src/server/basic-auth.ts +++ b/packages/plugins/auth/src/server/basic-auth.ts @@ -22,10 +22,13 @@ export class BasicAuth extends BaseAuth { if (!account && !email) { ctx.throw(400, ctx.t('Please enter your username or email', { ns: namespace })); } + const filter = email + ? { email } + : { + $or: [{ username: account }, { email: account }], + }; const user = await this.userRepository.findOne({ - filter: { - $or: [{ username: account || null }, { email: account || email }], - }, + filter, }); if (!user) {