@websolutespa/payload-plugin-bowl
Version:
Bowl PayloadCms plugin of the BOM Repository
56 lines (55 loc) • 1.56 kB
JavaScript
import { deepMerge, withCollectionHook } from '@websolutespa/payload-utils';
import { AuthenticationError } from 'payload';
import { options } from '../../options';
import { hasRole, isAdmin, isAdminOrSelf } from '../access';
import { translateCollection } from '../translations';
/**
* End User before login hook.
*
* @param req - Full express request.
* @param user - User being logged in.
* @returns The logged in user.
* @throws {AuthenticationError} If the user does not have a userRole associated.
*/ const beforeLoginHook = async ({ req, user })=>{
if (!hasRole(user, ...options.rolesUser)) {
throw new AuthenticationError();
}
return user;
};
export const UserDefault = {
access: {
create: isAdmin,
read: isAdminOrSelf,
update: isAdminOrSelf,
delete: isAdminOrSelf
},
admin: {
useAsTitle: 'email',
group: options.group.users,
defaultColumns: [
'email'
]
},
auth: {
useAPIKey: true,
lockTime: 10 * 60 * 1000,
maxLoginAttempts: 5,
tokenExpiration: 2 * 60 * 60,
verify: false
},
hooks: {
beforeLogin: [
beforeLoginHook
]
}
};
export const withUser = (config)=>{
const userConfig = deepMerge(UserDefault, config);
userConfig.fields = [
...userConfig.fields
];
withCollectionHook(userConfig, 'beforeLogin', beforeLoginHook);
translateCollection(userConfig);
return userConfig;
};
//# sourceMappingURL=withUser.js.map