node.jsfeathersjsfeathers-authenticationfeathers-service

Feathers authentication.create throws 401 not authenticated error


// code

app.use('/login', {
  async create(data, params) {
    data.strategy = 'local';
    return authentication.create(data, params);
  }
});

// config

"local": {
  "usernameField": "username",
  "passwordField": "password"
}

I am using authentication.create method in my login API, I am getting this error response when i try to login

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

// request

curl --location --request POST 'http://localhost:3030/login'
--header 'Content-Type: application/json'
--data-raw '{
"username": "uname",
"password": "123456"
}'

The username and password is correct, this works in my local machine(macOS), it throws error only when i run in a testing environment(ubuntu 20 in AWS EC2). they both point to the same database(mySQL 5) inside a AWS EC2 instance. what is causing the issue ?


Solution

  • Mentioned in the authentication configuration options in Windows and with PM2 the username field needs to be escaped since it is also an environment variable:

    "local": {
      "usernameField": "\\username",
      "passwordField": "password"
    }
    

    This will no longer be necessary in the upcoming version 5.