Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

418
Vistas
Block and deblock users using node js

I want to block and unblock user by clicking on a button, but the backend is not working as expected, the idea is when a user creats an account the default value of 'blocked' will be 0 but when a user is blocked, the value will be 1 and the user access to the home page will be blocked when the value is 1.

what i want to do in here is: for example the 'blocked' value is 0 when i send the request it will be changed to 1 and when i send another request for the second time the 1 will be changed again to 0....

The backend:

server.js:

app.get("/user/:username", (req, res) => {
  Register.findOne({username: req.params.username}, function (err, data) {
    if (err){
      res.status(500).send(err)
  } else{ 
    if( data.blocked == 0 ){
      data.blocked = 1}
    if( data.blocked == 1 ){ 
      data.blocked = 0 } }
        res.status(200).json({success :true ,message: data}) 
    
  })
})

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

There's a lot to unpack here... but I guess let's start with your main requirement.

what i want to do in here is: for example the 'blocked' value is 0 when i send the request it will be changed to 1 and when i send another request for the second time the 1 will be changed again to 0....

Assuming that data is what you think it is, just send back a res.status(401) and return:

function (err, data) {
  if (err) {
    console.error(err)
    return res.status(500)
  } else if (data.blocked === 1) {
    return res.status(401)
  }

  res.status(200)
}

So that should answer your question.


You probably don't want to do this on every route where you need authorization. So instead you should probably use some middleware to determine whether the user is blocked or not.

app.use((req, res, next) => {
  const username = req.params.username
  Register.findOne({ username }, function (err, data) {
    /* the rest of your code */
    if (data.blocked === 0) next()
  })
})

But even this is not great, because the user can just pass any username they want and gain access to the system. Instead what you want to do is authenticate the user and keep a session for them as a cookie. The session will contain their user information.

Lastly, you should not be using callbacks. It's 2022 :P. See if you can promisify your Express server or use any of the Express alternatives such as Koa/hapi/Nest.js, etc.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda