Skip to content

Commit

Permalink
Added eslint configuration, and fixed a pletora of lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Iyanu-Tomiwa committed Sep 15, 2018
1 parent 424905b commit d53ff7d
Show file tree
Hide file tree
Showing 37 changed files with 5,391 additions and 3,367 deletions.
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.git
coverage/
node_modules/
24 changes: 24 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module.exports = {
env: {
// jest: true,
mocha: true,
node: true,
},
extends: [
'airbnb-base',
],
plugins: [
// 'jest'
],
rules: {
"comma-dangle": 0,
"no-underscore-dangle": 0,
"no-param-reassign": 0,
"prefer-destructuring": 0,
// 'jest/no-disabled-tests': [2],
// 'jest/no-focused-tests': [2],
// 'jest/no-identical-title': [2],
// 'jest/prefer-to-have-length': [2],
// 'jest/valid-expect': [2],
}
};
148 changes: 82 additions & 66 deletions lib/authenticator.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
/**
* Module dependencies.
*/
var SessionStrategy = require('./strategies/session')
, SessionManager = require('./sessionmanager');

/* eslint-disable no-underscore-dangle, camelcase, no-proto, no-shadow */

const SessionStrategy = require('./strategies/session');
const SessionManager = require('./sessionmanager');
const connect = require('./framework/connect');


