How to encrypt field level using payload? #1435
Answered
by
DanRibbens
kalib-code
asked this question in
Q&A
-
I am new to payload and I was hoping if payload can handle encrypt field level to DB and decrypt on payload UI |
Beta Was this translation helpful? Give feedback.
Answered by
DanRibbens
Nov 17, 2022
Replies: 1 comment 1 reply
-
Here is an example using Payload's own ecrypt and decrypt functions. // encryptionHooks.ts
import { FieldHook } from 'payload/types';
const encryptKey: FieldHook = ({ req, value }) => (value ? req.payload.encrypt(value as string) : undefined);
const decryptKey: FieldHook = ({ req, value }) => (value ? req.payload.decrypt(value as string) : undefined);
const encryptionHooks = {
beforeChange: [
encryptKey,
],
afterRead: [
decryptKey,
],
};
export default encryptionHooks; Example usage in a field: import encryptionHooks from '../../fields/encryptionHooks';
const Customers: CollectionConfig = {
slug: 'customers',
fields: [
{
name: 'ssn',
label: 'SSN',
type: 'text',
hooks: encryptionHooks,
},
],
}; |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
kalib-code
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here is an example using Payload's own ecrypt and decrypt functions.
Example usage in a field: