2

I created feathers js app and then added authentication using feather generate authentication

authentication.js

const authentication = require('@feathersjs/authentication');
const jwt = require('@feathersjs/authentication-jwt');
const local = require('@feathersjs/authentication-local');


module.exports = function (app) {
  const config = app.get('authentication');
  // Set up authentication with the secret
  app.configure(authentication(config));
  app.configure(jwt());
  app.configure(local());

  // The `authentication` service is used to create a JWT.
  // The before `create` hook registers strategies that can be used
  // to create a new valid JWT (e.g. local or oauth2)
  app.service('authentication').hooks({
    before: {
      create: [
        authentication.hooks.authenticate(config.strategies)
      ],
      remove: [
        authentication.hooks.authenticate('jwt')
      ]
    }
  });
};

then I added following code to app.js

const authentication = require('./authentication');
app.configure(authentication);

default.json looks like this

"authentication": {
    "secret": "****",
    "strategies": [
      "jwt",
      "local"
    ],
    "path": "/authentication",
    "service": "users",
    "jwt": {
      "header": {
        "typ": "access"
      },
      "audience": "https://yourdomain.com",
      "subject": "anonymous",
      "issuer": "feathers",
      "algorithm": "HS256",
      "expiresIn": "1d"
    },
    "local": {
      "entity": "user",
      "usernameField": "email",
      "passwordField": "password"
    }
  }

when I send post request to users service at localhost:3030/users from postman new user is created but when I try to send post request to localhost:3030/authentication from postman using payload

{"strategy":"local","email":"user","password":"pass"}

I get following response

{
    "name": "NotAuthenticated",
    "message": "Invalid login",
    "code": 401,
    "className": "not-authenticated",
    "data": {
        "message": "Invalid login"
    },
    "errors": {}
}

Everything looks fine but I cannot get it to work,I have been following this tutorial, can some help me? Thanks in advance.

Asad Abbas
  • 160
  • 12
  • how did you create your user in the `users` collection, specifically the password? did you use the `hashPassword` hook to hash the value in the password field? – Joe Oct 04 '19 at 11:28
  • @Joe I did not use hashPassword hook to hash the value in password field – Asad Abbas Oct 04 '19 at 17:47
  • 1
    feathers auth is going to hash your password input to compare to what's in the db, so you need to follow the documentation for this to work correctly: https://docs.feathersjs.com/api/authentication/local.html#hashpassword-field – Joe Oct 04 '19 at 20:17
  • I have the same problem. I did create the feathers app with authentication using the cli and I'm not able to make it work without changing any line of code. My problems is the same as @AsadAbbas described here and cannot find the way to make it work. – jaloplo Oct 21 '19 at 15:42
  • @jaloplo I have solved the problem, can you please ask the question, and share link here, would need to know more details before I can help you – Asad Abbas Oct 23 '19 at 04:58

0 Answers0