Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

419
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda