This repository was archived by the owner on Mar 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
129 lines (111 loc) · 3.91 KB
/
index.js
File metadata and controls
129 lines (111 loc) · 3.91 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
var express = require('express'),
session = require('express-session'),
flash = require('connect-flash'),
cookieParser = require('cookie-parser'),
bodyParser = require('body-parser'),
winston = require('winston'),
// Services:
UserStore = require('../../tests/fakes/userStore'),
TokenStore = require('../../tests/fakes/tokenStore'),
emailService = require('../fakes/emailService'),
// Main lib:
localAuthFactory = require('../../src/index');
var logger = new (winston.Logger)({
transports: [
new (winston.transports.Console)({ level: 'debug' })
]
});
var app = express(),
port = process.env.PORT || 3000;
app.use(express.static(__dirname + '/public'));
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(cookieParser());
// TODO: Use proper security settings with HTTPS
app.use(session({
secret: 'keyboard cat',
resave: false,
saveUninitialized: false
}));
app.use(flash());
var services = {
emailService: emailService,
userStore: new UserStore(),
passwordResetTokenStore: new TokenStore(),
verifyEmailTokenStore: new TokenStore(),
logger: logger
};
var localAuth = localAuthFactory(app, services, {
failedLoginsBeforeLockout: 3,
accountLockedMs: 1000 * 20, // 20 seconds for sample app
verifyEmail: true
});
app.use(function(req, res, next) {
// Transfer flash state, if present, to locals so views can access:
res.locals.errors = (res.locals.errors || []).concat(req.flash('errors'));
res.locals.validationErrors = (res.locals.validationErrors || []).concat(req.flash('validationErrors'));
res.locals.successMsgs = (res.locals.successMsgs || []).concat(req.flash('successMsgs'));
next();
});
// ------------------------------------------------------------
app.get('/login', function(req, res) {
res.render('login');
});
app.post('/login', localAuth.login(), function(req, res) {
res.redirect('/home');
});
app.get('/logout', localAuth.logout(), function(req, res) {
res.redirect('/login');
});
app.get('/register', function(req, res) {
res.render('register');
});
app.post('/register', localAuth.register(), function(req, res) {
req.flash('successMsgs', 'Registered successfully');
res.redirect('/home');
});
app.get('/verifyemail', localAuth.verifyEmailView(), function(req, res) {
res.render('email_verification', { emailVerified: res.statusCode == 200 });
});
app.post('/unregister', localAuth.unregister(), function(req, res) {
req.flash('successMsgs', 'Unregistered successfully');
res.redirect('/register');
});
app.get('/forgotpassword', function(req, res) {
res.render('forgot_password');
});
app.post('/forgotpassword', localAuth.forgotPassword(), function(req, res) {
res.render('password_reset_requested', { email: res.locals.email });
});
app.get('/resetpassword', localAuth.resetPasswordView(), function(req, res) {
res.render('reset_password');
});
app.post('/resetpassword', localAuth.resetPassword(), function(req, res) {
req.flash('successMsgs', 'Your password has been reset');
res.redirect('/login');
});
app.get('/changepassword', function(req, res) {
res.render('change_password');
});
app.post('/changepassword', localAuth.changePassword(), function(req, res) {
req.flash('successMsgs', 'Your password has been changed');
res.redirect('/home');
});
// ------------------------------------------------------------
// App Specific Routes:
app.get('/', function(req, res) {
res.redirect('/home');
});
app.get('/home', localAuth.ensureAuthenticated(), function(req, res) {
res.render('home', { user: req.user, newUser: req.param('newUser') });
});
// ------------------------------------------------------------
app.use(function(err, req, res, next) {
logger.error(err);
res.status(500).render('error');
});
app.listen(port);
console.info('Running on port', port);