V
V
Vladimir2019-10-20 07:34:55
Node.js
Vladimir, 2019-10-20 07:34:55

Why doesn't passport authorize with correct data?

Good morning. Please tell me why, with the same data, the passport does not authorize the user?
I output the following to the console:
If authorization is correct: console.log(userDB.email[0]) - [email protected]
If authorization is incorrect: console.log(userDB.password[0]) - Hancock
And before checks:

console. log(email + '/' + password) //incoming data
console.log(userDB.email[0] + '/' + userDB.password[0]) //data from the database

In the console I get this:
[email protected] ru/Hancock //Incoming data
[email protected]/Hancock //Data from the
Hancock database //Incorrect authorization
Here is the code:

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

const Sequelize = require('sequelize');

// Option 1: Passing parameters separately
const sequelize = new Sequelize('test', 'root', '', {
  host: 'localhost',
  dialect: 'mysql',
  operatorsAliases: false,
  pool: {
    max: 5,
    min: 0,
    acquire: 30000,
    idle: 10000
  }

  // dialectOptions: {
  //   socketPath: '/Applications/MAMP/tmp/mysql/mysql.sock',
  //   supportBigNumbers: true,
  //   bigNumberStrings: true
  // },

});

sequelize
  .authenticate()
  .then(() => {
    console.log('Connection has been established successfully.');
  })
  .catch(err => {
    console.error('Unable to connect to the database:', err);
  });

const User = sequelize.define('users', {
  // attributes
  email: {
    type: Sequelize.STRING,
    allowNull: false
  },
  password: {
    type: Sequelize.STRING
    // allowNull defaults to true
  }
}, {
  // options
});

let userDB = null;

sequelize
  .query('SELECT * FROM users', { raw: true })
  .then(users => {
  	const isIds = users[0].map(user => user.id);
  	const isEmails = users[0].map(user => user.email);
  	const isPasswords = users[0].map(user => user.password);

  	userDB = {
    id: isIds,
    email: isEmails,
    password: isPasswords
  }
});

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[0] === id) ? userDB : false;
  	done(null, user);

});

passport.use(new LocalStrategy(
  {usernameField: 'email'},
  function(email, password, done) {
  	console.log(email + '/' + password)
  	console.log(userDB.email[0] + '/' + userDB.password[0])
  	if(email === userDB.email[0] && password === userDB.password[0]) {
  		console.log(userDB.email[0])
  		return done(null,userDB)
  	}
  	else {
  		console.log(userDB.password[0])
  		return done(null,false)
  	}
  })
);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Adel Khalitov, 2019-11-02
@HistoryART

Try https://nodejs.org/en/docs/guides/debugging-gettin...
I think this will help you

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question