Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

req.user is undefined #4

Closed
kahurangitama opened this issue May 13, 2016 · 2 comments
Closed

req.user is undefined #4

kahurangitama opened this issue May 13, 2016 · 2 comments

Comments

@kahurangitama
Copy link

Hi there!
I using PassportJS to authenticate users on my site.
My routes looks like:

/routes/songs.js:

router.route('/')
  .get(Songs.renderExplore)
  .post(require('permission')(['admin']), upload.array(), Songs.create)

this is the only place where I use permission.
Also I created a field named 'role' in my User schema.
Then I debugged my app and observe that at this point:

if (req.isAuthenticated() && !req.user[role])
    throw new Error("User doesn't have property named: " + role + ". See Advantage Start in docs")

req.user is undefined
but console.log(req.session.passport) returns me:

user: '57338a9a5c52e9159fc89095'

it mean that user is authorized and there is a record in session, but req.user is undefined;
my suggestion is that permission checks req.user before it actually filled by passport.deserializeUser()

How do you use permission with Passportjs?

Thanks in advance!

@tenodi
Copy link
Owner

tenodi commented May 19, 2016

Hey, sorry for not answering immediately. I'll take a look into it these days.

@kahurangitama
Copy link
Author

kahurangitama commented May 20, 2016

Sorry for disturbing, but it was my fault. For anyone who run into the same problem: double check your mongoose Schema, and rerun the server (it was my problem, I install npm, add field to existing user with MongoChef, but haven't update the Schema);

It should looks like this:

role: { type: String, default: 'member', enum: ['admin', 'moderator', 'member'] }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants