Closed good-idea closed 6 years ago
Ok, i figured this out shortly after posting this. For anyone coming here with the same question -- the solution is not using passport-jwt
to achieve this, but rather the underlying jsonwebtoken
.
My working middleware now looks like:
const jwt = require('jsonwebtoken')
const PassportJwt = require('passport-jwt')
const getUserFromToken = (req, res, next) => {
const token = PassportJwt.ExtractJwt.fromAuthHeaderWithScheme('Bearer')(req)
jwt.verify(token, jwtSecret, (err, decoded) => {
if (err) {
req.user = false
next()
return
}
req.user = decoded
next()
})
}
app.use(getUserFromToken)
app.use('/graphql', graphqlHTTP(graphQLConfig))
// Elsewhere, in my GraphQL resolvers
const userQuery = (obj, args, request, info) => {
// ^^^^^^^
// I've also seen this parameter referred to as 'context'
console.log(request.user) // either 'false' or the serialized user data
if (req.user) {
// do things that this user is allowed to do...
} else {
// user is not logged in, do some limited things..
}
}
I'm authenticating calls to my express API using passport. I have a pretty standard setup:
When a request is made to /graphql with this token, everything works as expected. But, an unauthenticated request (with no token) returns a 401. What I'd like to do differently is use the checkToken middleware on all requests, assigning
req.user
to either the authenticated user data orfalse
. I'd then handle any authorization elsewhere.I'd like to be able to do something like this:
How can I achieve this?