V
V
Vladimir2019-12-18 14:37:46
JavaScript
Vladimir, 2019-12-18 14:37:46

Why is the request not reaching passport?

Good afternoon. Please tell me why I can’t authorize the user, but I am immediately redirected to the page, regardless of whether I am authorized or not. In other words, why the request does not reach the passport, which entails non-working functions.
passport.js:

const { mysql,passport,localStrategy,bcrypt } = require('./packages')
const { pool } = require('./mysql')

passport.serializeUser(function(user, done) {

  done(null, user.id)

})

passport.deserializeUser(function(id, done) {

  	pool.query("SELECT * FROM users WHERE id='"+id+"'",function(err,res){	
    done(null, id)
  })

})

passport.use(new localStrategy({ usernameField: 'email' },
  async (email, password, done) => {

    	pool.query("SELECT * FROM users WHERE email = '" + email + "'", async (err,res) => {

      if (res.length < 1) {

        console.log(`Banned access for: ${email}`)

        return done(null,false)

      }

      else {

        const user = res[0]
        const Email = user.email
        const Pass = user.password
        const Phone = user.phone

        console.log(`Success access for: ${Email}`)

        bcrypt.compare(password, Pass, async (err, res) => {
          if(res) {

            console.log(user)

            return done(null,user)

          }

        })

      }

    })
  }
))

check_page:
const successAccess = (req,res,next) => {
  if(req.isAuthenticated()) next()
  else return res.redirect('/')
}

const bannedAccess = (req,res,next) => {
  if(req.isAuthenticated()) return res.redirect('/admin')
  else next()
}

module.exports = { successAccess,bannedAccess }

default_page:
const { app,static } = require('../modules/app_config')
const { bannedAccess } = require('../modules/check_page')
const { passport } = require('../modules/passport')

app.get('/',bannedAccess, (req, res) => {

  res.sendFile(`${static}/index.html`)

})

app:
//Export project modules
const { express,passport,session,util,mysql,fileStore,localStrategy,bcrypt,saltRounds } = require('./modules/packages')

//Export project routes
const { DEFAULT_PAGE } = require('./routes/default_page')

index.html:
<form id="login" action="/" method="POST">

      <h1>Авторизация</h1>

    		<input id="email" type="email" placeholder="Email" name="email" required>
    		<input id="pass" type="password" placeholder="Пароль" name="password" required>
    		<div class="info">
    			<div class="reset-password"><label>Введите ваш пароль.Забыли?Вам поможет </label> <a href="#">восстановление пароля</a></div>
    			<div class="cookie"><div class="checkbox"></div><label>Запомнить меня на сайте<label></div>
    		</div>

    		<div class="submit">
      		<button type="submit" class="auth">Войти</button>
      		<div class="or">или <a class="register" href="#">зарегистрироваться</a></div>
      	</div>

    	</form>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Robur, 2019-12-18
@Robur

because you haven't called passport anywhere.
add
app.use(passport.initialize())
and passport.authenticate to your routes. In general - for starters, do as in the documentation.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question