feat: added beforeLogin hook (#1289)
This commit is contained in:
@@ -3,3 +3,8 @@ export const devUser = {
|
||||
password: 'test',
|
||||
roles: ['admin'],
|
||||
};
|
||||
export const regularUser = {
|
||||
email: 'user@payloadcms.com',
|
||||
password: 'test2',
|
||||
roles: ['user'],
|
||||
};
|
||||
|
||||
46
test/hooks/collections/Users/index.ts
Normal file
46
test/hooks/collections/Users/index.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import { Payload } from '../../../../src';
|
||||
import { BeforeLoginHook, CollectionConfig } from '../../../../src/collections/config/types';
|
||||
import { AuthenticationError } from '../../../../src/errors';
|
||||
import { devUser, regularUser } from '../../../credentials';
|
||||
|
||||
const beforeLoginHook: BeforeLoginHook = ({ user }) => {
|
||||
const isAdmin = user.roles.includes('admin') ? user : undefined;
|
||||
if (!isAdmin) {
|
||||
throw new AuthenticationError();
|
||||
}
|
||||
return user;
|
||||
};
|
||||
|
||||
export const seedHooksUsers = async (payload: Payload) => {
|
||||
await payload.create({
|
||||
collection: hooksUsersSlug,
|
||||
data: devUser,
|
||||
});
|
||||
await payload.create({
|
||||
collection: hooksUsersSlug,
|
||||
data: regularUser,
|
||||
});
|
||||
};
|
||||
|
||||
export const hooksUsersSlug = 'hooks-users';
|
||||
const Users: CollectionConfig = {
|
||||
slug: hooksUsersSlug,
|
||||
auth: true,
|
||||
fields: [
|
||||
{
|
||||
name: 'roles',
|
||||
label: 'Role',
|
||||
type: 'select',
|
||||
options: ['admin', 'user'],
|
||||
defaultValue: 'user',
|
||||
required: true,
|
||||
saveToJWT: true,
|
||||
hasMany: true,
|
||||
},
|
||||
],
|
||||
hooks: {
|
||||
beforeLogin: [beforeLoginHook],
|
||||
},
|
||||
};
|
||||
|
||||
export default Users;
|
||||
@@ -3,6 +3,7 @@ import TransformHooks from './collections/Transform';
|
||||
import Hooks, { hooksSlug } from './collections/Hook';
|
||||
import NestedAfterReadHooks from './collections/NestedAfterReadHooks';
|
||||
import Relations from './collections/Relations';
|
||||
import Users, { seedHooksUsers } from './collections/Users';
|
||||
|
||||
export default buildConfig({
|
||||
collections: [
|
||||
@@ -10,8 +11,10 @@ export default buildConfig({
|
||||
Hooks,
|
||||
NestedAfterReadHooks,
|
||||
Relations,
|
||||
Users,
|
||||
],
|
||||
onInit: async (payload) => {
|
||||
await seedHooksUsers(payload);
|
||||
await payload.create({
|
||||
collection: hooksSlug,
|
||||
data: {
|
||||
|
||||
@@ -8,6 +8,9 @@ import { hooksSlug } from './collections/Hook';
|
||||
import { generatedAfterReadText, nestedAfterReadHooksSlug } from './collections/NestedAfterReadHooks';
|
||||
import { relationsSlug } from './collections/Relations';
|
||||
import type { NestedAfterReadHook } from './payload-types';
|
||||
import { hooksUsersSlug } from './collections/Users';
|
||||
import { devUser, regularUser } from '../credentials';
|
||||
import { AuthenticationError } from '../../src/errors';
|
||||
|
||||
let client: RESTClient;
|
||||
|
||||
@@ -117,4 +120,20 @@ describe('Hooks', () => {
|
||||
expect(retrievedDoc.group.subGroup.shouldPopulate.title).toEqual(relation.title);
|
||||
});
|
||||
});
|
||||
describe('auth collection hooks', () => {
|
||||
it('allow admin login', async () => {
|
||||
const { user } = await payload.login({
|
||||
collection: hooksUsersSlug,
|
||||
data: {
|
||||
email: devUser.email,
|
||||
password: devUser.password,
|
||||
},
|
||||
});
|
||||
expect(user).toBeDefined();
|
||||
});
|
||||
|
||||
it('deny user login', async () => {
|
||||
await expect(() => payload.login({ collection: hooksUsersSlug, data: { email: regularUser.email, password: regularUser.password } })).rejects.toThrow(AuthenticationError);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user