How to get passport to work with mongoDB

Hello. I am working on a node application.  I'm trying to use Passport for the log in.   I found an example online of what I want to do.  However I cannot get it to work.  I am running into an issue with my routes.  I get a compiler error that I have not seen before.  I tried to attached the project to this question, but it won't accept zip files so I will just attach my index.js and AuthController.js.  This is the error I get on index.js when it tries to do my first route.
 
var auth = require("../controllers/AuthController.js");

// restrict index for logged in user only
router.get('/', auth.home);

Open in new window

I get this error:  
index.js:349
throw new mongoose.Error.OverwriteModelError(name);
OverwriteModelError: Cannot overwrite `User` model once compiled.
at MongooseError.OverwriteModelError (C:\Users\ernest\Documents\coding bootcamp\code\Passport MongoDB\node-passport-auth\node_modules\mongoose\lib\error\overwriteModel.js:18:11)
    at Mongoose.model (C:\Users\ernest\Documents\coding bootcamp\code\Passport MongoDB\node-passport-auth\node_modules\mongoose\lib\index.js:349:13)
    at Object.<anonymous> (C:\Users\ernest\Documents\coding bootcamp\code\Passport MongoDB\node-passport-auth\models\user.js:12:27)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (C:\Users\ernest\Documents\coding bootcamp\code\Passport MongoDB\node-passport-auth\app.js:21:12)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)

npm ERR! Windows_NT 6.3.9600
npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "start"
npm ERR! node v6.11.0
npm ERR! npm  v3.10.10
npm ERR! code ELIFECYCLE
npm ERR! node-passport-auth@0.0.0 start: `node ./bin/www`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the node-passport-auth@0.0.0 start script 'node ./bin/www'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the node-passport-auth package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     node ./bin/www
npm ERR! You can get information on how to open an issue for this project with:
npm ERR!     npm bugs node-passport-auth
npm ERR! Or if that isn't available, you can get their info via:
npm ERR!     npm owner ls node-passport-auth
npm ERR! There is likely additional logging output above.
index.js
AuthController.js
cdlcidditAsked:
Who is Participating?
 
cdlcidditAuthor Commented:
@BigRat and  @leakim971. Thank you for your responses.  I think I may have figured it out.  I removed the Passport authenticate stuff to a separate passport.js file.  Once I did this it corrected the problem.  Here is the code that I moved to is own .js file:
var User = require('./models/user');
       passport.use(new LocalStrategy(User.authenticate()));
       passport.serializeUser(User.serializeUser());
       passport.deserializeUser(User.deserializeUser());

module.exports = User

Open in new window



I took that out of my app.js file.
0
 
BigRatCommented:
The second question is I think related to the issue about the node environment variable.

I think it is set to C:\Program Files\nodejs\node_modules\npm\bin
And I think it must be changed to C:\Program Files\nodejs

I have the same problem when trying to install node web-kit but I haven't had time to check it out yet.
0
 
leakim971PluritechnicianCommented:
could you post the User model ?
0
 
cdlcidditAuthor Commented:
I figured out the problem was the code needed to be moved to it's own file. Once I did that the app ran with no errors.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.