From c00e5e8904eb054097973f2f01cf6255d13958f3 Mon Sep 17 00:00:00 2001 From: Dan Ribbens Date: Mon, 9 Nov 2020 14:10:37 -0500 Subject: [PATCH] rename collection auth email verification to verify and add to collection schema json --- demo/collections/Admin.js | 2 +- demo/collections/PublicUsers.js | 2 +- payload.d.ts | 2 +- src/admin/components/Routes.js | 2 +- .../views/collections/Edit/Auth/index.js | 8 +++--- .../views/collections/Edit/Default.js | 7 +++-- src/auth/operations/login.js | 2 +- src/auth/strategies/apiKey.js | 2 +- src/auth/strategies/jwt.js | 2 +- src/collections/init.js | 2 +- src/collections/operations/create.js | 4 +-- src/collections/sanitize.js | 3 +-- src/index.js | 1 - src/schema/collection.schema.json | 27 +++++++++++++++++++ src/schema/payload.schema.json | 2 +- 15 files changed, 48 insertions(+), 20 deletions(-) diff --git a/demo/collections/Admin.js b/demo/collections/Admin.js index 68533d26bc..8a9481bba8 100644 --- a/demo/collections/Admin.js +++ b/demo/collections/Admin.js @@ -21,7 +21,7 @@ module.exports = { }, auth: { tokenExpiration: 7200, // 2 hours - emailVerification: false, + verify: false, maxLoginAttempts: 5, lockTime: 600 * 1000, // lock time in ms useAPIKey: true, diff --git a/demo/collections/PublicUsers.js b/demo/collections/PublicUsers.js index 56c00ab21f..906c800e38 100644 --- a/demo/collections/PublicUsers.js +++ b/demo/collections/PublicUsers.js @@ -32,7 +32,7 @@ module.exports = { }, auth: { tokenExpiration: 300, - emailVerification: true, + verify: true, maxLoginAttempts: 5, lockTime: 600 * 1000, // lock time in ms generateVerificationUrl: (req, token) => `http://localhost:3000/api/verify?token=${token}`, diff --git a/payload.d.ts b/payload.d.ts index 23e1e30ec8..acddbecb5e 100644 --- a/payload.d.ts +++ b/payload.d.ts @@ -150,7 +150,7 @@ declare module "@payloadcms/payload/types" { }, auth?: { tokenExpiration?: number; - emailVerification?: boolean; + verify?: boolean | { generateEmailHTML: string, generateEmailSubject: string }; maxLoginAttempts?: number; lockTime?: number; useAPIKey?: boolean; diff --git a/src/admin/components/Routes.js b/src/admin/components/Routes.js index 6cb37f2d49..f059586c9d 100644 --- a/src/admin/components/Routes.js +++ b/src/admin/components/Routes.js @@ -81,7 +81,7 @@ const Routes = () => { {collections.map((collection) => { - if (collection?.auth?.emailVerification) { + if (collection?.auth?.verify) { return ( { - const { useAPIKey, requirePassword, emailVerification, collection: { slug }, email } = props; + const { useAPIKey, requirePassword, verify, collection: { slug }, email } = props; const [changingPassword, setChangingPassword] = useState(requirePassword); const { getField } = useFormFields(); const modified = useFormModified(); @@ -117,7 +117,7 @@ const Auth = (props) => { )} )} - {emailVerification && ( + {verify && ( { Auth.defaultProps = { useAPIKey: false, requirePassword: false, - emailVerification: false, + verify: false, collection: undefined, email: '', }; @@ -139,7 +139,7 @@ Auth.defaultProps = { Auth.propTypes = { useAPIKey: PropTypes.bool, requirePassword: PropTypes.bool, - emailVerification: PropTypes.bool, + verify: PropTypes.bool, collection: PropTypes.shape({ slug: PropTypes.string, }), diff --git a/src/admin/components/views/collections/Edit/Default.js b/src/admin/components/views/collections/Edit/Default.js index 5c7edb234b..bd7ee36141 100644 --- a/src/admin/components/views/collections/Edit/Default.js +++ b/src/admin/components/views/collections/Edit/Default.js @@ -93,7 +93,7 @@ const DefaultEditView = (props) => { @@ -239,7 +239,10 @@ DefaultEditView.propTypes = { timestamps: PropTypes.bool, auth: PropTypes.shape({ useAPIKey: PropTypes.bool, - emailVerification: PropTypes.bool, + verify: PropTypes.oneOfType([ + PropTypes.bool, + PropTypes.object, + ]), maxLoginAttempts: PropTypes.number, }), upload: PropTypes.shape({}), diff --git a/src/auth/operations/login.js b/src/auth/operations/login.js index a22ce707a3..70515e010a 100644 --- a/src/auth/operations/login.js +++ b/src/auth/operations/login.js @@ -33,7 +33,7 @@ async function login(args) { const userDoc = await Model.findByUsername(email); - if (!userDoc || (args.collection.config.auth.emailVerification && userDoc._verified === false)) { + if (!userDoc || (args.collection.config.auth.verify && userDoc._verified === false)) { throw new AuthenticationError(); } diff --git a/src/auth/strategies/apiKey.js b/src/auth/strategies/apiKey.js index 1f6d48474d..8fb82d01c0 100644 --- a/src/auth/strategies/apiKey.js +++ b/src/auth/strategies/apiKey.js @@ -9,7 +9,7 @@ module.exports = ({ operations }, { Model, config }) => { return new PassportAPIKey(opts, true, async (apiKey, done, req) => { try { const where = {}; - if (config.auth.emailVerification) { + if (config.auth.verify) { where.and = [ { apiKey: { diff --git a/src/auth/strategies/jwt.js b/src/auth/strategies/jwt.js index 1591fb72bc..84f56bcbe9 100644 --- a/src/auth/strategies/jwt.js +++ b/src/auth/strategies/jwt.js @@ -19,7 +19,7 @@ module.exports = ({ config, collections, operations }) => { const collection = collections[token.collection]; const where = {}; - if (collection.config.auth.emailVerification) { + if (collection.config.auth.verify) { where.and = [ { email: { diff --git a/src/collections/init.js b/src/collections/init.js index 9fb6626b79..29cc5ee447 100644 --- a/src/collections/init.js +++ b/src/collections/init.js @@ -91,7 +91,7 @@ function registerCollections() { unlock, } = this.requestHandlers.collections.auth; - if (collection.auth.emailVerification) { + if (collection.auth.verify) { router .route(`/${slug}/verify/:token`) .post(verifyEmail); diff --git a/src/collections/operations/create.js b/src/collections/operations/create.js index 776f778f23..f4cf028277 100644 --- a/src/collections/operations/create.js +++ b/src/collections/operations/create.js @@ -161,7 +161,7 @@ async function create(args) { if (data.email) { data.email = data.email.toLowerCase(); } - if (collectionConfig.auth.emailVerification) { + if (collectionConfig.auth.verify) { data._verified = false; data._verificationToken = crypto.randomBytes(20).toString('hex'); } @@ -209,7 +209,7 @@ async function create(args) { // 10. Send verification email if applicable // ///////////////////////////////////// - if (collectionConfig.auth && collectionConfig.auth.emailVerification && !disableVerificationEmail) { + if (collectionConfig.auth && collectionConfig.auth.verify && !disableVerificationEmail) { sendVerificationEmail({ config: this.config, sendEmail: this.sendEmail, diff --git a/src/collections/sanitize.js b/src/collections/sanitize.js index 95bf09abc3..d1c8cda122 100644 --- a/src/collections/sanitize.js +++ b/src/collections/sanitize.js @@ -233,7 +233,6 @@ const sanitizeCollection = (collections, collection) => { if (!sanitized.hooks.beforeLogin) sanitized.hooks.beforeLogin = []; if (!sanitized.hooks.afterLogin) sanitized.hooks.afterLogin = []; if (!collection.auth.forgotPassword) sanitized.auth.forgotPassword = {}; - if (!collection.auth.verify) sanitized.auth.verify = {}; let authFields = baseAuthFields; @@ -241,7 +240,7 @@ const sanitizeCollection = (collections, collection) => { authFields = authFields.concat(baseAPIKeyFields); } - if (collection.auth.emailVerification) { + if (collection.auth.verify) { authFields.push({ name: '_verified', type: 'checkbox', diff --git a/src/index.js b/src/index.js index 60adb79121..63cde36759 100644 --- a/src/index.js +++ b/src/index.js @@ -22,7 +22,6 @@ const buildEmail = require('./email/build'); const identifyAPI = require('./express/middleware/identifyAPI'); const errorHandler = require('./express/middleware/errorHandler'); const performFieldOperations = require('./fields/performFieldOperations'); -const validateSchema = require('./schema/validateSchema'); const localOperations = require('./collections/operations/local'); const localGlobalOperations = require('./globals/operations/local'); diff --git a/src/schema/collection.schema.json b/src/schema/collection.schema.json index 3808fbe501..520513f19f 100644 --- a/src/schema/collection.schema.json +++ b/src/schema/collection.schema.json @@ -29,6 +29,33 @@ "type": "object", "description": "Callable functions to determine permission access" }, + "auth": { + "type": "object", + "description": "Authentication properties", + "default": false, + "properties": { + "verify": { + "anyOf": [ + { + "type": "boolean" + }, + { + "type": "object", + "properties": { + "generateEmailSubject": { + "type": "string", + "description": "Subject field used when sending verify email for new user accounts" + }, + "generateEmailHTML": { + "type": "object", + "description": "Function that returns HTML for the body of the email sent to verify user accounts" + } + } + } + ] + } + } + }, "fields": { "type": "array", "description": "The attributes of the collection", diff --git a/src/schema/payload.schema.json b/src/schema/payload.schema.json index 3b873f61bb..1c6c21f549 100644 --- a/src/schema/payload.schema.json +++ b/src/schema/payload.schema.json @@ -28,7 +28,7 @@ }, "ogImage": { "type": "string", - "default": "/ok.jpg" + "description": "src url for the admin image" }, "favicon": { "type": "string",