loopback icon indicating copy to clipboard operation
loopback copied to clipboard

Login throws 401

Open rkmax opened this issue 11 years ago • 32 comments

I'm using version 2.14.0. I have a Model without properties using User as base.

When I do a POST to /api/Model/login

{"email": "[email protected]", "password": "password", "ttl": 1209600000}

I get

{
  "error": {
    "name": "Error",
    "status": 401,
    "message": "login failed",
    "statusCode": 401,
    "code": "LOGIN_FAILED",
    "stack": "Error: login failed\n    at /home/julian/Development/example/node_modules/loopback/common/models/user.js:204:26\n    at /home/julian/Development/example/node_modules/loopback-datasource-juggler/lib/dao.js:1352:5\n    at allCb (/home/julian/Development/example/node_modules/loopback-datasource-juggler/lib/dao.js:1291:7)\n    at PostgreSQL.<anonymous> (/home/julian/Development/example/node_modules/loopback-connector-postgresql/lib/postgresql.js:741:7)\n    at null.<anonymous> (/home/julian/Development/example/node_modules/loopback-connector-postgresql/lib/postgresql.js:133:7)\n    at null.<anonymous> (/home/julian/Development/example/node_modules/loopback/node_modules/continuation-local-storage/context.js:74:17)\n    at bound (domain.js:254:14)\n    at runBound (domain.js:267:12)\n    at Query.handleReadyForQuery (/home/julian/Development/example/node_modules/loopback-connector-postgresql/node_modules/pg/lib/query.js:80:10)\n    at null.<anonymous> (/home/julian/Development/example/node_modules/loopback-connector-postgresql/node_modules/pg/lib/client.js:158:19)"
  }
}

rkmax avatar Apr 19 '15 17:04 rkmax

I've found the problem here https://github.com/strongloop/loopback/blob/master/common/models/user.js#L116-L145, the function only returns the username or email but never the password

rkmax avatar Apr 19 '15 17:04 rkmax

the problem persist with the version 2.16.3

rkmax avatar Apr 19 '15 17:04 rkmax

The problem has nothing to do with https://github.com/strongloop/loopback/blob/master/common/models/user.js#L116-L145. The query is only used to find matching users with username or email. Then the password is checked against the found record.

Please run your app with DEBUG=loopback:user node . to see what causes the login failure.

raymondfeng avatar Apr 19 '15 17:04 raymondfeng

@raymondfeng Console returns

loopback:user The password is invalid for user [email protected] +36s

I have in the following boot-script

  datasource.autoupdate('Model', function(err) {
     if (err) {
       console.log(err);
       return;
     }

     app.models.Model.create([
      {"email": "[email protected]", "password": "password"}
     ]);
   });

rkmax avatar Apr 19 '15 18:04 rkmax

@raymondfeng thanks for all your help, was doing several things wrong. and everything works as it should

rkmax avatar Apr 19 '15 19:04 rkmax

@rkmax Do you remember what your issue was and how you resolved it? I'm getting the same error, and I'm wondering if I was doing the same wrong things.

paulespinosa avatar Jan 05 '16 03:01 paulespinosa

@paulespinosa I don't recall in this moment. but try with the debug string that Raymond said, it gave me clues to solve the issue

rkmax avatar Jan 05 '16 14:01 rkmax

Hai, I am getting the same error as you have posted @rkmax . I followed what @raymondfeng have told. But I am getting the following in the console.

loopback:user No matching record is found for user [email protected]

But I have created this user already. I dont no what I am missing. Can someone help? Thanks

amazinggadget avatar Apr 20 '17 09:04 amazinggadget

@amazinggadget i have the same problem. My user exists in mongo db an i get the message no record found at login process... . Have you solve the problem?

ihr-it-projekt avatar Sep 08 '17 14:09 ihr-it-projekt

@amazinggadget any news on this topic? I am facing the same problem :(

angelo24983 avatar Nov 17 '17 12:11 angelo24983

Same probleme here. :-/ Any suggestions to resolve it?

chrischnweiss avatar Jan 08 '18 14:01 chrischnweiss

I'm having a similar problem. However I am able to make the first login, but then after I logout, on the second time I attempt to login I get the 401. Have no clue what's going on and it's been quite a frustrating journey.

JulsRicketti avatar Jan 23 '18 02:01 JulsRicketti

same error... any suggestions?

ZeeAzmat avatar Feb 16 '18 06:02 ZeeAzmat

same error

wasiddiqui avatar Mar 29 '18 19:03 wasiddiqui

I am getting the same error... I can't find anything on this issue to fix it. I installed loopback globally, made a new project with the cli, ran it without changing any files, posted a new user object to POST api/Users, got a 200 response back, and when I try to login with the same email and password I get Error "401 Error: login failed EVERY TIME." What am I doing wrong?

ghost avatar Oct 28 '18 21:10 ghost

I also tried with MongoDB & MySQL and there is no difference.

ghost avatar Oct 28 '18 21:10 ghost

Same probleme... I find that POST api/Users don't save password to db image I don't understand why, so I recreated project with 2.x version of LoopBack and everything works as it should.

ramirus avatar Oct 29 '18 10:10 ramirus

I think that my problem in #4043 is related to that... As said in my issue registration emails me a token=undefined. Now if I try to login it returns a LOGIN_FAILED. I've checked and the User is created in the DB

matteo-pennisi avatar Oct 29 '18 10:10 matteo-pennisi

I confirm, as @ramirus said, that password is not saved into DB. Also verificationToken is not present. If I run from my pc everything works well, verificationToken and password are correctly created and inserted into DB. What's is going on? It worked a week ago :/

matteo-pennisi avatar Oct 29 '18 10:10 matteo-pennisi

UPDATE I've found the reason why is working on local and not on the server. I think that is a problem with some packages from packages.json. It was using the dependencies from yarn.lock file. Now I've set dependencies in packages.json without ^ and using the same version as .lock file and now is working. This is my packages.json file:

"dependencies": {
    "compression": "^1.7.3",
    "cors": "^2.8.4",
    "helmet": "^3.13.0",
    "loopback": "3.22.2",
    "loopback-boot": "2.27.1",
    "loopback-cascade-delete-mixin": "2.0.0",
    "loopback-component-explorer": "6.2.0",
    "loopback-component-storage": "3.5.0",
    "loopback-connector-mongodb": "3.6.0",
    "loopback-ds-timestamp-mixin": "3.4.1",
    "loopback-connector-remote":"3.4.0",
    "loopback-connector":"4.5.1",
    "loopback-datasource-juggler":"3.23.0",
    "loopback-phase":"3.3.0",
    "loopback-swagger":"5.7.0",
    "loopback3-xtotalcount": "^0.1.2",
    "redis": "^2.8.0",
    "serve-favicon": "^2.0.1",
    "strong-error-handler": "^3.2.0"
  },

These is something wrong with one of these dependencies. However try to modify the file and write back if is working.

matteo-pennisi avatar Oct 29 '18 13:10 matteo-pennisi