problem sending values from one route to another

Asked

Viewed 200 times

0

I am making a login system with mysql I am unable to pass the route values to another i want to go from login router to users router the code is followed

    connection.query('SELECT * FROM users WHERE username = ?', [username], function(err, rows, fields) {
     if (err) {
     appData.error = 1;
     appData['data'] = 'Error Occured!';
     res.status(400).json(appData);
     } else {
     if (rows.length > 0) {
     if (rows[0].password == password) {

    if (rows[0].nivel== 1) {

       res.redirect(307, '/Administration');
    //eu quero passar o username/nivel para /Administration
  res.send(username);
    }else if (rows[0].nivel== 2) {
       res.redirect(307, '/Users');

    }
    else if (rows[0].nivel== 3) {
       res.redirect(307, '/usersvip');

    }
    else if (rows[0].nivel== 4) {
       res.redirect(307, '/moderador');

    }
    else if (rows[0].nivel== 2) {
       res.redirect(307, '/activar');

    }
    else {
         res.redirect(307, '/banned');
    }

the page where I want to receive the values

var express = require('express');
var router = express.Router();
router.post('/', function(req, res, next) {
  res.render('Administration/index', { title: 'Home'});

});


module.exports = router;
  • Do you want to pass data on redirect()? If so I think in query, but this is not the best way.

  • i want to do is the login is done successfully or not if it is successful if level 1 goes to page 2 for another passing the values i in php used session_regenerate_id(); $Member = $result -> fetch_assoc(); $_SESSION['SES_NIVEL'] = $Member['nivel']; $_SESSION['SESS_EMAIL'] = $Member['mail']; there is no same metadata but for nodejs with the express

  • So I advise you to take a look at express-Session. This type of validation becomes simpler with use of Session and Session validation middleware. Github

  • Look you can make a simple example where a login value is sent to user using sessions and I’m not getting it

  • I cannot answer your question, I would be out of scope and would be negative, but take a look at this simple example that is in mine Github. Another example a little more sophisticated Bitbucket

  • in your github project you use this dependencies "dependencies": {"bcrypt": " 3.0.0","body-parser": " 1.18.3","ejs": " 2.6.1","express": " 4.16.3","express-Session": " 1.15.6","Mongoose": " 5.2.4","Passport": " 0.4.0",Passport-local": " 1.0.0" },"devDependencies": {"nodemon": " 1.18.2" } but I use these "dependencies": {"body-parser": "~1.13.2","cookie-parser": "~1.3.5","Cors": " 2.8.4","debug": "~2.2.0","express": "~4.13.1","express-Session": " 1.15.6","jade": "~1.11.0","jsonwebtoken": " 8.3.0","Morgan": "~1.6.1","mysql": " 2.16.0"serves-favicon": "~2.3.0" which I have to change&#Xa"; }

  • for it to work with your example that you have on your github I just have to install the right dependencies "dependencies": { "bcrypt": " 3.0.0", "Passport": " 0.4.0", "Passport-local": " 1.0.0" },

  • That’s right. If you give an npm install or Yarn it installs everything automatically.

  • This example uses mongodb as database.

  • regarding what you have in the controller I have to change something is in auth.js, home.js, user.js that mysql usage I’m starting with nodejs I usually use php or only have in the templates folder is that I have alteral

  • So what you have in the controller of that ai is only for mongodb if you want to manipulate mysql index another folder of mine, Mysql or using sequelize, firm grip on the studies, for mysql I started using the native dependecia then I started to use sequelize.

  • what’s the difference of using the database connection in app.js or a route

  • In terms of use in the api none, but in terms of design the structure gets more organized, have you ever imagined 7 databases configured in main? At first I configured anyway, hj I use OOD model.

  • or better is to call in the app I created two files one for connection and another that saves the connection

  • Look I can’t use integrating your two examples auth with passaport and people could help me always use pastben or create a repository on github I just need a simple example

  • if possible use with Session and pass route values to another and thanks for the help

  • Access this link here https://chat.stackexchange.com/rooms/81195/discussion-about-question-ams-and-rsrd

  • Follow https://github.com/andersonmendesdev/exampleLogin

  • thanks for the help

  • Already now I use bcrypt to increpitar my password

  • Great, watch out for questions out of context or too vague the staff negative you and you lose the right to ask. If you have skype add la ai hora q have doubt you ask me skype: Anderson.mendes88

  • I’m already on Skype thank you

  • topic can be closed problem has been solved thanks AMS for the help

Show 18 more comments

1 answer

0


got it this way

const authenticateUser = async (connection,  req, res) => {

  const user = await User.findUser(connection, req.body.username)
    if(!user){
        return res.render('login/login',{error: true})
    }
    if(!await bcrypt.compare(req.body.password, user.password)){
        return res.render('login/login', {error: true})
}
    else{
      if(user.user_type_id==1){
        req.session.user = user
        res.locals.user = user
      res.redirect('/Administration/index');
    }else if(user.user_type_id==2){
      req.session.user = user
      res.locals.user = user
      res.redirect('/Administration/index');
    }else if(user.user_type_id==3){
      req.session.user = user
      res.locals.user = user
      res.redirect('/Administration/index');
    }else if(user.user_type_id==4){
      req.session.user = user
      res.locals.user = user
      res.redirect('/Administration/index');
      }
      else if(user.user_type_id==5){
        req.session.user = user
        res.locals.user = user
        res.redirect('/Administration/index');
    } else {
        console.log("banned");
    }



  }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.