From 2139eb410f8c95505ef7b90e35a099b0955d4e12 Mon Sep 17 00:00:00 2001 From: James Date: Fri, 22 Jan 2021 17:13:27 -0500 Subject: [PATCH] feat: auto-removes verificationToken upon manual user verify --- docs/hooks/fields.mdx | 4 ++-- .../baseFields/baseVerificationFields.ts | 19 ++++++++++++++++++- src/fields/hookPromise.ts | 6 ++---- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/docs/hooks/fields.mdx b/docs/hooks/fields.mdx index ab4a0e2781..4de353eac7 100644 --- a/docs/hooks/fields.mdx +++ b/docs/hooks/fields.mdx @@ -44,7 +44,7 @@ Example field configuration: ## Arguments and return values -All field-level hooks are formatted to accept the same arguments, although some may be `undefined` based on which field hook you are utilizing. +All field-level hooks are formatted to accept the same arguments, although some arguments may be `undefined` based on which field hook you are utilizing. Tip:
@@ -65,7 +65,7 @@ Field Hooks receive one `args` argument that contains the following properties: #### Return value -All field hooks can optionally modify the return value of the field before the operation continues. Field Hooks may optionally return the value that should be used within the field. +Field hooks **must** return the intended value for the field to be used in the operation. You can modify the return value of the field before the operation continues, or simply send the `value` that you receive through the hook's argument. If you return `undefined` within a `beforeChange` or `beforeValidate` hook, the property will be unset from its document. Important
diff --git a/src/fields/baseFields/baseVerificationFields.ts b/src/fields/baseFields/baseVerificationFields.ts index a4ec730411..253334305a 100644 --- a/src/fields/baseFields/baseVerificationFields.ts +++ b/src/fields/baseFields/baseVerificationFields.ts @@ -1,4 +1,16 @@ -import { Field } from '../config/types'; +import { Field, FieldHook } from '../config/types'; + +const autoRemoveVerificationToken: FieldHook = ({ originalDoc, data, value }) => { + // If a user manually sets `_verified` to true, + // and it was `false`, set _verificationToken to `undefined`. + // This is useful because the admin panel + // allows users to set `_verified` to true manually + if (data?._verified === true && originalDoc?._verified === false) { + return undefined; + } + + return value; +}; export default [ { @@ -17,5 +29,10 @@ export default [ name: '_verificationToken', type: 'text', hidden: true, + hooks: { + beforeChange: [ + autoRemoveVerificationToken, + ], + }, }, ] as Field[]; diff --git a/src/fields/hookPromise.ts b/src/fields/hookPromise.ts index bce1372753..7ee3b64858 100644 --- a/src/fields/hookPromise.ts +++ b/src/fields/hookPromise.ts @@ -33,11 +33,9 @@ const hookPromise = async ({ data: fullData, operation, req, - }) || data[field.name]; + }); - if (hookedValue !== undefined) { - resultingData[field.name] = hookedValue; - } + resultingData[field.name] = hookedValue; }, Promise.resolve()); } };