V
V
Vladimir2019-11-16 17:06:45
Node.js
Vladimir, 2019-11-16 17:06:45

Why doesn't the identical function work?

Good evening. Please help. There are 2 identical functions, they differ only in the callback, but one works, and the second does not for some reason.. Please tell me what I'm doing wrong? (successAccess function)
Functions

const { app,static } = require('../modules/app_config')
const { passport } = require('../modules/packages')


successAccess = (req,res,next) => {
  if(req.isAuthenticated()) {
    console.log(`admin`)
    return res.redirect('/admin')
  } else {
    console.log(`next`)
    next()
  }
}

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

module.exports = { successAccess,bannedAccess }

(It doesn't work here, put it on post, for some reason it worked ..)
const { app,static } = require('../modules/app_config')
const { successAccess,bannedAccess } = require('../modules/access_in_pages')
const { passport } = require('../modules/packages')

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

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

})

app.post('/', (req, res, next) => {
    passport.authenticate('local', function(err, user) {
      if (err) {
      	return next(err)
      }
      if (!user) { 
      	return res.redirect('/')
      }
      req.logIn(user, function(err) {
        if (err) { 
        	return next(err)
        }
        return res.redirect('/admin')
      })
    })(req, res, next)
})

(And it works here) ..
const { app,static } = require('../modules/app_config')
const { succesAccess,bannedAccess } = require('../modules/access_in_pages')
const { pool } = require('../modules/mysql')


app.get('/admin',bannedAccess, (req, res) => {
  res.sendFile(`${static}/login.html`)
})

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

  const company_list = new Promise((resolve,reject) => {

    pool.query("SELECT * FROM the_company WHERE creator_id = '" + req.user + "'" , function(err,res){
      resolve(res.map(the_company_list => the_company_list))
    })

  })

  company_list.then(result => {
    res.send(result)
  })

})

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