how to do confirm email address with express/node?

前端 未结 8 994
隐瞒了意图╮
隐瞒了意图╮ 2020-12-12 09:28

Im trying to build confirm email address for users, to verify their email is real. What package should i use to confirm the email address of the user. So far Im using mongoo

8条回答
  •  清歌不尽
    2020-12-12 10:08

    if you are just testing on your local machine, one simple way of understanding how to do it can be :

    Assuming you already know sending mails through nodemailer..

    Once user signs up, after storing sign-up data in your database, on your server side take user email from sign-up data received and a random generated number and build a custom url with the address of page where user will be directed after he/she clicks on the link given in mail.

    var customUrl = "http://"+ your host + "/" + your verification web-page + "?email=" + userEmail + "&id=" + randomNumber;
    

    An example can be:

    var userEmail = someone@example.com
    var host = localhost:8080
    var directWebPage = verifyUserEmail.html
    var randomNumber = // generate with math.random() // lets say 111
    

    Putting in above format of customUrl it looks something like this

    customUrl:http://localhost:8080/verifyUserEmail.htmlemail=someone@example.com&id=111
    

    Save this customUrl somewhere (probably in your database) Now, send an email to user with email body containing this cutomUrl link.

    Click to verify your email
    

    When user clicks on the link he/she will be directed to verifyUserEmail.html page and when that happens you can extract the page url containing email and id information

    For example in angular I go like this-

    var urlVerifyData = $location.url(); or $location.absUrl();
    

    Now extract email form urlVerifyData string using javascript string methods

    Request your server with this email and urlVerifyData

    Now query your database for this email and verify previously stored customUrl with user's urlVerifyData

    If they match, hola ! You got yourself a genuine user !!!

提交回复
热议问题