forked from gara-MI/everyauth
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
165 lines (144 loc) · 4.55 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
153
154
155
156
157
158
159
160
161
162
163
164
165
var path = require('path');
var EventEmitter = require('events').EventEmitter;
var express = require('express');
var ExpressRouter = require('express/lib/router');
var merge = require('./lib/utils').merge;
var everyauth = module.exports = {};
everyauth.helpExpress = function () {
console.warn('everyauth.helpExpress is being deprecated. helpExpress is now automatically invoked when it detects express. So remove everyauth.helpExpress from your code');
return this;
};
everyauth.debug = false;
// The connect middleware. e.g.,
// connect(
// ...
// , everyauth.middleware()
// , ...
// )
everyauth.middleware = function (opts) {
opts = merge({
autoSetupRoutes: true
}, opts);
var userAlias = everyauth.expressHelperUserAlias || 'user';
var router = new ExpressRouter;
if (opts.autoSetupRoutes) {
var router = new ExpressRouter();
var modules = everyauth.enabled;
for (var _name in modules) {
var _module = modules[_name];
_module.validateSequences();
_module.routeApp(router);
}
}
return function (req, res, next) {
fetchUserFromSession(req, function (err) {
addRequestLocals(req, res, userAlias);
registerReqGettersAndMethods(req);
if (router) {
router.handle(req, res, next);
} else {
next();
}
});
}
};
function addRequestLocals (req, res, userAlias) {
if (res.locals) {
var session = req.session;
var auth = session && session.auth;
var everyauthLocal = merge(auth, {
loggedIn: !! (auth && auth.loggedIn)
, user: req.user
});
if (everyauth.enabled.password) {
// Add in access to loginFormFieldName() + passwordFormFieldName()
everyauthLocal.password || (everyauthLocal.password = {});
everyauthLocal.password.loginFormFieldName = everyauth.password.loginFormFieldName();
everyauthLocal.password.passwordFormFieldName = everyauth.password.passwordFormFieldName();
}
res.locals.everyauth = everyauthLocal;
res.locals[userAlias] = req.user;
}
}
function registerReqGettersAndMethods (req) {
var methods = everyauth._req._methods
var getters = everyauth._req._getters;
for (var name in methods) {
req[name] = methods[name];
}
for (name in getters) {
Object.defineProperty(req, name, {
get: getters[name]
});
}
}
function fetchUserFromSession (req, callback) {
var session = req.session
var auth = session && session.auth;
if (!auth || !auth.userId) return callback();
var everymodule = everyauth.everymodule;
// var pause = __pause(req);
req.pause();
var findUserById_function = everymodule.findUserById();
findUserById_function.length === 3
? findUserById_function( req, auth.userId, findUserById_callback )
: findUserById_function( auth.userId, findUserById_callback );
function findUserById_callback (err, user) {
if (err) {
return callback(err);
}
if (user) req.user = user;
else delete session.auth;
callback();
}
}
everyauth._req = {
_methods: {}
, _getters: {}
};
everyauth.addRequestMethod = function (name, fn) {
this._req._methods[name] = fn;
return this;
};
everyauth.addRequestGetter = function (name, fn, isAsync) {
this._req._getters[name] = fn;
return this;
};
everyauth
.addRequestMethod('logout', function () {
var req = this;
delete req.session.auth;
})
.addRequestGetter('loggedIn', function () {
var req = this;
return !!(req.session && req.session.auth && req.session.auth.loggedIn);
});
everyauth.modules = {};
everyauth.enabled = {};
// Grab all filenames in ./modules -- They correspond to the modules of the same name
// as the filename (minus '.js')
var fs = require('fs');
var files = fs.readdirSync(__dirname + '/lib/modules').filter( function (file) {
return path.extname(file) === '.js';
});
var includeModules = files.map( function (fname) {
return path.basename(fname, '.js');
});
for (var i = 0, l = includeModules.length; i < l; i++) {
var name = includeModules[i];
// Lazy enabling of a module via `everyauth` getters
// i.e., the `facebook` module is not loaded into memory
// until `everyauth.facebook` is evaluated
Object.defineProperty(everyauth, name, {
get: (function (name) {
return function () {
var mod = this.modules[name] || (this.modules[name] = require('./lib/modules/' + name));
// Make `everyauth` accessible from each auth strategy module
if (!mod.everyauth) mod.everyauth = this;
if (mod.shouldSetup)
this.enabled[name] = mod;
return mod;
}
})(name)
});
};