fix: basic-auth compitibility issue (#2515)

This commit is contained in:
YANG QIA 2023-08-23 18:38:57 +08:00 committed by GitHub
parent 17f438ac39
commit 8b054e6aac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 3 deletions

View File

@ -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();
});
});
});

View File

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