-
Notifications
You must be signed in to change notification settings - Fork 7
/
example.js
94 lines (80 loc) · 2.63 KB
/
example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
'use strict';
/**
* A simple database less email verificaiton server
*
* node example.js
*/
var port = 9876;
var express = require('express');
var app = express();
// Initialize with a 43 char base64 password. Google 'password generator'
var urlCrypt = require('url-crypt')('~{ry*I)44==yU/]9<7DPk!Hj"R#:-/Z7(hTBnlRS=4CXF');
var router = require('express').Router();
/**
* Send email verification link
*/
router.get('/emailLink/:email', function(req, res, next) {
// Encrypt your data
var payload = {
email: req.params.email.toString(),
date: new Date(),
ip: req.ip,
};
var base64 = urlCrypt.cryptObj(payload);
// make a link
var registrationUrl = 'http://' + req.headers.host + '/register/checkLink/' + base64;
// Send it by email
var message = {
message: {
to: [{email: payload.email}],
from_email: '[email protected]',
subject: 'Emailification of Your Account',
text: 'Paste the url below into your browser to Emailify! ' + registrationUrl,
html: '<a href="'+registrationUrl+'">Emailify now!</a>'
}
};
/*
var mandrill = require('node-mandrill')(config.mandrill.key);
mandrill('/messages/send', {
message: message
}, function(error, response) {
if (error) {
console.log( JSON.stringify(error) );
next(error);
}
return res.send('Email sent!');
});
*/
// Or just print to console.
console.log('');
console.log('*** /register/emailLink/:email ********************************************');
console.log(message);
return res.send('Link printed! Click it!');
});
/**
* Given a link, validate it
*/
router.get('/checkLink/:base64', function(req, res) {
var payload;
try {
payload = urlCrypt.decryptObj(req.params.base64);
} catch(e) {
// The link was mangled or tampered with.
return res.status(400).send('Bad request. Please check the link.');
}
// Payload was encrypted, so couldn't have been modified
// Only people you gave it to can give it back.
// If you emailed it to payload.email, then it's verified.
// You can cross-check the payload.ip with req.ip
// You can use payload.date to expire the verification
console.log('');
console.log('*** /register/checkLink/:base64 ********************************************');
console.log('You are verified: ' + payload.email);
console.log(payload);
return res.send('You are verified! Secret data you sent:' + JSON.stringify(payload));
});
app.use('/register', router);
app.listen(port);
console.log('Emailification at http://localhost:' + port);
console.log('');
console.log('Try http://localhost:' + port + '/register/emailLink/[email protected]');