NodeJS, express request.login is not a function

Viewed 798

I'm using these versions:

  • Node 11.6.0
  • npm 6.5.0-next.0
  • Express 4.17.1
  • Passport 0.5.0
  • Passport-local 1.0.0

I'm using passport and express request login to create a simple login. I'm not using a database yet, I'm just trying to make it work with mock data before going on:

const express = require("express");
const router = express.Router();
const passport = require("passport");

router.post("/login", (request, response, next) => {
    passport.authenticate("login", (error, user) => {
        try {
            if(error) return next(error);
            if(!user) return next(new Error("Email and password are required."));

            request.login(user, {session: false}, (error) => {
                if(error) return next(error);
                return response.status(200).json({status: 200, user});
            });
        } 
        catch (error) {
            console.log(error);    
            return next(error);
        }
    })(request, response, next);
});

Arriving here, it gives me error:

TypeError: request.login is not a function
    at passport.authenticate (D:\Progetti Zenva\NodeJS\restApi\routes\user.js:20:21)
    at Strategy.strategy.success (D:\Progetti Zenva\NodeJS\restApi\node_modules\passport\lib\middleware\authenticate.js:213:18)
    at verified (D:\Progetti Zenva\NodeJS\restApi\node_modules\passport-local\lib\strategy.js:83:10)
    at Strategy.passport.use.localStrategy.Strategy [as _verify] (D:\Progetti Zenva\NodeJS\restApi\authentication\authentication.js:28:12)
    at Strategy.authenticate (D:\Progetti Zenva\NodeJS\restApi\node_modules\passport-local\lib\strategy.js:90:12)
    at authenticate (D:\Progetti Zenva\NodeJS\restApi\node_modules\passport\lib\middleware\authenticate.js:361:7)
    at Layer.handle [as handle_request] (D:\Progetti Zenva\NodeJS\restApi\node_modules\express\lib\router\layer.js:95:5)
    at next (D:\Progetti Zenva\NodeJS\restApi\node_modules\express\lib\router\route.js:137:13)

This is the authentication.js file, with my strategies:

const passport = require("passport");
const localStrategy = require("passport-local");


passport.use("login", new localStrategy.Strategy({
    usernameField: "email",
    passwordField: "password",
}, (email, password, done) => {
    if(email !== "joe@test.com") return done(new Error("User not found."));
    if(password !== "test") return done(new Error("Invalid password."));

    return done(null, {name: "joe"});
}));

I tried using alias too as someone else pointed out (request.logIn), and I tried following the documentation (Here) but it doesn't work.

UPDATED ADDING MY STRATEGY.

Any clue?

3 Answers

From the docs:

Authenticating requests is as simple as calling passport.authenticate() and specifying which strategy to employ. authenticate()

http://www.passportjs.org/docs/authenticate/

There isn't a strategy called login. Which is what you are trying to pass in

passport.authenticate("login", (error, user) => {

If you are just testing this out try the local strategy.

passport.authenticate("local", (error, user) => {

Here is an example from their github that uses this:

https://github.com/passport/express-4.x-local-example

https://github.com/jaredhanson/passport

const passport = require("passport");
const localStrategy = require("passport-local");

// add this next line, don't need to downgrade
// must be set before passport.use(...)
app.use(passport.initialize());

passport.use("login", new localStrategy.Strategy({
    usernameField: "email",
    passwordField: "password",
}, (email, password, done) => {
    if(email !== "joe@test.com") return done(new Error("User not found."));
    if(password !== "test") return done(new Error("Invalid password."));

    return done(null, {name: "joe"});
}));
Related