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

105
Visualizações
Sending multiple post requests to same url using express.js

on my website I have a login form where the user inputs their information and when they submit a post request is made where their info is checked, and if it is valid I redirect them back to the login form where they then enter the code that was sent to their email. My question is when the post request is made the second time how would I use the same url but not have to go through the validating again.

Auth.js

//Login Route
router.post('/login', async(req, res) => {
    //Validate Data
    const { error } = loginValidation(req.body);

    if (error) {
        let msg = error.message;
        return res.cookie('loginError', msg, [{ httpOnly: true }]).redirect('/login');;

    }
    //Check if user exists
    const user = await User.findOne({ email: req.body.email });
    if (!user) {
        let msg = 'Email or password is invalid!'
        return res.cookie('loginError', msg, [{ httpOnly: true }]).redirect('/login');;
    }


    //Check if password is correct
    const validPass = await bcrypt.compare(req.body.password, user.password);
    if (!validPass) {
        let msg = 'Email or password is invalid!'
        return res.cookie('loginError', msg, [{ httpOnly: true }]).redirect('/login');;
    }

    const verificationCode = Math.floor(100000 + Math.random() * 900000);
    email.loginCode(req.body.email, verificationCode);
    return res.cookie('formComplete', 'true', [{ httpOnly: true }]).redirect('/login');
    //Create and assign a jwt
    const token = jwt.sign({ _id: user._id }, process.env.TOKEN_SECRET);
    res.header('auth-token', token).redirect('/dashboard');
});

Sorry if this is a dumb question, i'm new to express.js, if you need any more info let me know.

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Actually, it's not really related to express.
You are asking about "state" in the server.

If you was to "refactor" your question, it could become "I have this user that's signed in, how to verify him before he visit any routes"

The solution can be several

  1. Add a global state. Let say isVerified = false, after verification for the first time, you set it to true
  2. As you see in my "refactor" question above, session is something you use to check user logged in or shopping cart...

I suggest you dig into both solutions above, for example for the first one, where to declare that global state? What if we need to check multiple users?

about 4 years ago · Santiago Trujillo 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