-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 2005856
Showing
12 changed files
with
270 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
SENDGRID_USERNAME=YOUR_USERNAME | ||
SENDGRID_PASSWORD=YOUR_PASSWORD |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Logs | ||
logs | ||
*.log | ||
|
||
# Runtime data | ||
pids | ||
*.pid | ||
*.seed | ||
|
||
# Directory for instrumented libs generated by jscoverage/JSCover | ||
lib-cov | ||
|
||
# Coverage directory used by tools like istanbul | ||
coverage | ||
|
||
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) | ||
.grunt | ||
|
||
# Compiled binary addons (http://nodejs.org/api/addons.html) | ||
build/Release | ||
|
||
# Dependency directory | ||
# Commenting this out is preferred by some people, see | ||
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git- | ||
node_modules | ||
|
||
# Users Environment Variables | ||
.lock-wscript | ||
|
||
.env |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Email-confirmation email app | ||
|
||
`cp .env.example .env` | ||
|
||
Add your credentials to .env | ||
|
||
`mongod` | ||
|
||
`npm start` |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
var express = require('express'), | ||
path = require('path'), | ||
favicon = require('static-favicon'), | ||
logger = require('morgan'), | ||
cookieParser = require('cookie-parser'), | ||
bodyParser = require('body-parser'), | ||
mongoose = require('mongoose'), | ||
crypto = require('crypto'), | ||
dotenv = require('dotenv'); | ||
|
||
dotenv.load(); | ||
|
||
var sendgrid = require('sendgrid')(process.env.SENDGRID_USERNAME, process.env.SENDGRID_PASSWORD); | ||
|
||
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); | ||
|
||
var routes = require('./routes/index'); | ||
var users = require('./routes/users'); | ||
|
||
var app = express(); | ||
|
||
// view engine setup | ||
app.set('views', path.join(__dirname, 'views')); | ||
app.set('view engine', 'jade'); | ||
|
||
app.use(favicon()); | ||
app.use(logger('dev')); | ||
app.use(bodyParser.json()); | ||
app.use(bodyParser.urlencoded()); | ||
app.use(cookieParser()); | ||
app.use(express.static(path.join(__dirname, 'public'))); | ||
|
||
app.use('/', routes); | ||
app.use('/users', users); | ||
|
||
app.post('/login', function(req,res) { | ||
console.log(req.body); | ||
|
||
//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); | ||
|
||
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); | ||
}); | ||
}); | ||
|
||
res.render('index', {title: 'Sent authentication email'}); | ||
}); | ||
|
||
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); | ||
|
||
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); | ||
}); | ||
|
||
res.send(user); | ||
|
||
//update page | ||
}); | ||
}); | ||
|
||
res.render('index', {title: 'Authenticating...'}); | ||
}); | ||
|
||
/// catch 404 and forward to error handler | ||
app.use(function(req, res, next) { | ||
var err = new Error('Not Found'); | ||
err.status = 404; | ||
next(err); | ||
}); | ||
|
||
/// error handlers | ||
|
||
// development error handler | ||
// will print stacktrace | ||
if (app.get('env') === 'development') { | ||
app.use(function(err, req, res, next) { | ||
res.status(err.status || 500); | ||
res.render('error', { | ||
message: err.message, | ||
error: err | ||
}); | ||
}); | ||
} | ||
|
||
// production error handler | ||
// no stacktraces leaked to user | ||
app.use(function(err, req, res, next) { | ||
res.status(err.status || 500); | ||
res.render('error', { | ||
message: err.message, | ||
error: {} | ||
}); | ||
}); | ||
|
||
|
||
module.exports = app; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#!/usr/bin/env node | ||
var debug = require('debug')('node-emailauth'); | ||
var app = require('../app'); | ||
|
||
app.set('port', process.env.PORT || 3000); | ||
|
||
var server = app.listen(app.get('port'), function() { | ||
debug('Express server listening on port ' + server.address().port); | ||
}); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"name": "node-emailauth", | ||
"version": "0.0.1", | ||
"private": true, | ||
"scripts": { | ||
"start": "node ./bin/www" | ||
}, | ||
"dependencies": { | ||
"body-parser": "~1.0.0", | ||
"cookie-parser": "~1.0.1", | ||
"debug": "~0.7.4", | ||
"dotenv": "^0.4.0", | ||
"express": "~4.2.0", | ||
"jade": "~1.3.0", | ||
"mongoose": "^3.8.15", | ||
"morgan": "~1.0.0", | ||
"sendgrid": "^1.2.0", | ||
"static-favicon": "~1.0.0" | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
body { | ||
padding: 50px; | ||
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif; | ||
} | ||
|
||
a { | ||
color: #00B7FF; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
var express = require('express'); | ||
var router = express.Router(); | ||
|
||
/* GET home page. */ | ||
router.get('/', function(req, res) { | ||
res.render('index', { title: 'Email Confirmation Authentication' }); | ||
}); | ||
|
||
module.exports = router; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
var express = require('express'); | ||
var router = express.Router(); | ||
|
||
/* GET users listing. */ | ||
router.get('/', function(req, res) { | ||
res.send('respond with a resource'); | ||
}); | ||
|
||
module.exports = router; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
extends layout | ||
|
||
block content | ||
h1= message | ||
h2= error.status | ||
pre #{error.stack} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
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") |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
doctype html | ||
html | ||
head | ||
title= title | ||
link(rel='stylesheet', href='/stylesheets/style.css') | ||
body | ||
block content |