Login runs with admin.html but no with client .html

Hello,
I have that javascript code in app.js with jwt and session to authenticate admin or no admin (client). In local mode, all runs well but with web host, I have an internal error, if you have a suggestion to solve the problem, thanks, here’s the following message of the web host server :

Checking whether to disconnect long-running connections for process 70002, application VBJ (production) at Function process_params 
App 70002 output:     at /home/c2100699c/VBJ/app.js:229:5
App 70002 output:     at isValidCredentials (/home/c2100699c/VBJ/app.js:244:14)

Here is my code in app.js

  / Middleware pour vérifier l'authentification avec JWT
function verifyToken(req, res, next) {
  const token = req.cookies.token || '';
  jwt.verify(token, 'secret', function(err, decoded) {
  if (err) {
  return res.redirect('/login.html');
  } else {
  req.decoded = decoded;
  next();
  }
  });
  }
  
  // Route pour le login
  app.post('/auth', function(req, res) {
  const username = req.body.username;
  const password = req.body.password;
  const email = req.body.email;
  console.log('Request data:', username, password, email);
  
  if ((username === '***********' && email === '*****************') && password === '*********') {
  const token = jwt.sign({ username, email, role: 'admin' }, 'secret', { expiresIn: '1h' });
  res.cookie('token', token, { httpOnly: true });
  return res.redirect('/admin.html');
  } else {
  isValidCredentials(username, password, email, function(valid, isAdmin) {
  if (valid) {
  const role = isAdmin ? 'admin' : 'client';
  const token = jwt.sign({ username, email, role }, 'secret', { expiresIn: '1h' });
  res.cookie('token', token, { httpOnly: true });
  return res.redirect('/client.html');
  } else {
  return res.redirect('/login.html');
  }
  });
  }
  });
  
  function isValidCredentials(username, password, email, callback) {
  const query = 'SELECT * FROM users WHERE username = ? AND password = ? AND email = ? AND administrateur = 0';
  connection.query(query, [username, password, email], function(error, results, fields) {
  if (error) throw error;
  if (results.length > 0) {
  const isAdmin = results[0].administrateur === 1;
  callback(true, isAdmin);
  } else {
  callback(false, false);
  }
  });
  }
  
  // Route pour la page d'administration
  app.get('/admin', verifyToken, function(req, res) {
  if (req.session.loggedin) {
  res.sendFile(__dirname + '/admin.html');
  } else {
  res.send('Veuillez vous connecter pour accéder à cette page');
  }
  res.end();
  });
  
  // Route pour la page client
  app.get('/client', verifyToken, function(req, res) {
  if (req.session.loggedin) {
  res.sendFile(__dirname + '/client.html');
  } else {
  res.send('Veuillez vous connecter pour accéder à cette page');
  }
  res.end();
  });

and my code in login.js

/* Script for login form */
const loginForm = document.getElementById('login');
submit.addEventListener('click', function(event) {
  event.preventDefault();
  login();
});

function login() {
  const username = document.getElementById('username').value;
  const password = document.getElementById('password').value;
  const email = document.getElementById('emailRegister').value;

  fetch('/auth', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({username, password, email})
  })
  .then(res => {
    console.log('Response status:', res.status);
    console.log('Response headers:', res.headers);
    return res.json();
  })
  .then(data => {
    console.log('Response data:', data);
    if (data.userType === 'administrateur') {
      window.location.href = '/admin.html';
    } else if (data.userType === 'client') {
      window.location.href = '/client.html';
    } else {
      document.querySelector("#message2").innerText = "Identifiant, email ou mot de passe incorrect";
    }
  })
  .catch(error => {
    console.log('Error:', error);
  });
}

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.