Skip to content
This repository has been archived by the owner on Sep 28, 2021. It is now read-only.

(semver-minor) define/export error constants #105

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
var xtend = require('xtend');

var ERROR_CORS_REQUEST = 'Can not read cookies from CORS-Requests. See CORS-Workaround in the readme.';
var ERROR_NO_COOKIE_PARSER = 'cookieParser is required use require(\'cookie-parser\'), connect.cookieParser or express.cookieParser';
var ERROR_NO_SESSION = 'No session found';
var ERROR_PASSPORT_NOT_INITIALIZED = 'Passport was not initialized';
var ERROR_SESSION_STORE = 'Error in session store:\n';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The \n should probably not be in the constant.. I'll move it to the message concatenaed to it on line 79.

var ERROR_USER_NOT_AUTHORIZED = 'User not authorized through passport. (User Property not found)';
var ERROR_USER_NOT_FOUND = 'User not found';

function parseCookie(auth, cookieHeader) {
var cookieParser = auth.cookieParser(auth.secret);
var req = {
Expand Down Expand Up @@ -45,7 +53,7 @@ function authorize(options) {
try {
auth.cookieParser = require('cookie-parser');
} catch (err) {
throw new Error('cookieParser is required use require(\'cookie-parser\'), connect.cookieParser or express.cookieParser');
throw new Error(ERROR_NO_COOKIE_PARSER);
}
}

Expand All @@ -64,26 +72,26 @@ function authorize(options) {
};

if(data.xdomain && !data.sessionID)
return auth.fail(data, 'Can not read cookies from CORS-Requests. See CORS-Workaround in the readme.', false, accept);
return auth.fail(data, ERROR_CORS_REQUEST, false, accept);

auth.store.get(data.sessionID, function(err, session){
if(err)
return auth.fail(data, 'Error in session store:\n' + err.message, true, accept);
return auth.fail(data, ERROR_SESSION_STORE + err.message, true, accept);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the session store error is going to be concatenated to this constant, one must search the error for a pattern/regexp that matches this constant to know its an external error, but that is better than that external error message never being passed at all.

if(!session)
return auth.fail(data, 'No session found', false, accept);
return auth.fail(data, ERROR_NO_SESSION, false, accept);
if(!session[auth.passport._key])
return auth.fail(data, 'Passport was not initialized', true, accept);
return auth.fail(data, ERROR_PASSPORT_NOT_INITIALIZED, true, accept);

var userKey = session[auth.passport._key][auth.userProperty];

if(typeof(userKey) === 'undefined')
return auth.fail(data, 'User not authorized through passport. (User Property not found)', false, accept);
return auth.fail(data, ERROR_USER_NOT_AUTHORIZED, false, accept);

auth.passport.deserializeUser(userKey, data, function(err, user) {
if (err)
return auth.fail(data, err, true, accept);
if (!user)
return auth.fail(data, "User not found", false, accept);
return auth.fail(data, ERROR_USER_NOT_FOUND, false, accept);
data[auth.userProperty] = user;
data[auth.userProperty].logged_in = true;
auth.success(data, accept);
Expand All @@ -110,3 +118,11 @@ function filterSocketsByUser(socketIo, filter){

exports.authorize = authorize;
exports.filterSocketsByUser = filterSocketsByUser;

exports.ERROR_CORS_REQUEST = ERROR_CORS_REQUEST;
exports.ERROR_NO_COOKIE_PARSER = ERROR_NO_COOKIE_PARSER;
exports.ERROR_NO_SESSION = ERROR_NO_SESSION;
exports.ERROR_PASSPORT_NOT_INITIALIZED = ERROR_PASSPORT_NOT_INITIALIZED;
exports.ERROR_SESSION_STORE = ERROR_SESSION_STORE;
exports.ERROR_USER_NOT_AUTHORIZED = ERROR_USER_NOT_AUTHORIZED;
exports.ERROR_USER_NOT_FOUND = ERROR_USER_NOT_FOUND;