Link to home
Start Free TrialLog in
Avatar of Isaac
IsaacFlag for United States of America

asked on

nodejs express - passport

Hello,

I am very new to nodejs but have been learning a lot these past few weeks.  I am trying to use passport to implement authentication but I am getting an error that I need to fix.  Any assistance would be much appreciated.  Here is the error I get..
User generated image
Below is part of my code:

index.js
const express = require('express');
const mongoose = require('mongoose');
const hbs = require('express-handlebars');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser')
const passport = require('passport')
const LocalStrategy = require('passport-local').Strategy
var User = require('./models/account')

mongoose.connect('mongodb:cluster0-shard-00-00-yng8g.mongodb.net:27017,cluster0-shard-00-01-yng8g.mongodb.net:27017,cluster0-shard-00-02-yng8g.mongodb.net:27017/vendorCollection?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin');


app.use(cookieParser())
//Persists data in memory inside of application
app.use(require('express-session')({
    secret: 'vendorSecret',
    resave: false,
    saveUninitialized: false
}))
app.use(passport.initialize())
app.use(passport.session())

//Allows you to use static files
app.use(express.static('public'))

app.use('/vendor', vendorRouter)
app.use('/admin', adminRouter)

passport.use( new LocalStrategy(User.authenticate()) )     //<---Error on this line
passport.serializeUser(User.serialize())
passport.deserializeUser(User.deserialize())

app.listen(3000, ()=>{
  console.log("Server Listening");
})

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of HainKurt
HainKurt
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
also, when you comment out  29, do you get same error on 30?
similarly, when you comment out  29 and 30, do you get same error on 31?
Avatar of Isaac

ASKER

Here's the code for

account.js
//Create model for vendor
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const passportLocalMongoose = require('passport-local-mongoose');

const Account = new Schema({
    username: String,
    password: String
})

Account.plugin(passportLocalMongoose)

Open in new window


Using this User.Authenticate() gives me the following error:
User generated image
When I remove the parenthesis, I get
TypeError: LocalStrategy requires a verify callback


When I comment out line 29, I get
passport.serializeUser(User.serialize())
                            ^

TypeError: User.serialize is not a function
so none of these works...

User.authenticate()
User.serialize()
User.deserialize()

and I dont see any code for those...

is it supposed to come from here?

Account.plugin(passportLocalMongoose)
Avatar of Isaac

ASKER

I figured it out... I did not export the module to be used/seen elsewhere.

module.exports = mongoose.model('Account', Account)
Avatar of Isaac

ASKER

This led me to figure it out

module.exports = mongoose.model('Account', Account)