I am attempting to add the passport service to my app, but keep getting the following error, I believe I have installed all the dependencies. All searches for this error are futile. Looking forward to your help:
Following is the error message I get in the terminal:
file:///home/sirbt/Desktop/UberAds/passport-config.js:37
passport.use(new LocalStrategy({ usernameField: 'loginEmailAddress'}, authenticateUser ))
^
TypeError: passport.use is not a function
at initialize (file:///home/sirbt/Desktop/UberAds/passport-config.js:37:14)
at file:///home/sirbt/Desktop/UberAds/server.js:32:28
at ModuleJob.run (internal/modules/esm/module_job.js:170:25)
at async Loader.import (internal/modules/esm/loader.js:178:24)
at async Object.loadESM (internal/process/esm_loader.js:68:5)
Find below the contents of my passport-config.js
file where the issue seems to originate from:
./passport-config.js
import { Strategy as LocalStrategy } from "passport-local";
import * as passport from "passport";
import pkg1 from 'bcrypt';
const {bcrypt } = pkg1;
export default function initialize( _passport, getUserByEmail, getUserById ) {
const authenticateUser = async (loginEmailAddress, loginPasswordSignIn, done) => {
const user = getUserByEmail(loginEmailAddress)
if(user == null) {
return done(null, false, {message: 'No user with that email' });
}
try {
if( await bcrypt.compare(loginPasswordSignIn, user.password)){
return done(null, user, { message:'Password incorrect' })
} else {
return done(null, false, { message:'Password incorrect' })
}
} catch (e) {
return done(e)
}
}
passport.use(new LocalStrategy({ usernameField: 'loginEmailAddress'}, authenticateUser ))
passport.serializeUser((user, done) => done( null, user.id ) )
passport.deserializeUser((id, done) => {
return done( null, getUserById(id) )
})
}
Following is my server.js
app
./server.js
import express from 'express';
import mongodb from 'mongodb';
import Collection from 'mongodb';
import bcrypt from 'bcrypt';
import pkg3 from 'passport';
const { passport } = pkg3;
import pkg from 'express-flash';
const { flash } = pkg;
import pkg2 from 'express-session';
const { session } = pkg2;
import initialize from './passport-config.js';
const initializePassport = initialize();
initializePassport (
passport,
email => users.find(user => user.email === email),
id => users.find(user => user.id === id)
);
const bcryptt = bcrypt;
const MongoClient = mongodb.MongoClient;
const app = express();
app.use(express.static ('public'));
app.use(express.json() );
app.use(express.urlencoded({ extended: true }));
app.use( flash() );
app.use( session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false
}) );
app.use(passort.initialize());
app.use(passort.session());
app.use('/css', express.static(__dirname 'public/css'));
app.use('/js', express.static(__dirname 'public/js'));
app.use('/images', express.static(__dirname 'public/images'));
app.use('/fonts', express.static(__dirname 'public/fonts'));
app.set('view engine', 'ejs');
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
app.route('/advertiserLogin')
.get((req, res)=>{
res.render('advertiserLogin', {name: "Sir BT!"});
})
.post( passport.authenticate('local', {
successRedirect: '/advertiser',
failureRedirect: '/advertiserLogin',
failureFlash: true
}) );
app.listen (7000)
And finally the contents of my package.json
file
./package.json
{
"name": "smartqueues",
"version": "1.0.0",
"description": "",
"main": "server.js",
"type": "module",
"scripts": {
"start": "babel src --out-dir dist",
"devStart": "nodemon server.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@types/mongodb": "^4.0.7",
"babel-cli": "^6.26.0",
"bcrypt": "^5.0.1",
"ejs": "^3.1.8",
"express": "^4.18.1",
"express-ejs-layouts": "^2.5.1",
"express-flash": "^0.0.2",
"express-session": "^1.17.3",
"mongodb": "^4.7.0",
"mongoose": "^6.4.0",
"passport": "^0.6.0",
"passport-local": "^1.0.0",
},
"devDependencies": {
"@babel/cli": "^7.17.10",
"@babel/core": "^7.18.5",
"@babel/preset-env": "^7.18.2",
"nodemon": "^2.0.16"
}
}
Desperately looking forward to your assistance!
CodePudding user response:
You need to import the default export:
import passport from 'passport';