Reputation: 170
This is a very specific question, but I really don't know what is causing the error. I have the following code:
router.get('/user/profile', passport.authenticate('jwt', { session: false }), function(req, res, next){
// Something
});
When I access the route it goes through my passport-jwt code that includes my custom getUserById() function, that is defined in the users model
let opts = {};
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderWithScheme('jwt');
opts.secretOrKey = config.secret;
passport.use(new JwtStrategy(opts, function(jwt_payload, done){
Users.getUserById(jwt_payload._id, function(err, user){
if(err){
return done(err, false);
}
if(user){
return done(null, user);
} else {
return done(null, false);
}
});
}));
getUserById()
module.exports.getUserById = function(id, callback){
Users.findById(id, callback);
}
When I access the route my server spits out the following error:
ReferenceError: Users is not defined
at Function.module.exports.getUserById (E:\Projects\CSGOOrb\models\users.js:55:2)
at JwtStrategy._verify (E:\Projects\CSGOOrb\config\passport.js:18:9)
at E:\Projects\CSGOOrb\node_modules\passport-jwt\lib\strategy.js:123:34
at E:\Projects\CSGOOrb\node_modules\jsonwebtoken\verify.js:27:18
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickCallback (internal/process/next_tick.js:180:9)
I have required my user model in all files that make use of it. Does anyone know what else could be causing it?
EDIT:
passport.js
const colors = require('colors/safe');
const Users = require('../models/users');
const config = require('../config/database');
const jwt = require('jsonwebtoken');
const JwtStrategy = require('passport-jwt').Strategy;
const ExtractJwt = require('passport-jwt').ExtractJwt;
const SteamStrategy = require('passport-steam').Strategy;
module.exports = function(passport){
let opts = {};
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderWithScheme('jwt');
opts.secretOrKey = config.secret;
passport.use(new JwtStrategy(opts, function(jwt_payload, done){
Users.getUserById(jwt_payload._id, function(err, user){
if(err){
return done(err, false);
}
if(user){
return done(null, user);
} else {
return done(null, false);
}
});
}));
...
users.js (Mongoose Model)
const mongoose = require('mongoose');
const config = require('../config/database');
// Users Schema
const UsersSchema = mongoose.Schema(
{
admin: {
type: Boolean,
default: false
},
mod: {
type: Boolean,
default: false
},
user: {
type: Boolean,
default: true
},
steam: {
id: {
type: String
},
identifier: {
type: String
},
name: {
type: String
},
avatar: {
type: String
},
avatarmedium: {
type: String
},
avatarfull: {
type: String
},
tradelink: {
type: String
}
}
},
{
timestamps:
{
createdAt: 'created_at',
updatedAt: 'updated_at'
}
}
);
const Users = module.exports = mongoose.model('User', UsersSchema);
module.exports.getUserById = function(id, callback){
Users.findById(id, callback);
}
module.exports.getUserById64 = function(id, callback){
const query = {id: steam.id};
Users.findOne(query, callback);
}
Upvotes: 1
Views: 5337
Reputation: 170
Figured it out myself after some more testing. I added
const Users =
in front of
module.exports = mongoose.model('User', UsersSchema);
so now it's
const Users = module.exports = mongoose.model('User', UsersSchema);
It works perfectly now. Also big thanks to @Yury Tarabanko for his suggestion in the comments.
Upvotes: 1