feat: disableLocalStrategy with auth fields still enabled (#9579)

Adds configuration options to `auth.disableLocalStrategy` to allow
customization of how payload treats an auth enabled collection.

Two new properties have been added to `disableLocalStrategy`:

- `enableFields` Include auth fields on the collection even though the
local strategy is disabled. Useful when you do not want the database or
types to vary depending on the auth configuration used.
- `optionalPassword`: makes the password field not required
This commit is contained in:
Dan Ribbens
2024-12-03 09:52:23 -05:00
committed by GitHub
parent 40f5c72e8d
commit 6104fe5011
16 changed files with 256 additions and 41 deletions

View File

@@ -1,4 +1,4 @@
import type { Payload, User } from 'payload'
import type { FieldAffectingData, Payload, User } from 'payload'
import { jwtDecode } from 'jwt-decode'
import path from 'path'
@@ -9,7 +9,13 @@ import type { NextRESTClient } from '../helpers/NextRESTClient.js'
import { devUser } from '../credentials.js'
import { initPayloadInt } from '../helpers/initPayloadInt.js'
import { apiKeysSlug, namedSaveToJWTValue, saveToJWTKey, slug } from './shared.js'
import {
apiKeysSlug,
namedSaveToJWTValue,
partialDisableLocaleStrategiesSlug,
saveToJWTKey,
slug,
} from './shared.js'
let restClient: NextRESTClient
let payload: Payload
@@ -709,6 +715,70 @@ describe('Auth', () => {
})
})
describe('disableLocalStrategy', () => {
it('should allow create of a user with disableLocalStrategy', async () => {
const email = 'test@example.com'
const user = await payload.create({
collection: partialDisableLocaleStrategiesSlug,
data: {
email,
// password is not required
},
})
expect(user.email).toStrictEqual(email)
})
it('should retain fields when auth.disableLocalStrategy.enableFields is true', () => {
const authFields = payload.collections[partialDisableLocaleStrategiesSlug].config.fields
// eslint-disable-next-line jest/no-conditional-in-test
.filter((field) => 'name' in field && field.name)
.map((field) => (field as FieldAffectingData).name)
expect(authFields).toMatchObject([
'updatedAt',
'createdAt',
'email',
'resetPasswordToken',
'resetPasswordExpiration',
'salt',
'hash',
'loginAttempts',
'lockUntil',
])
})
it('should prevent login of user with disableLocalStrategy.', async () => {
await payload.create({
collection: partialDisableLocaleStrategiesSlug,
data: {
email: devUser.email,
password: devUser.password,
},
})
await expect(async () => {
await payload.login({
collection: partialDisableLocaleStrategiesSlug,
data: {
email: devUser.email,
password: devUser.password,
},
})
}).rejects.toThrow('You are not allowed to perform this action.')
})
it('rest - should prevent login', async () => {
const response = await restClient.POST(`/${partialDisableLocaleStrategiesSlug}/login`, {
body: JSON.stringify({
email,
password,
}),
})
expect(response.status).toBe(403)
})
})
describe('API Key', () => {
it('should authenticate via the correct API key user', async () => {
const usersQuery = await payload.find({