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) {