1

Hi I'm looking to add a non user field to the payload in the auth process so I can included in the JWT token.

The request is like this:

{ "scriptId": "script1", "password": "password", "userName": "user1" }

config is like this:

  "auth": {
    "local": {
      "usernameField" : "scriptId"
    },
    "token": {
      "secret": "mysecret",
      "payload": ["scriptId","userName"],
      "expiresIn": "10min"
    },
    "successRedirect": "/chat.html"
  }

How can I pass the userName parameter so I can retrieve it in the before hook of the auth/token service so I can it to the hook.data

Or if there's a better way to do it just let me know.

KARTHIKEYAN.A
  • 18,210
  • 6
  • 124
  • 133
nando_023
  • 11
  • 2

3 Answers3

1

Check this PR:

// default.json
{
  "auth": {
    "extraFields": [
      "email",
      "roles"
    ]
  }
}

The extraFields are fieldnames of your user object. So data that goes in the JWT needs to be provided by your user endpoint.

j2L4e
  • 6,914
  • 34
  • 40
1

you can add this in following way

app.service('authentication').hooks({
    before: {
      create: [
        authentication.hooks.authenticate(config.strategies),
        function (hook) {
         hook.params.payload = {
            userId: hook.params.user.userId,
            accountId: hook.params.user.accountId
          };
          return Promise.resolve(hook);
        }
      ],
      remove: [
        authentication.hooks.authenticate('jwt')
      ]
    }
  });
KARTHIKEYAN.A
  • 18,210
  • 6
  • 124
  • 133
0
  1. create an after hook called add_payload (or whatever u want) for service authenticate using the command 'feathers generate hook'

  2. inside /src/service/authentication/index.js add inside the end of the export

app.service('/auth/local').after(hooks.after);

  1. create index.js file in /src/service/authentication/hooks, the contents should look like:

'use strict' const addPayload = require('./add_payload'); const globalHooks = require('../../../hooks'); const hooks = require('feathers-hooks'); exports.after = { all: [addPayload()], find: [], get: [], create: [], update: [], patch: [], remove: [] };

  1. inside the hook function... /src/service/authentication/hooks/add_payload.js

    return function(hook) { hook.result.aa = "xyz"; // add whatever you want to the return here... hook.addPayload = true; };

Aaron Gong
  • 977
  • 7
  • 18