forked from zemirco/lockit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
201 lines (174 loc) · 4.65 KB
/
index.js
File metadata and controls
201 lines (174 loc) · 4.65 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
var path = require('path'),
events = require('events'),
util = require('util'),
express = require('express'),
chalk = require('chalk'),
extend = require('node.extend'),
Signup = require('lockit-signup'),
Login = require('lockit-login'),
ForgotPassword = require('lockit-forgot-password'),
ChangeEmail = require('lockit-change-email'),
DeleteAccount = require('lockit-delete-account'),
utils = require('lockit-utils'),
configDefault = require('./config.default.js');
/**
* Lockit constructor function.
*
* @constructor
* @param {Object} config
*/
function Lockit()
{
if(!(this instanceof Lockit))
{
return new Lockit();
}
return this;
}
util.inherits(Lockit, events.EventEmitter);
/**
* Initializer
*
* @public
*/
Lockit.prototype.init = function(config, next)
{
lockit.config = config || {};
var that = lockit;
lockit.config.that = that;
if(!lockit.config.db)
{
lockit.database();
}
if(!lockit.config.mail.emailType || !lockit.config.mail.emailSettings)
{
lockit.email();
}
// use default values for all values that aren't provided
//lockit.config = extend(true, {}, configDefault, lockit.config);
// router
lockit.router = express.Router();
// create db adapter only once and pass it to modules
var db = utils.getDatabase(lockit.config);
lockit.adapter = lockit.config.db.adapter || require(db.adapter)(lockit.config, function(err, db)
{
if(err)
{
next(err);
}
else
{
// load all required modules
that.signup = new Signup(that.config, that.adapter);
that.login = new Login(that.config, that.adapter);
that.deleteAccount = new DeleteAccount(that.config, that.adapter);
that.forgotPassword = new ForgotPassword(that.config, that.adapter);
that.changeEmail = new ChangeEmail(that.config, that.adapter);
// send all GET requests for lockit routes to '/index.html'
if(that.config.rest)
{
that.rest();
}
// expose name and email to template engine
that.router.use(function(req, res, next)
{
res.locals.name = req.user ? req.user.name || '' : '';
res.locals.email = req.user ? req.user.email || '' : '';
next();
});
// add submodule routes
that.router.use(that.signup.router);
that.router.use(that.login.router);
that.router.use(that.deleteAccount.router);
that.router.use(that.forgotPassword.router);
that.router.use(that.changeEmail.router);
// pipe events to lockit
var emitters = [that.signup, that.login, that.deleteAccount, that.forgotPassword, that.changeEmail];
utils.pipe(emitters, that);
// special event for quick start
that.signup.on('signup::post', function(user)
{
if(that.config.db.url === 'sqlite://' && that.config.db.name === ':memory:')
{
message = 'http://localhost:3000/signup/' + user.signupToken;
console.log(
chalk.bgBlack.green('lockit'),
chalk.bgBlack.yellow(message),
'cmd + double click on os x'
);
}
that.emit('signup::post', user);
});
events.EventEmitter.call(that);
next();
}
});
};
/**
* Use SQLite as fallback database.
*
* @private
*/
Lockit.prototype.database = function()
{
lockit.config.db = {
url: 'sqlite://',
name: ':memory:',
collection: 'my_user_table'
};
var message = 'no db config found. Using SQLite.';
console.log(chalk.bgBlack.green('lockit'), message);
};
/**
* Stub emails.
*
* @private
*/
Lockit.prototype.email = function()
{
var message = 'no email config found. Check your database for tokens.';
console.log(chalk.bgBlack.green('lockit'), message);
};
/**
* Send all routes to Single Page Application entry point.
*
* @private
*/
Lockit.prototype.rest = function()
{
var that = lockit;
var __parentDir = path.dirname(module.parent.filename);
var routes = [
lockit.config.signup.route,
lockit.config.signup.resendRoute,
lockit.config.signup.resendRoute + '/:token',
lockit.config.login.route,
lockit.config.login.logoutRoute,
lockit.config.login.twoFactorRoute,
lockit.config.forgotPassword.route,
lockit.config.forgotPassword.route + '/:token',
lockit.config.changeEmail.route,
lockit.config.changeEmail.route + '/:token',
lockit.config.deleteAccount.route,
];
routes.forEach(function(route)
{
that.router.get(route, function(req, res)
{
// check if user would like to render a file or use static html
if(that.config.rest.useViewEngine)
{
res.render(that.config.rest.index,
{
basedir: req.app.get('views'),
route: route
});
}
else
{
res.sendfile(path.join(__parentDir, that.config.rest.index));
}
});
});
};
var lockit = module.exports = exports = Lockit();