diff --git a/lib/auth_middleware.js b/lib/auth_middleware.js index 6f7e32b..08fbf9d 100644 --- a/lib/auth_middleware.js +++ b/lib/auth_middleware.js @@ -81,7 +81,7 @@ module.exports = function(optionsOrStrategy) { middlewareCallback= scope; scope= undefined; } - RequestMethods.logout.call( this, {scope:scope, request:req, response:res}, logoutHandler, function() { + RequestMethods.logout.call( this, {scope:scope, request:req, response:res}, logoutHandler, function(err) { //Clear out the saved auth details //TODO: this should be scope-aware. createAuthDetails( req ); @@ -90,7 +90,7 @@ module.exports = function(optionsOrStrategy) { traceFunction( message, {scope:scope, request:req, response:res}, linePrefix ); }; - if( middlewareCallback) middlewareCallback(); + if( middlewareCallback) middlewareCallback(err); }) }; diff --git a/lib/requestMethods.js b/lib/requestMethods.js index 478c810..492fb1e 100644 --- a/lib/requestMethods.js +++ b/lib/requestMethods.js @@ -1,3 +1,4 @@ + /*! * Copyright(c) 2010 Ciaran Jessup * MIT Licensed @@ -155,17 +156,21 @@ module.exports.isUnAuthenticated= function(scope) { }; module.exports.logout= function( authContext, logoutHandler, middlewareCallback ) { - var ad= this.getAuthDetails(); - ad.trace( "Logout", authContext.scope, "!!!" ); - var user; - if( authContext.scope === undefined) { - user= ad.user; - delete ad.user; - ad.scopedUsers= {}; - } - else { - user= ad.scopedUsers[authContext.scope].user; - delete ad.scopedUsers[authContext.scope].user; - } - logoutHandler( authContext, user, middlewareCallback ); + var user, err; + var ad= this.getAuthDetails(); + ad.trace( "Logout", authContext.scope, "!!!" ); + if( authContext.scope === undefined) { + user= ad.user; + delete ad.user; + ad.scopedUsers= {}; + } + else { + try { + user= ad.scopedUsers[authContext.scope].user; + delete ad.scopedUsers[authContext.scope].user; + } catch(error){ + err= new Error('There are no user credentials associated with the scope: "' + authContext.scope + '"'); + } + } + logoutHandler( authContext, user, middlewareCallback(err)); }; \ No newline at end of file