0

I am redirecting a user to login page based on roles if they are not an admin to avoid access to a route, below is the code:

router.post('/gymcreate',async(req,res) => {
  
   let userName = req.session.user.email;
   let gymName = req.body.firstname;
   let location = req.body.city;
   let phoneNumber = req.body.mobile;
   let priceRange = req.body.price;
   try{
     check(userName,gymName,location,phoneNumber,priceRange);
   
     const creategym = await gymData.create(userName,gymName,location,phoneNumber,priceRange);
    
     if(creategym){
       res.status(200).redirect('/gyms')
       }
   else {
       res.status(500).render('gymbars/creategym', {title: "Error", error: 'Internal Server Error'})
       }
   }
   catch(e){
    res.status(400).render('gymbars/creategym', {title: "Error", error: e})
   }

      
 
  });

Along with the redirect() I also want to send a message saying, "You do not have access to this page". Is there a way to do this without using client side javascript?

Swayam Shah
  • 200
  • 1
  • 12
  • Does this answer your question? [How do I redirect in expressjs while passing some context?](https://stackoverflow.com/questions/19035373/how-do-i-redirect-in-expressjs-while-passing-some-context) – roberrrt-s Dec 09 '21 at 19:03
  • 1
    I would also take a better look at the http status quotes. Status 401 is Unauthorized and 403 is Forbidden. I would recomend using thoses statuses and then checking them in your client and give appropriate response there. Catch and returning 400 looks a little off aswell. 400 status is bad request meaning the client did something wrong but should really be an 500 error, since it returns if check fails or gymdata.create fails. – Bergur Dec 09 '21 at 19:09

0 Answers0