Files
payload/test/auth/custom-strategy/config.ts
2024-03-27 16:47:22 -04:00

82 lines
1.7 KiB
TypeScript

import type { AuthStrategyFunction } from 'payload/auth.js'
import { buildConfigWithDefaults } from '../../buildConfigWithDefaults.js'
import { usersSlug } from './shared.js'
export const strategyName = 'test-local'
const customAuthenticationStrategy: AuthStrategyFunction = async ({ headers, payload }) => {
const usersQuery = await payload.find({
collection: usersSlug,
where: {
code: {
equals: headers.get('code'),
},
secret: {
equals: headers.get('secret'),
},
},
})
const user = usersQuery.docs[0] || null
if (!user) return null
return {
...user,
_strategy: `${usersSlug}-${strategyName}`,
collection: usersSlug,
}
}
export default buildConfigWithDefaults({
admin: {
user: 'users',
},
collections: [
{
slug: usersSlug,
access: {
create: () => true,
},
auth: {
disableLocalStrategy: true,
strategies: [
{
name: strategyName,
authenticate: customAuthenticationStrategy,
},
],
},
fields: [
{
name: 'code',
type: 'text',
index: true,
label: 'Code',
unique: true,
},
{
name: 'secret',
type: 'text',
label: 'Secret',
},
{
name: 'name',
type: 'text',
label: 'Name',
},
{
name: 'roles',
type: 'select',
defaultValue: ['user'],
hasMany: true,
label: 'Role',
options: ['admin', 'editor', 'moderator', 'user', 'viewer'],
required: true,
saveToJWT: true,
},
],
},
],
})