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:
@@ -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],
|
||||
},
|
||||
|
||||
10
test/hooks/collections/Users/meHook.ts
Normal file
10
test/hooks/collections/Users/meHook.ts
Normal 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,
|
||||
}
|
||||
}
|
||||
}
|
||||
12
test/hooks/collections/Users/refreshHook.ts
Normal file
12
test/hooks/collections/Users/refreshHook.ts
Normal 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,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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'
|
||||
|
||||
@@ -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', () => {
|
||||
|
||||
Reference in New Issue
Block a user