-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathpassport.js
55 lines (50 loc) · 1.49 KB
/
passport.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
/* jshint node: true */
'use strict';
var passport = require('passport'),
LocalStrategy = require('passport-local').Strategy,
smfAuth = require('./integration/smf-auth'),
config = require('./config');
var users = {};
// Passport session setup.
// To support persistent login sessions, Passport needs to be able to
// serialize users into and deserialize users out of the session. Typically,
// this will be as simple as storing the user ID when serializing, and finding
// the user by ID when deserializing.
passport.serializeUser(function(user, done) {
done(null, user.username);
});
passport.deserializeUser(function(id, done) {
var user = users[id];
if (user) { done(null, user); }
else { done(null, false); }
});
passport.use(new LocalStrategy(
function(username, password, done) {
process.nextTick(function () {
if (config.fakeAuth.enabled) {
users[username] = {
username: username,
userId: config.fakeAuth.userId,
email: config.fakeAuth.email,
admin: config.fakeAuth.admin
};
return done(null, users[username]);
}
else {
smfAuth.authenticate(username, password, function(err, user) {
if (err) {
return done(err);
}
if (!user) {
return done(null, false);
}
else {
users[user.username] = user;
return done(null, user);
}
});
}
});
}
));
module.exports = passport;