-
Notifications
You must be signed in to change notification settings - Fork 48
/
index.js
152 lines (122 loc) · 3.97 KB
/
index.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
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
'use strict';
var path = require('path');
var events = require('events');
var util = require('util');
var express = require('express');
var chalk = require('chalk');
var extend = require('node.extend');
var Signup = require('lockit-signup');
var Login = require('lockit-login');
var ForgotPassword = require('lockit-forgot-password');
var DeleteAccount = require('lockit-delete-account');
var utils = require('lockit-utils');
var configDefault = require('./config.default.js');
/**
* Lockit constructor function.
*
* @constructor
* @param {Object} config
*/
var Lockit = module.exports = function(config) {
if (!(this instanceof Lockit)) {return new Lockit(config); }
this.config = config || {};
var that = this;
if (!this.config.db) {this.database(); }
if (!this.config.emailType || !this.config.emailSettings) {this.email(); }
// use default values for all values that aren't provided
this.config = extend(true, {}, configDefault, this.config);
// create db adapter only once and pass it to modules
var db = utils.getDatabase(this.config);
this.adapter = this.config.db.adapter || require(db.adapter)(this.config);
// load all required modules
var signup = new Signup(this.config, this.adapter);
var login = new Login(this.config, this.adapter);
var deleteAccount = new DeleteAccount(this.config, this.adapter);
var forgotPassword = new ForgotPassword(this.config, this.adapter);
// router
this.router = new express.Router();
// send all GET requests for lockit routes to '/index.html'
if (this.config.rest) {this.rest(); }
// expose name and email to template engine
this.router.use(function(req, res, next) {
res.locals.name = req.session.name || '';
res.locals.email = req.session.email || '';
next();
});
// add submodule routes
this.router.use(signup.router);
this.router.use(login.router);
this.router.use(deleteAccount.router);
this.router.use(forgotPassword.router);
// pipe events to lockit
var emitters = [signup, login, deleteAccount, forgotPassword];
utils.pipe(emitters, that);
// special event for quick start
signup.on('signup::post', function(user) {
if (that.config.db.url === 'sqlite://' && that.config.db.name === ':memory:') {
var 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(this);
};
util.inherits(Lockit, events.EventEmitter);
/**
* Use SQLite as fallback database.
*
* @private
*/
Lockit.prototype.database = function() {
this.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 = this;
var parentDir = path.dirname(module.parent.filename);
var routes = [
this.config.signup.route,
this.config.signup.route + '/resend-verification',
this.config.signup.route + '/:token',
this.config.login.route,
this.config.login.logoutRoute,
this.config.forgotPassword.route,
this.config.forgotPassword.route + '/:token',
this.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')
});
} else {
res.sendfile(path.join(parentDir, that.config.rest.index));
}
});
});
};