Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

done function with null user returns an error #191

Open
flahol opened this issue Dec 10, 2021 · 0 comments
Open

done function with null user returns an error #191

flahol opened this issue Dec 10, 2021 · 0 comments

Comments

@flahol
Copy link

flahol commented Dec 10, 2021

I have an express 4, sequelize, passport, ES6 server.
When I authenticate a user with the right password, it works.
But when I have a bad user or a wrong password the done(null,false,{message:"error"}) throws an error :

TypeError: Cannot convert undefined or null to object
POSThttp://localhost:8080/api/v2/login

auth.service.js

import passport from "passport";
import { ExtractJwt, Strategy as JwtStrategy } from "passport-jwt";
import { Strategy as LocalStrategy } from "passport-local";

import userModel from "../modules/user/user.model";
import { db } from "../config/database.js";
import bcrypt from "bcryptjs";

const User = userModel(db.sequelize, db.Sequelize);

const localOptions = {
  usernameField: "email",
  passwordField: "password",
  session: false,
};

const jwtOptions = {
  jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
  secretOrKey: process.env.JWT_SECRET,
};

const localStrategy = new LocalStrategy(localOptions, function (email, password, done) {
  User.findOne({ where: { email: email }, raw: true })
    .then((user) => {
      if (!user) {
        return done(null, false, { message: `Cannot find contact with email=${email}.` });
      }

      bcrypt.compare(password, user.password).then((r) => {
        if (!r) {
          return done(null, false, { message: "Invalid password" });
        }
      });

      return done(null, user);
    })
    .catch((err) => {
      done(err, null, {
        message: "Error connecting to database",
      });
    });
});

passport.use(localStrategy);

export const authLocal = passport.authenticate("local", { session: false });

routes.js

import { Router } from "express";
import * as userController from "./user.controller.js";
import { authLocal } from "../../services/auth.services.js";

const routes = new Router();

routes.post("/login", authLocal, userController.login);

export default routes;

If I change
return done(null, false, { message: "Invalid password" }); with
return done(null, {name:"test"} , { message: "Invalid password" }); it seems to go for the next callback userController.login but it is not what I want.
Do I need to add a failure route ? I can't find how to solve this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant