Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

308
Views
Trouble running passport login Strategy. Authentication error~ TypeError: Cannot read property 'authenticate' of undefined

Things I've tried

  • Moving the authenticate directly into the server
  • Uninstalling and reinstalling the following(as well as their corresponding types):
    • passport
    • passport-jwt
    • passport-local

All of these still result in getting the following error message:
"TypeError: Cannot read property 'authenticate' of undefined"~Crashing the app

Code

Server File

import * as express from "express";
import apiRouter from "./routes/index";
import * as passport from 'passport';
import * as PassportLocal from 'passport-local';


import './middleware/passport-stradegies';

const app = express();


app.use(passport.initialize());
app.use(express.static("public"));
app.use(express.json());
app.use(apiRouter);

const port = process.env.PORT || 3000;
app.listen(port, () => console.log(`Server listening on port: ${port}`));



Passport Login Code

import * as passport from 'passport';
import * as PassportLocal from 'passport-local';
import blogs from "../db/blogs";
import { compareHash } from "../utils";

passport.serializeUser((user, done)=> done(null, user));
passport.deserializeUser((user, done)=> done(null, user));

passport.use(new PassportLocal.Strategy({
    usernameField: 'email'
},async (email, password, done) =>{
    try {
         const [userFound] = await blogs.find("email", email);
          if (userFound && compareHash(password, userFound.userpassword)) {
            done(null, userFound);
    }else{
        done(null, false);
    }
    } catch (error) {
        done(error);
    }
}))

Login / Token Creation Page

import * as express from "express";
// import * as jwt from 'jsonwebtoken';
import { authenticate } from 'passport';
import config from '../../config';


const router = express.Router();

router.post("/", authenticate('local'), async (req, res, next) => {
   try {
  //     const token = jwt.sign({
  //       userid: userFound.id, email: userFound.email, role: 1}, 
  //       config.jwt.secret, {expiresIn: '15d'}
  //       );
  //     return res.json(token);
    res.json('plz');
  } catch (err) {
    console.log(err);
    res.status(500).json({ message: "My code login broke oops" });
  }
});

export default router;
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The only problem is passport changed how authenticate is imported

In login/token Authentication Page

Change the import:

// import { authenticate } from 'passport'; to
import * as passport from 'passport';

// then anywhere you use authenticate just use passport.authenticate
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!