[英]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
将同时处理POST
和GET
请求。 There's a couple of things to keep in mind with this approach though: 但是,使用此方法时需要牢记以下几点:
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.