fnakstad / angular-client-side-auth

One way to implement authentication/authorization in Angular applications
http://angular-client-side-auth.herokuapp.com/
MIT License
1.63k stars 346 forks source link

Mongoose and req.user problem #31

Closed prmcewen closed 11 years ago

prmcewen commented 11 years ago

I'm trying to adapt your code to store users in a MongoDB database using Mongoose.

What I'm having trouble with is req.user on the server side code. It ends up as

{ options: {}, safe: undefined, _conditions: { _id: 53395f1e44908fd4cb9f0fbe }, _updateArg: {}, _fields: undefined, _geoComparison: undefined, op: 'findOne', model: { [Function: model] base: [Object], modelName: 'User', model: [Function: model], db: [Object], findByUsername: [Function], schema: [Object], options: undefined, collection: [Object] } }

rather than something like

{ id: 1, username: 'user', password: '123', role: { bitMask: 2, title: 'user' } }

as it is when I run your code.

My inference is that it is being set as a Mongoose object, but I can't figure out where it is being set in order to ensure that it is set properly. Can you perhaps point me in the right direction?

Any insight would be appreciated. When I get it working, I'll put the code up on github as a fork of your code.

prmcewen commented 11 years ago

And as usual, as soon as I describe the problem to someone, I figure out the solution.

It was the deserializeUser function which should be

deserializeUser: function(id, done) { User.findById(id, function (err, user) { if(user) { done(null, user); } else { done(null, false); } }); }

fnakstad commented 11 years ago

Happy you figured it out, and thanks for writing up your solution :) That's really nice to have as documentation in case others run into the same problem.