V
V
Vladimir2019-10-18 05:32:06
MySQL
Vladimir, 2019-10-18 05:32:06

How to connect mysql to the authentication script on passport.js (so that the user is searched in the database)?

Good morning. Please tell me, here I have a userDB object, static, how can I load it into it or in some other way use users from the database to compare with the entered data. Thanks in advance.

const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;

const mysql = require('mysql');

const userDB = {
  id: 1,
  email: "[email protected]",
  password: "123"
}

passport.serializeUser(function(user, done) {
  console.log("Serialize: ", user);
  	done(null, user.id);
});

passport.deserializeUser(function(id, done) {
  /*User.findById(id, function(err, user) {
    done(err, user);
  });*/

  	console.log("Deserialize: ", id);
  	user = (userDB.id === id) ? userDB : false;
  	done(null, user);

});

passport.use(new LocalStrategy(
  {usernameField: 'email'},
  function(email, password, done) {
  	if(email === userDB.email && password === userDB.password) {
  		return done(null,userDB)
  	}
  	else {
  		return done(null,false)
  	}
  })
);

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question