简体   繁体   English

我们如何构建一个节点 api 它将向用户发送邮件并生成 otp 以访问我的 python 微服务

[英]How can we build a node api which will send mail to users and will also generate otp for accessing my python microservices

Is there a way to write an email API on nodejs so that an email containing unique auto-generated OTP will be sent to the user in order to access my python micro-services? Is there a way to write an email API on nodejs so that an email containing unique auto-generated OTP will be sent to the user in order to access my python micro-services?

Nodemailer is a popular module for sending email inside nodejs. Nodemailer是一个流行的模块,用于在 nodejs 中发送 email。 You can find all the details on the website.您可以在网站上找到所有详细信息。

Alternatively, you can use 3rd party API for handling transactional emails.或者,您可以使用第 3 方 API 来处理交易电子邮件。 In my experience, SendGrid does the job and has a neat API that's easy to implement.以我的经验, SendGrid完成了这项工作,并且有一个易于实现的简洁 API。

As far as OTP goes, you can use whatever you prefer to generate the password, just never use Math.random because it's not secure.就 OTP 而言,您可以使用任何您喜欢的方式来生成密码,只是永远不要使用Math.random ,因为它不安全。 In general, this is how I would structure my app:一般来说,这就是我构建我的应用程序的方式:

  1. Generate OTP using generate-password , or however you see fit使用generate-password或您认为合适的方式生成 OTP
  2. Save the OTP hash to the database (you shouldn't store actual passwords to databases)将 OTP hash 保存到数据库中(您不应将实际密码存储到数据库中)
  3. Send the email with your link using Nodemailer or a 3rd party transactional email provider使用 Nodemailer 或第 3 方交易 email 提供程序发送 email 与您的链接
  4. Once user does what he's instructed to do, you compare the OTP hash with the hash you have saved in your database, and react accordingly一旦用户按照指示执行操作,您将 OTP hash 与您保存在数据库中的 hash 进行比较,并做出相应的反应

If you need any code examples or anything else let me know and I'll edit my answer.如果您需要任何代码示例或其他任何内容,请告诉我,我将编辑我的答案。 Hope this helps:)希望这可以帮助:)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM