简体   繁体   English

如何在使用GraphQl和Node.js时验证用户电子邮件

[英]How to verify user email while using GraphQl and Node.js

I'm trying to implement user email verification in my pet project with GraphQL and Node.js. 我正在尝试使用GraphQL和Node.js在我的宠物项目中实施用户电子邮件验证。

I already have signUp resolver which sends verification token but I've just understood that when a user clicks a link there is no way to send data from an email to the next GraphQL resolver which would use the token and verify the email. 我已经有了signUp解析器来发送验证令牌,但是我刚刚了解到,当用户单击链接时,无法将数据从电子邮件发送到下一个使用该令牌并验证电子邮件的GraphQL解析器。

So the question is: shall I make REST endpoint /verify to do the work or there is a way to use /graphql endpoint 所以问题是:我应该让REST端点/verify来完成工作,还是可以使用/graphql端点

If you use a separate /verify endpoint, you'll most likely want to also redirect the user back to your site after processing the request. 如果您使用单独的/verify端点,则很可能在处理请求后也希望将用户重定向回您的站点。 One approach would be to effectively reverse this flow, linking to your website and then having your page make the necessary GraphQL request. 一种方法是有效逆转此流程,链接到您的网站,然后让您的页面提出必要的GraphQL请求。

Alternatively, you can invoke your verify resolver through a link in the email. 或者,您可以通过电子邮件中的链接调用verify解析器。 express-graphql will handle both POST and GET requests. express-graphql将同时处理POSTGET请求。 There's a couple of things to keep in mind with this approach though: 但是,使用此方法时需要牢记以下几点:

  • It will only work with queries, so your "verify" field will need to be on the Query type 它仅适用于查询,因此您的“验证”字段将需要在查询类型上
  • The request will work in a browser context, but will flat out fail if you call it from inside, for example, GraphiQL 该请求将在浏览器上下文中工作,但是如果您从内部调用它(例如GraphiQL),则将失败

Here's a basic example: 这是一个基本示例:

const typeDefs = `
  type Query {
    verify: Boolean # Can be any nullable scalar
  }
`
const resolvers = {
  Query: {
    verify: (root, args, ctx) => {
      // Your verification logic
      ctx.res.redirect('https://www.google.com')
    }
  }
}
const schema = makeExecutableSchema({ typeDefs, resolvers })

app.use('/graphql', graphqlHTTP((req, res) => ({
  schema: MyGraphQLSchema,
  graphiql: false,
  // Inject the response object into the context
  context: { req, res },
})))

app.listen(4000)

You can then just navigate to this url in your browser: 然后,您可以在浏览器中导航至以下网址:

http://localhost:4000/graphql?query={verify}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Node.js发送电子邮件时未处理的承诺拒绝 - Unhandled promise rejection while sending email using Node.js 如何允许用户使用 node.js 通过用户名或 email 登录 - How to allow user login by either username or email using node.js 如何使用 Bolt for JavaScript(Node.JS) 获取 Slack 机器人的当前用户 email 地址 - How to get the current user email address for Slack bot using Bolt for JavaScript(Node.JS) 如何验证node.js中的PayPal Webhooks? - How to verify PayPal Webhooks in node.js? 如何在服务器端(Node.js)以编程方式验证Facebook用户的访问令牌? - How to verify a Facebook user's access token programmatically on the server-side (Node.js)? 如何在 node.js 上使用邮戳电子邮件? - How to use postmark email on node.js? GraphQL 如何在 node.js 中将服务器错误与客户端错误(最好使用 TryCatch 块)分开? - GraphQL How to seperate Server errors from Client errors (ideally using TryCatch blocks) in node.js? 如何在使用node.js时从URL获取Id - how to get the Id from the URL while using node.js 使用 node.js 时如何将数据发送到指定连接 - How to send data to a specified connection while using node.js 如何在 node.js 中使用 bulkCreate 时检查更新和插入 - How to check update and insert while using bulkCreate in node.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM