Isaac
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..
Below is part of my code:
index.js
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..
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");
})
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Here's the code for
account.js
Using this User.Authenticate() gives me the following error:
When I remove the parenthesis, I get
TypeError: LocalStrategy requires a verify callback
When I comment out line 29, I get
passport.serializeUser(Use r.serializ e())
^
TypeError: User.serialize is not a function
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)
Using this User.Authenticate() gives me the following error:
When I remove the parenthesis, I get
TypeError: LocalStrategy requires a verify callback
When I comment out line 29, I get
passport.serializeUser(Use
^
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(passportLoc alMongoose )
User.authenticate()
User.serialize()
User.deserialize()
and I dont see any code for those...
is it supposed to come from here?
Account.plugin(passportLoc
ASKER
I figured it out... I did not export the module to be used/seen elsewhere.
module.exports = mongoose.model('Account', Account)
module.exports = mongoose.model('Account', Account)
ASKER
This led me to figure it out
module.exports = mongoose.model('Account', Account)
module.exports = mongoose.model('Account', Account)
similarly, when you comment out 29 and 30, do you get same error on 31?