Skip to content
This repository has been archived by the owner on Aug 29, 2018. It is now read-only.

Commit

Permalink
Now pulls from global auth config properly. Closes #1. Closes #2
Browse files Browse the repository at this point in the history
  • Loading branch information
ekryski committed Nov 23, 2016
1 parent a2f12f9 commit b776b77
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
10 changes: 7 additions & 3 deletions src/index.js
Expand Up @@ -17,8 +17,7 @@ const KEYS = [
'entity',
'service',
'passReqToCallback',
'session',
'local'
'session'
];

export default function init(options = {}) {
Expand All @@ -30,8 +29,12 @@ export default function init(options = {}) {
throw new Error(`Can not find app.passport. Did you initialize feathers-authentication before feathers-authentication-local?`);
}

let name = options.name || defaults.name;
let authOptions = app.get('auth') || {};
let localOptions = authOptions[name] || {};

// NOTE (EK): Pull from global auth config to support legacy auth for an easier transition.
const localSettings = merge({}, defaults, pick(app.get('auth') || {}, KEYS), omit(options, ['Verifier']));
const localSettings = merge({}, defaults, pick(authOptions, KEYS), localOptions, omit(options, ['Verifier']));
let Verifier = DefaultVerifier;

if (options.Verifier) {
Expand All @@ -49,6 +52,7 @@ export default function init(options = {}) {
// Register 'local' strategy with passport
debug('Registering local authentication strategy with options:', localSettings);
app.passport.use(localSettings.name, new LocalStrategy(localSettings, verifier.verify.bind(verifier)));
app.passport.options(localSettings.name, localSettings);

return result;
}
Expand Down
6 changes: 5 additions & 1 deletion src/verifier.js
Expand Up @@ -71,7 +71,11 @@ class LocalVerifier {
this.service.find({ query })
.then(this._normalizeResult)
.then(entity => this._comparePassword(entity, password))
.then(entity => done(null, entity))
.then(entity => {
const id = entity[this.service.id];
const payload = { [`${this.options.entity}Id`]: id };
done(null, entity, payload);
})
.catch(error => error ? done(error) : done(null, error));
}
}
Expand Down
40 changes: 40 additions & 0 deletions test/index.test.js
Expand Up @@ -55,6 +55,16 @@ describe('feathers-authentication-local', () => {
passportLocal.Strategy.restore();
});

it('registers the strategy options', () => {
sinon.spy(app.passport, 'options');
app.configure(local());
app.setup();

expect(app.passport.options).to.have.been.calledOnce;

app.passport.options.restore();
});

describe('passport strategy options', () => {
let authOptions;
let args;
Expand Down Expand Up @@ -110,6 +120,36 @@ describe('feathers-authentication-local', () => {
passportLocal.Strategy.restore();
});

it('pulls options from global config', () => {
sinon.spy(passportLocal, 'Strategy');
let authOptions = app.get('auth');
authOptions.local = { usernameField: 'username' };
app.set('auth', authOptions);

app.configure(local());
app.setup();

expect(passportLocal.Strategy.getCall(0).args[0].usernameField).to.equal('username');
expect(passportLocal.Strategy.getCall(0).args[0].passwordField).to.equal('password');

passportLocal.Strategy.restore();
});

it('pulls options from global config with custom name', () => {
sinon.spy(passportLocal, 'Strategy');
let authOptions = app.get('auth');
authOptions.custom = { usernameField: 'username' };
app.set('auth', authOptions);

app.configure(local({ name: 'custom' }));
app.setup();

expect(passportLocal.Strategy.getCall(0).args[0].usernameField).to.equal('username');
expect(passportLocal.Strategy.getCall(0).args[0].passwordField).to.equal('password');

passportLocal.Strategy.restore();
});

describe('custom Verifier', () => {
it('throws an error if a verify function is missing', () => {
expect(() => {
Expand Down

0 comments on commit b776b77

Please sign in to comment.