Comparing version 0.0.1 to 0.0.4
56
index.js
@@ -75,2 +75,3 @@ 'use strict'; | ||
req.login = function(user, cb) { | ||
req.principal = res.locals.principal = user; | ||
req.session.set('authPrincipalId', options.getUserId(user).toString(), cb); | ||
@@ -98,7 +99,3 @@ }; | ||
// Store the token in session | ||
req.session.set('authPersistenceToken', token, function(err) { | ||
if (err) return cb(err); | ||
// Return this token | ||
cb(null, token); | ||
}); | ||
req.session.set('authPersistenceToken', token, cb); | ||
}); | ||
@@ -113,2 +110,4 @@ }; | ||
req.logout = function(cb) { | ||
delete req.principal; | ||
delete res.locals.principal; | ||
// Only destroy the session if not persistent | ||
@@ -157,18 +156,16 @@ if (!persistence) | ||
* authentication identity. | ||
* | ||
* @param cb {Function} Callback | ||
*/ | ||
req.trySessionLogin = function(cb) { | ||
function trySessionLogin(req, res, next) { | ||
req.session.get('authPrincipalId', function(err, userId) { | ||
if (err) return cb(err); | ||
if (!userId) return cb(); | ||
if (err) return next(err); | ||
if (!userId) return next(); | ||
options.findUserById(userId, function(err, user) { | ||
if (err) return cb(err); | ||
if (err) return next(err); | ||
if (!user) | ||
return req.session.remove('authPrincipalId', cb); | ||
return req.session.remove('authPrincipalId', next); | ||
req.principal = res.locals.principal = user; | ||
return cb(); | ||
return next(); | ||
}); | ||
}); | ||
}; | ||
} | ||
@@ -178,13 +175,11 @@ /** | ||
* be previously set via `req.persistLogin`. | ||
* | ||
* @param cb {Function} Callback | ||
*/ | ||
req.tryPersistentLogin = function(cb) { | ||
function tryPersistentLogin(req, res, next) { | ||
function unauthenticated() { | ||
res.clearCookie(persistence.cookie.name); | ||
return cb(); | ||
return next(); | ||
} | ||
// Read data from the cookie | ||
var cookieValue = req.signedCookies[persistence.cookie.name]; | ||
if (!cookieValue) return cb(); | ||
if (!cookieValue) return next(); | ||
var userId = cookieValue.substring(0, cookieValue.indexOf(':')); | ||
@@ -196,24 +191,21 @@ var token = cookieValue.substring(cookieValue.indexOf(':') + 1); | ||
options.findUserById(userId, function(err, user) { | ||
if (err) return cb(err); | ||
if (err) return next(err); | ||
if (!user) return unauthenticated(); | ||
// See if user really owns the token | ||
persistence.hasToken(user, token, function(err, owns) { | ||
if (err) return cb(err); | ||
if (err) return next(err); | ||
if (!owns) return unauthenticated(); | ||
// Log him in | ||
req.login(user, function(err) { | ||
if (err) return cb(err); | ||
req.trySessionLogin(cb); | ||
}); | ||
req.login(user, next); | ||
}); | ||
}); | ||
}; | ||
} | ||
/** | ||
* Middleware body. | ||
*/ | ||
req.trySessionLogin(function(err) { | ||
if (err) return next(err); | ||
trySessionLogin(req, res, function(err) { | ||
if (err) | ||
return next(err); | ||
if (req.principal) | ||
return next(); | ||
if (persistence) | ||
req.tryPersistentLogin(next); | ||
tryPersistentLogin(req, res, next); | ||
else next(); | ||
@@ -220,0 +212,0 @@ }); |
{ | ||
"name": "alt-auth", | ||
"version": "0.0.1", | ||
"version": "0.0.4", | ||
"description": "Alternative Auth Middleware for Express", | ||
@@ -5,0 +5,0 @@ "dependencies": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10508
267