feat: adds me and refresh hooks (#6968)

## Description

Duplicate of https://github.com/payloadcms/payload/pull/6965 for 2.x
This commit is contained in:
James Mikrut
2024-06-28 09:06:27 -04:00
committed by GitHub
parent cf52d64d98
commit c82d2caa29
12 changed files with 215 additions and 39 deletions

View File

@@ -7,6 +7,8 @@ import type { Payload } from '../../../../packages/payload/src/payload'
import { AuthenticationError } from '../../../../packages/payload/src/errors'
import { devUser, regularUser } from '../../../credentials'
import { afterLoginHook } from './afterLoginHook'
import { meHook } from './meHook'
import { refreshHook } from './refreshHook'
const beforeLoginHook: BeforeLoginHook = ({ req, user }) => {
const isAdmin = user.roles.includes('admin') ? user : undefined
@@ -48,6 +50,8 @@ const Users: CollectionConfig = {
},
],
hooks: {
me: [meHook],
refresh: [refreshHook],
afterLogin: [afterLoginHook],
beforeLogin: [beforeLoginHook],
},

View File

@@ -0,0 +1,10 @@
import type { MeHook } from '../../../../packages/payload/src/collections/config/types'
export const meHook: MeHook = ({ user }) => {
if (user.email === 'dontrefresh@payloadcms.com') {
return {
exp: 10000,
user,
}
}
}

View File

@@ -0,0 +1,12 @@
import type { RefreshHook } from '../../../../packages/payload/src/collections/config/types'
export const refreshHook: RefreshHook = ({ user }) => {
if (user.email === 'dontrefresh@payloadcms.com') {
return {
exp: 1,
refreshedToken: 'fake',
strategy: 'local-jwt',
user,
}
}
}

View File

@@ -1,6 +1,6 @@
import type { SanitizedConfig } from '../../packages/payload/src/config/types'
import { APIError } from '../../packages/payload/errors'
import { APIError } from '../../packages/payload/src/errors'
import { buildConfigWithDefaults } from '../buildConfigWithDefaults'
import AfterOperation from './collections/AfterOperation'
import ChainingHooks from './collections/ChainingHooks'

View File

@@ -327,6 +327,32 @@ describe('Hooks', () => {
})
describe('auth collection hooks', () => {
let hookUser
let hookUserToken
beforeAll(async () => {
const email = 'dontrefresh@payloadcms.com'
hookUser = await payload.create({
collection: hooksUsersSlug,
data: {
email,
password: devUser.password,
roles: ['admin'],
},
})
const { token } = await payload.login({
collection: hooksUsersSlug,
data: {
email: hookUser.email,
password: devUser.password,
},
})
hookUserToken = token
})
it('should call afterLogin hook', async () => {
const { user } = await payload.login({
collection: hooksUsersSlug,
@@ -354,6 +380,32 @@ describe('Hooks', () => {
}),
).rejects.toThrow(AuthenticationError)
})
it('should respect refresh hooks', async () => {
const response = await fetch(`${apiUrl}/${hooksUsersSlug}/refresh-token`, {
method: 'POST',
headers: {
Authorization: `JWT ${hookUserToken}`,
},
})
const data = await response.json()
expect(data.exp).toStrictEqual(1)
expect(data.refreshedToken).toStrictEqual('fake')
})
it('should respect me hooks', async () => {
const response = await fetch(`${apiUrl}/${hooksUsersSlug}/me`, {
headers: {
Authorization: `JWT ${hookUserToken}`,
},
})
const data = await response.json()
expect(data.exp).toStrictEqual(10000)
})
})
describe('hook parameter data', () => {