Files
payloadcms/test/login-with-username/int.spec.ts
Jarrod Flesch 1ebd54b315 feat: allows loginWithUsername to not require username (#7480)
Allows username to be optional when using the new loginWithUsername
feature. This can be done by the following:

```ts
auth: {
  loginWithUsername: {
    requireUsername: false, // <-- new property, default true
    requireEmail: false, // default: false
    allowEmailLogin: true, // default false
  },
},
```
2024-08-05 11:35:01 -04:00

119 lines
2.7 KiB
TypeScript

import type { Payload } from 'payload'
import { devUser } from '../credentials.js'
import { initPayloadInt } from '../helpers/initPayloadInt.js'
import configPromise from './config.js'
let payload: Payload
describe('Login With Username Feature', () => {
beforeAll(async () => {
;({ payload } = await initPayloadInt(configPromise))
})
afterAll(async () => {
if (typeof payload.db.destroy === 'function') {
await payload.db.destroy()
}
})
describe('hook execution', () => {
it('should not allow creation with neither email nor username', async () => {
let errors = []
try {
await payload.create({
collection: 'login-with-either',
data: {
email: null,
username: null,
},
})
} catch (error) {
errors = error.data.errors
}
expect(errors).toHaveLength(2)
})
})
it('should not allow removing both username and email fields', async () => {
const emailToUse = 'example@email.com'
const usernameToUse = 'exampleUser'
const exampleUser = await payload.create({
collection: 'login-with-either',
data: {
email: emailToUse,
username: usernameToUse,
password: 'test',
},
})
let errors = []
try {
await payload.update({
collection: 'login-with-either',
id: exampleUser.id,
data: {
email: null,
username: null,
},
})
} catch (error) {
errors = error.data.errors
}
expect(errors).toHaveLength(2)
errors = []
await payload.update({
collection: 'login-with-either',
id: exampleUser.id,
data: {
username: null,
},
})
expect(errors).toHaveLength(0)
try {
await payload.update({
collection: 'login-with-either',
id: exampleUser.id,
data: {
email: null,
},
})
} catch (error) {
errors = error.data.errors
}
expect(errors).toHaveLength(2)
})
it('should allow login with either username or email', async () => {
await payload.create({
collection: 'login-with-either',
data: {
email: devUser.email,
username: 'dev',
password: devUser.password,
},
})
const loginWithEmail = await payload.login({
collection: 'login-with-either',
data: {
email: devUser.email,
password: devUser.password,
},
})
expect(loginWithEmail).toHaveProperty('token')
const loginWithUsername = await payload.login({
collection: 'login-with-either',
data: {
username: 'dev',
password: devUser.password,
},
})
expect(loginWithUsername).toHaveProperty('token')
})
})