From 87ccdae795fa56ab4cf4c9167b6ca6326c86a71e Mon Sep 17 00:00:00 2001 From: Elliot DeNolf Date: Thu, 24 Sep 2020 16:39:38 -0400 Subject: [PATCH] implement verify graphql resolver --- src/auth/graphql/resolvers/verifyEmail.js | 23 +++++++++++++++++++++++ src/auth/operations/verifyEmail.js | 1 + src/collections/graphql/init.js | 10 +++++++++- src/init/bindResolvers.js | 4 ++-- 4 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 src/auth/graphql/resolvers/verifyEmail.js diff --git a/src/auth/graphql/resolvers/verifyEmail.js b/src/auth/graphql/resolvers/verifyEmail.js new file mode 100644 index 0000000000..727ec17af6 --- /dev/null +++ b/src/auth/graphql/resolvers/verifyEmail.js @@ -0,0 +1,23 @@ +/* eslint-disable no-param-reassign */ +function verifyEmail(collection) { + async function resolver(_, args, context) { + if (args.locale) context.req.locale = args.locale; + if (args.fallbackLocale) context.req.fallbackLocale = args.fallbackLocale; + + const options = { + collection, + token: args.token, + req: context.req, + res: context.res, + api: 'GraphQL', + }; + + const success = await this.operations.collections.auth.verifyEmail(options); + return success; + } + + const verifyEmailResolver = resolver.bind(this); + return verifyEmailResolver; +} + +module.exports = verifyEmail; diff --git a/src/auth/operations/verifyEmail.js b/src/auth/operations/verifyEmail.js index 2d73a18593..1f9d3cd6fd 100644 --- a/src/auth/operations/verifyEmail.js +++ b/src/auth/operations/verifyEmail.js @@ -20,6 +20,7 @@ async function verifyEmail(args) { user._verified = true; await user.save(); + return true; } module.exports = verifyEmail; diff --git a/src/collections/graphql/init.js b/src/collections/graphql/init.js index 305a11e38d..3f3cbb0e68 100644 --- a/src/collections/graphql/init.js +++ b/src/collections/graphql/init.js @@ -16,7 +16,7 @@ function registerCollections() { } = this.graphQL.resolvers.collections; const { - login, logout, me, init, refresh, forgotPassword, resetPassword, + login, logout, me, init, refresh, forgotPassword, resetPassword, verifyEmail, } = this.graphQL.resolvers.collections.auth; Object.keys(this.collections).forEach((slug) => { @@ -266,6 +266,14 @@ function registerCollections() { resolve: resetPassword(collection), }; + this.Mutation.fields[`verifyEmail${singularLabel}`] = { + type: GraphQLBoolean, + args: { + token: { type: GraphQLString }, + }, + resolve: verifyEmail(collection), + }; + this.Mutation.fields[`refreshToken${singularLabel}`] = { type: new GraphQLObjectType({ name: formatName(`${slug}Refreshed${singularLabel}`), diff --git a/src/init/bindResolvers.js b/src/init/bindResolvers.js index 72fa8e1fb9..3ca3e10ce5 100644 --- a/src/init/bindResolvers.js +++ b/src/init/bindResolvers.js @@ -6,7 +6,7 @@ const logout = require('../auth/graphql/resolvers/logout'); const me = require('../auth/graphql/resolvers/me'); const refresh = require('../auth/graphql/resolvers/refresh'); const resetPassword = require('../auth/graphql/resolvers/resetPassword'); -// const verifyEmail = require('../auth/resolvers/verifyEmail'); +const verifyEmail = require('../auth/graphql/resolvers/verifyEmail'); const create = require('../collections/graphql/resolvers/create'); const find = require('../collections/graphql/resolvers/find'); @@ -37,7 +37,7 @@ function bindResolvers(ctx) { me: me.bind(ctx), refresh: refresh.bind(ctx), resetPassword: resetPassword.bind(ctx), - // verifyEmail: verifyEmail.bind(ctx), + verifyEmail: verifyEmail.bind(ctx), }, }, globals: {