Files
payload/test/auth/custom-strategy/config.ts
Elliot DeNolf 7309d474ee feat!: type auto-generation (#6657)
Types are now auto-generated by default.

You can opt-out of this behavior by setting:
```ts
buildConfig({
  // Rest of config
  typescript: {
    autoGenerate: false
  },
})
```
2024-06-10 13:42:44 -04:00

91 lines
1.9 KiB
TypeScript

import { fileURLToPath } from 'node:url'
import path from 'path'
const filename = fileURLToPath(import.meta.url)
const dirname = path.dirname(filename)
import type { AuthStrategyFunction } from 'payload/auth'
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,
},
],
},
],
typescript: {
outputFile: path.resolve(dirname, 'payload-types.ts'),
},
})