Skip to content
This repository was archived by the owner on Mar 22, 2022. It is now read-only.

populate-user.js do not get settings #86

Closed
kulakowka opened this issue Feb 29, 2016 · 2 comments
Closed

populate-user.js do not get settings #86

kulakowka opened this issue Feb 29, 2016 · 2 comments

Comments

@kulakowka
Copy link
Contributor

This hook do not get settings for local auth from my config/default.json.

I have added configuration to default.json file.

{
  "host": "localhost",
  "port": 3001,
  "postgres": "postgres://postgres:@localhost:5432/feathers",
  "auth": {
    "token": {
      "secret": "bGGBbrJRXyIPRQW3BLaVp/P9/ounEv2L7qIVGfw3uVePt1eUg4Fi6Ji6Bpjknm9rh9hKHaMLwW7aGBVCejXIxA=="
    },
    "local": {
      "usernameField": "email",
      "userEndpoint": "/api/v1/users",
      "idField": "username"
    }
  }
}

And i passed settings to feather-authentication:

const authentication = require('feathers-authentication')

module.exports = function () {
  const app = this

  let config = app.get('auth')

  app.set('auth', config)
  app.configure(authentication(config))
}
@kulakowka
Copy link
Contributor Author

I also found that options.idField parameter is not actually used. It should be like this:

/**
 * Populate the current user associated with the JWT
 */
const defaults = {
  userEndpoint: '/users',
  passwordField: 'password',
  idField: '_id'
};

export default function(options = {}){
  options = Object.assign({}, defaults, options);

  return function(hook) {
    // If we already have a current user just pass through
    if (hook.params.user) {
      return Promise.resolve(hook);
    }

    let id;

    // If it's an after hook grab the id from the result
    if (hook.type === 'after') {
      id = hook.result[options.idField];
    }
    // Check to see if we have an id from a decoded JWT
    else if (hook.params.payload) {
      id = hook.params.payload[options.idField];
    }

    // If we didn't find an id then just pass through
    if (id === undefined) {
      return Promise.resolve(hook);
    }

    return new Promise(function(resolve, reject){
      hook.app.service(options.userEndpoint).get(id, {}).then(user => {
        // attach the user to the hook for use in other hooks or services
        hook.params.user = user;

        // If it's an after hook attach the user to the response
        if (hook.result) {
          hook.result.data = Object.assign({}, user = !user.toJSON ? user : user.toJSON());

          // format response
          delete hook.result[options.idField];
          delete hook.result.data[options.passwordField];
        }

        return resolve(hook);
      }).catch(reject);
    });
  };
}

I'll try to do PR with the necessary changes.

@ekryski
Copy link
Member

ekryski commented Feb 29, 2016

This was closed by #87

@ekryski ekryski closed this as completed Feb 29, 2016
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants