Skip to content

Instantly share code, notes, and snippets.

@heitortsergent
Last active July 19, 2022 09:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save heitortsergent/8772c6009ed8704e9e8c to your computer and use it in GitHub Desktop.
Save heitortsergent/8772c6009ed8704e9e8c to your computer and use it in GitHub Desktop.
Email-confirmation Email Tutorial
app.post('/login', function(req,res) {
console.log('user email: ', req.body.email);
res.render('index', {title: 'Sent authentication email'});
});
app.get('/verify_email', function(req,res) {
console.log('verify_email token: ',req.query.token);
res.render('index', {title: 'Authenticating...'});
});
var mongoose = require('mongoose');
var dburi =
process.env.MONGOLAB_URI ||
process.env.MONGOHQ_URL ||
'mongodb://localhost/node-emailauth';
mongoose.connect(dburi, function (err, res) {
if (err) {
console.log ('ERROR connecting to: ' + dburi + '. ' + err);
} else {
console.log ('Succeeded connected to: ' + dburi);
}
});
var userSchema = new mongoose.Schema({
email: { type: String, required:true, unique:true },
authToken: { type: String, required:true, unique:true },
isAuthenticated: { type: Boolean, required:true }
});
var User = mongoose.model('User', userSchema);
app.post('/login', function(req,res) {
console.log('user email: ', req.body.email);
//generate authentication token
var seed = crypto.randomBytes(20);
var authToken = crypto.createHash('sha1').update(seed + req.body.email).digest('hex');
var newUser = new User({
email: req.body.email,
authToken: authToken,
isAuthenticated: false
});
newUser.save(function(err, newUser) {
if (err) {
return console.error(err);
}
console.dir(newUser);
});
res.render('index', {title: 'Sent authentication email'});
});
var crypto = require('crypto');
app.get('/verify_email', function(req,res) {
console.log('verify_email token: ',req.query.token);
User.findOne({ authToken: req.query.token }, function(err, user) {
if (err) { return console.error(err); }
console.dir(user);
user.isAuthenticated = true;
user.save(function (err) {
if (err) return console.error(err);
console.log('succesfully updated user');
console.log(user);
res.send(user);
});
});
res.render('index', {title: 'Authenticating...'});
});
var sendgrid = require('sendgrid’)(YOUR_SENDGRID_USERNAME, YOUR_SENDGRID_PASSWORD);
var authenticationURL = 'http://localhost:3000/verify_email?token=' + newUser.authToken;
sendgrid.send({
to: newUser.email,
from: 'youremail@example.com',
subject: 'Confirm your email',
html: '<a target=_blank href=\"' + authenticationURL + '\">Confirm your email</a>'
}, function(err, json) {
if (err) { return console.error(err); }
console.log(json);
});
sendgrid.send({
to: user.email,
from: 'youremail@example.com',
subject: 'Email confirmed!',
html: 'Awesome! We can now send you kick-ass emails'
}, function(err, json) {
if (err) { return console.error(err); }
console.log(json);
});
extends layout
block content
h1 #{title}
div.loginbox
form(name="login", action="/login", method="post")
input(type="email", name="email")
input(type="submit", value="login")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment