trailsjs / sails-auth

Passport-based User Authentication system for sails.js applications. Designed to work well with the sails-permissions module.
https://www.npmjs.org/package/sails-auth
MIT License
265 stars 141 forks source link

issue with sails 0.12.14 #167

Open ariston1983 opened 6 years ago

ariston1983 commented 6 years ago

I just found issue on sails-auth/api/services/passport.js on passport.connect for existing user/passport. the issue is on line 134 for saving passport and return user record.

On sails 0.12.14 the callback for the promise is no longer has parameter model in it, instead it has parameter error. so the old code:

return passport.save() .then(function (passport) {

is no longer works because the promise resolve function with parameter error:

return passport.save() .then(function (err) {

for this old code, the passport that necessary use to return the passport user always undefined since the save is success and no error returned. That the first issue on saving passport, the second one is on line 138 to return associated user within the passport:

return sails.models.user.findOne(passport.user.id);

first issue is that passport.user is identity number when auto populate flag not set to true. if we enable the populate flag to true then all models that has association in it will be auto populate, it can slow down the API. for anyone who disable the populate flag to false we need to treat the passport.user as the user identity not user entity. It's good to assume that anyone who use sails-auth module set their sails model config populate flag to default (e.g. False). The second issue with the code is that findOne function required criteria object and not support identity value (number), the code need to change into:

return sails.models.user.findOne({id: passport.user});

this code will work and return user associated with the passport if exists. I have test the changes on my local and the result it fix issue when I login with existing passport (if I use the old code I got error: "cannot access User of undefined").

passport-fix.txt