-
-
Save mfrancois3k/6d28444b83ea90e4077744e6fb80f38d to your computer and use it in GitHub Desktop.
Email-confirmation Email Tutorial
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var crypto = require('crypto'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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...'}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var sendgrid = require('sendgrid’)(YOUR_SENDGRID_USERNAME, YOUR_SENDGRID_PASSWORD); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var authenticationURL = 'http://localhost:3000/verify_email?token=' + newUser.authToken; | |
sendgrid.send({ | |
to: newUser.email, | |
from: '[email protected]', | |
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); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sendgrid.send({ | |
to: user.email, | |
from: '[email protected]', | |
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); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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...'}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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