feathersjs-ecosystem / feathers-authentication-hooks

Useful hooks for authentication and authorization

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Newbie Q: Very confused about setting user ID.

adavie1 opened this issue · comments

Steps to reproduce

(First please check that this issue is not already solved as described
here
)

  • Tell us what broke. The more detailed the better.
  • If you can, please create a simple example that reproduces the issue and link to a gist, jsbin, repo, etc.

Expected behavior

Following the migration docs:

const { authenticate } = require('@feathersjs/authentication').hooks;
const { setField } = require('feathers-authentication-hooks');

module.exports = {
  before: {
    all: [ authenticate('jwt') ],
    find: [],
    get: [],
    create: [
      setField({
        from: 'params.user.id',
        as: 'params.userId'
        })
      ],
    update: [],
    patch: [],
    remove: []
  },

I get this error:
Forbidden: Expected field user.id not available // data.userId
Forbidden: Expected field params.userId not available // params.user.id
etc.
I've tried for the as prop: params.userId and data.user.id and userId

Honestly, the whole setField thing is way more confusing than the earlier methods.

Tell us what should happen
No idea. Still trying for figure out what as expects as a parameter.

Actual behavior

Tell us what happens instead
See above.

System configuration

Tell us about the applicable parts of your setup.

Module versions (especially the part that's not working):
Feathers 4.2.3
Feathers-authentication-hooks 1.0.1

Databaase is NeDB v5.1.0

NodeJS version:
12.x
Operating System:
Ubuntu16.04

Browser Version:
Chrome, up to date.
React Native Version:

Module Loader:

If you are using NeDB it's not 'params.user.id' but params.user._id.

If this is confusing I recommend to get more familiar with the hooks API, how params are set (e.g. through a REST request), the authentication guide and how Feathers database adapter query syntax works.

Thanks for the reply: Still getting Forbidden: Expected field params.user._id not available

This is my hook code:

const { authenticate } = require('@feathersjs/authentication').hooks;
const { setField } = require('feathers-authentication-hooks');

module.exports = {
  before: {
    all: [ authenticate('jwt') ],
    find: [],
    get: [],
    create: [
      setField({
        from: 'params.user.id',
        as: 'params.user._id'
        })
      ],
    update: [],
    patch: [],
    remove: []
  },

Ah, the issue was in from, not as:

  setField({
        from: 'params.user._id',
        as: 'data.userId'
        })
      ],

The above works.