Skip to content

Commit 6227276

Browse files
fix: corrects local strategy user lookup when using loginWithUsername (#7587)
## Description Fixes the local strategy user lookup. - [x] I have read and understand the [CONTRIBUTING.md](https://github.com/payloadcms/payload/blob/main/CONTRIBUTING.md) document in this repository. ## Type of change - [x] Bug fix (non-breaking change which fixes an issue) ## Checklist: - [x] I have added tests that prove my fix is effective or that my feature works - [ ] Existing test suite passes locally with my changes - [ ] I have made corresponding changes to the documentation
1 parent ee62ed6 commit 6227276

File tree

2 files changed

+86
-28
lines changed

2 files changed

+86
-28
lines changed

packages/payload/src/auth/strategies/local/register.ts

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { SanitizedCollectionConfig } from '../../../collections/config/types.js'
22
import type { JsonObject, Payload } from '../../../index.js'
3-
import type { PayloadRequest } from '../../../types/index.js'
3+
import type { PayloadRequest, Where } from '../../../types/index.js'
44

55
import { ValidationError } from '../../../errors/index.js'
66
import { generatePasswordSaltHash } from './generatePasswordSaltHash.js'
@@ -22,23 +22,43 @@ export const registerLocalStrategy = async ({
2222
}: Args): Promise<Record<string, unknown>> => {
2323
const loginWithUsername = collection?.auth?.loginWithUsername
2424

25+
let whereConstraint: Where
26+
27+
if (!loginWithUsername) {
28+
whereConstraint = {
29+
email: {
30+
equals: doc.email,
31+
},
32+
}
33+
} else {
34+
whereConstraint = {
35+
or: [],
36+
}
37+
38+
if (doc.email) {
39+
whereConstraint.or.push({
40+
email: {
41+
equals: doc.email,
42+
},
43+
})
44+
}
45+
46+
if (doc.username) {
47+
whereConstraint.or.push({
48+
username: {
49+
equals: doc.username,
50+
},
51+
})
52+
}
53+
}
54+
2555
const existingUser = await payload.find({
2656
collection: collection.slug,
2757
depth: 0,
2858
limit: 1,
2959
pagination: false,
3060
req,
31-
where: loginWithUsername
32-
? {
33-
username: {
34-
equals: doc.username,
35-
},
36-
}
37-
: {
38-
email: {
39-
equals: doc.email,
40-
},
41-
},
61+
where: whereConstraint,
4262
})
4363

4464
if (existingUser.docs.length > 0) {

test/login-with-username/int.spec.ts

Lines changed: 54 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,20 @@ describe('Login With Username Feature', () => {
1717
}
1818
})
1919

20-
describe('hook execution', () => {
21-
it('should not allow creation with neither email nor username', async () => {
22-
let errors = []
23-
try {
24-
await payload.create({
25-
collection: 'login-with-either',
26-
data: {
27-
email: null,
28-
username: null,
29-
},
30-
})
31-
} catch (error) {
32-
errors = error.data.errors
33-
}
34-
expect(errors).toHaveLength(2)
35-
})
20+
it('should not allow creation with neither email nor username', async () => {
21+
let errors = []
22+
try {
23+
await payload.create({
24+
collection: 'login-with-either',
25+
data: {
26+
email: null,
27+
username: null,
28+
},
29+
})
30+
} catch (error) {
31+
errors = error.data.errors
32+
}
33+
expect(errors).toHaveLength(2)
3634
})
3735

3836
it('should not allow removing both username and email fields', async () => {
@@ -115,4 +113,44 @@ describe('Login With Username Feature', () => {
115113
})
116114
expect(loginWithUsername).toHaveProperty('token')
117115
})
116+
117+
it('should allow mutliple creates with optional email and username', async () => {
118+
// create a user with just email
119+
await payload.create({
120+
collection: 'login-with-either',
121+
data: {
122+
email: 'email1@mail.com',
123+
password: 'test',
124+
},
125+
})
126+
127+
// create second user with just email
128+
const emailUser2 = await payload.create({
129+
collection: 'login-with-either',
130+
data: {
131+
email: 'email2@mail.com',
132+
password: 'test',
133+
},
134+
})
135+
expect(emailUser2).toHaveProperty('id')
136+
137+
// create user with just username
138+
await payload.create({
139+
collection: 'login-with-either',
140+
data: {
141+
username: 'username1',
142+
password: 'test',
143+
},
144+
})
145+
146+
// create second user with just username
147+
const usernameUser2 = await payload.create({
148+
collection: 'login-with-either',
149+
data: {
150+
username: 'username2',
151+
password: 'test',
152+
},
153+
})
154+
expect(usernameUser2).toHaveProperty('id')
155+
})
118156
})

0 commit comments

Comments
 (0)