/**
Expand All @@ -18,7 +22,7 @@ function Authenticator() {
this._infoTransformers = [];
this._framework = null;
this._userProperty = 'user';

this.init();
}

Expand All @@ -27,8 +31,8 @@ function Authenticator() {
*
* @api protected
*/
Authenticator.prototype.init = function() {
this.framework(require('./framework/connect')());
Authenticator.prototype.init = function init() {
this.framework(connect());
this.use(new SessionStrategy(this.deserializeUser.bind(this)));
this._sm = new SessionManager({ key: this._key }, this.serializeUser.bind(this));
};
Expand All @@ -48,13 +52,13 @@ Authenticator.prototype.init = function() {
* @return {Authenticator} for chaining
* @api public
*/
Authenticator.prototype.use = function(name, strategy) {
Authenticator.prototype.use = function use(name, strategy) {
if (!strategy) {
strategy = name;
name = strategy.name;
}
if (!name) { throw new Error('Authentication strategies must have a name'); }

this._strategies[name] = strategy;
return this;
};
Expand All @@ -78,7 +82,7 @@ Authenticator.prototype.use = function(name, strategy) {
* @return {Authenticator} for chaining
* @api public
*/
Authenticator.prototype.unuse = function(name) {
Authenticator.prototype.unuse = function unuse(name) {
delete this._strategies[name];
return this;
};
Expand All @@ -102,7 +106,7 @@ Authenticator.prototype.unuse = function(name) {
* @return {Authenticator} for chaining
* @api public
*/
Authenticator.prototype.framework = function(fw) {
Authenticator.prototype.framework = function framework(fw) {
this._framework = fw;
return this;
};
Expand All @@ -126,10 +130,10 @@ Authenticator.prototype.framework = function(fw) {
* @return {Function} middleware
* @api public
*/
Authenticator.prototype.initialize = function(options) {
Authenticator.prototype.initialize = function initialize(options) {
options = options || {};
this._userProperty = options.userProperty || 'user';

return this._framework.initialize(this, options);
};

Expand All @@ -139,7 +143,10 @@ Authenticator.prototype.initialize = function(options) {
*
* Examples:
*
* passport.authenticate('local', { successRedirect: '/', failureRedirect: '/login' })(req, res);
* passport.authenticate('local', {
* successRedirect: '/',
* failureRedirect: '/login'
* })(req, res);
*
* passport.authenticate('local', function(err, user) {
* if (!user) { return res.redirect('/login'); }
Expand All @@ -161,7 +168,7 @@ Authenticator.prototype.initialize = function(options) {
* @return {Function} middleware
* @api public
*/
Authenticator.prototype.authenticate = function(strategy, options, callback) {
Authenticator.prototype.authenticate = function authenticate(strategy, options, callback) {
return this._framework.authenticate(this, strategy, options, callback);
};

Expand All @@ -185,11 +192,11 @@ Authenticator.prototype.authenticate = function(strategy, options, callback) {
* @return {Function} middleware
* @api public
*/
Authenticator.prototype.authorize = function(strategy, options, callback) {
Authenticator.prototype.authorize = function authorize(strategy, options, callback) {
options = options || {};
options.assignProperty = 'account';
var fn = this._framework.authorize || this._framework.authenticate;

const fn = this._framework.authorize || this._framework.authenticate;
return fn(this, strategy, options, callback);
};

Expand Down Expand Up @@ -222,7 +229,7 @@ Authenticator.prototype.authorize = function(strategy, options, callback) {
* @return {Function} middleware
* @api public
*/
Authenticator.prototype.session = function(options) {
Authenticator.prototype.session = function session(options) {
return this.authenticate('session', options);
};

Expand All @@ -245,51 +252,54 @@ Authenticator.prototype.sessionManager = function(mgr) {
*
* @api public
*/
Authenticator.prototype.serializeUser = function(fn, req, done) {

// eslint-disable-next-line consistent-return
Authenticator.prototype.serializeUser = function serializeUser(fn, req, done) {
if (typeof fn === 'function') {
return this._serializers.push(fn);
}

// private implementation that traverses the chain of serializers, attempting
// to serialize a user
var user = fn;
const user = fn;

// For backwards compatibility
if (typeof req === 'function') {
done = req;
req = undefined;
}

var stack = this._serializers;

const stack = this._serializers;
// eslint-disable-next-line consistent-return
(function pass(i, err, obj) {
// serializers use 'pass' as an error to skip processing
if ('pass' === err) {
if (err === 'pass') {
err = undefined;
}
// an error or serialized object was obtained, done
if (err || obj || obj === 0) { return done(err, obj); }
var layer = stack[i];

const layer = stack[i];
if (!layer) {
return done(new Error('Failed to serialize user into session'));
}


function serialized(e, o) {
pass(i + 1, e, o);
}

try {
var arity = layer.length;
if (arity == 3) {
const arity = layer.length;
if (arity === 3) {
layer(req, user, serialized);
} else {
layer(user, serialized);
}
} catch(e) {
} catch (e) {
return done(e);
}
})(0);
}(0));
};

/**
Expand All @@ -305,54 +315,57 @@ Authenticator.prototype.serializeUser = function(fn, req, done) {
*
* @api public
*/
Authenticator.prototype.deserializeUser = function(fn, req, done) {

// eslint-disable-next-line consistent-return
Authenticator.prototype.deserializeUser = function deserializeUser(fn, req, done) {
if (typeof fn === 'function') {
return this._deserializers.push(fn);
}

// private implementation that traverses the chain of deserializers,
// attempting to deserialize a user
var obj = fn;
const obj = fn;

// For backwards compatibility
if (typeof req === 'function') {
done = req;
req = undefined;
}

var stack = this._deserializers;

const stack = this._deserializers;
// eslint-disable-next-line consistent-return
(function pass(i, err, user) {
// deserializers use 'pass' as an error to skip processing
if ('pass' === err) {
if (err === 'pass') {
err = undefined;
}
// an error or deserialized user was obtained, done
if (err || user) { return done(err, user); }
// a valid user existed when establishing the session, but that user has
// since been removed
if (user === null || user === false) { return done(null, false); }
var layer = stack[i];

const layer = stack[i];
if (!layer) {
return done(new Error('Failed to deserialize user out of session'));
}


function deserialized(e, u) {
pass(i + 1, e, u);
}

try {
var arity = layer.length;
if (arity == 3) {
const arity = layer.length;
if (arity === 3) {
layer(req, obj, deserialized);
} else {
layer(obj, deserialized);
}
} catch(e) {
} catch (e) {
return done(e);
}
})(0);
}(0));
};

/**
Expand Down Expand Up @@ -393,67 +406,70 @@ Authenticator.prototype.deserializeUser = function(fn, req, done) {
*
* @api public
*/
Authenticator.prototype.transformAuthInfo = function(fn, req, done) {

// eslint-disable-next-line consistent-return
Authenticator.prototype.transformAuthInfo = function transformAuthInfo(fn, req, done) {
if (typeof fn === 'function') {
return this._infoTransformers.push(fn);
}

// private implementation that traverses the chain of transformers,
// attempting to transform auth info
var info = fn;
const info = fn;

// For backwards compatibility
if (typeof req === 'function') {
done = req;
req = undefined;
}

var stack = this._infoTransformers;

const stack = this._infoTransformers;
// eslint-disable-next-line consistent-return
(function pass(i, err, tinfo) {
// transformers use 'pass' as an error to skip processing
if ('pass' === err) {
if (err === 'pass') {
err = undefined;
}
// an error or transformed info was obtained, done
if (err || tinfo) { return done(err, tinfo); }
var layer = stack[i];

const layer = stack[i];
if (!layer) {
// if no transformers are registered (or they all pass), the default
// behavior is to use the un-transformed info as-is
return done(null, info);
}


function transformed(e, t) {
pass(i + 1, e, t);
}

try {
var arity = layer.length;
if (arity == 1) {
const arity = layer.length;
if (arity === 1) {
// sync
var t = layer(info);
const t = layer(info);
transformed(null, t);
} else if (arity == 3) {
} else if (arity === 3) {
layer(req, info, transformed);
} else {
layer(info, transformed);
}
} catch(e) {
} catch (e) {
return done(e);
}
})(0);
}(0));
};

/**
* Return strategy with given `name`.
* Return strategy with given `name`.
*
* @param {String} name
* @return {Strategy}
* @api private
*/
Authenticator.prototype._strategy = function(name) {
Authenticator.prototype._strategy = function _strategy(name) {
return this._strategies[name];
};

Expand Down
Loading

0 comments on commit d53ff7d

Please sign in to comment.