Home > other >  Express params routing sending to /userid instead of actual /{user id}
Express params routing sending to /userid instead of actual /{user id}

Time:03-11

I have a router which looks like this :

router.get('/', index_controller.index);
router.get('/login', index_controller.login)
router.get('/profile/:userId', index_controller.profile)

And then 2 controllers which should redirect a logged in user to profile/{userId}

login = (req, res) => {
    res.oidc.login(
      { returnTo: '/profile/:userid'});
};

profile = (req, res) => {
    const userid = req.oidc.userid;
    res.render('profile', { userid: userid,
      user: req.oidc.user,
    });
    console.log(userid);
  };

But when the user logs in, they get redirected to '/profile/:userId' instead of the actual userId.

I've been trying to follow examples such as this , the Express docs here but don't see what I'm missing because when I navigate manually, by copying and pasting the userId into the URL in the target format it works fine and loads up that users profile correctly. I think I'm missing some step in actually making the URL 'appear' in the URL once a user has logged in, but the functionality itself is working.

How do I make the profile page only available for the current user at profile/{userId} as intended?

CodePudding user response:

You hardcoded the string '/profile/:userid', but you need to somehow include the current user id.

I don't know how you can access the user in your case, but you'll have to do something like this:

login = (req, res) => {
    res.oidc.login(
      { returnTo: `/profile/${currentUser.id}`});
};
  • Related