0

After I define a cookie in an express cookie session, I can log it to the console with ease. However, when I attempt to access this cookie in another route of my application, it returns 'undefined'.

Setting the cookie:

router.get('/callback', catchAsync(async (req, res) => {
  console.log("in /callback");
  if (!req.query.code) throw new Error('NoCodeProvided');
  const code = req.query.code;
  const creds = btoa(`${CLIENT_ID}:${CLIENT_SECRET}`);
  var response = await fetch(`https://discordapp.com/api/oauth2/token?grant_type=authorization_code&code=${code}&redirect_uri=${redirect}`,
    {
      method: 'POST',
      headers: {
        Authorization: `Basic ${creds}`,
      },
    });
  var json = await response.json();

  req.session.token = json.access_token

  console.log(req.session.token)

>>> RETURNS THE TOKEN CORRECTLY <<<

Attempting to access the cookie in another route:

router.get('/loggedin', catchAsync(async (req, res) => {
  console.log("/loggedin");
  console.log("token: " + req.session.token);

>>> RETURNS 'token: undefined' <<<

1 Answers1

-1

In the first router.get('/callback'..) the catchAsync() function is not declared globally. It just handle this specific route, and doesn't really require a name.

You should wrap this function inside a middleware or create a function which is available globally, I don't know what is the goal but here is the 2 option.

Option 1 initiate the functionality as a middleware. The behaviour is depends on where you place it!!!! Maybe in that case doesn't fully makes sense, but you can play around, but I think you will get it.

// if you put before your router initiation it is going to have effect on all of the routes
app.use(async(req, res, next) => {
    if (!req.query.code) throw new Error('NoCodeProvided');
    const code = req.query.code;
    const creds = btoa(`${CLIENT_ID}:${CLIENT_SECRET}`);
    var response = await fetch(`https://discordapp.com/api/oauth2/token?grant_type=authorization_code&code=${code}&redirect_uri=${redirect}`,
      {
        method: 'POST',
        headers: {
          Authorization: `Basic ${creds}`,
        },
      });
    var json = await response.json();

    req.session.token = json.access_token

    console.log(req.session.token)
    // 
    // and you can do whatever want to do
    // but have to call next
    //
    next()

})

// and your router looks like

router.get('/callback', (req, res) => {
    // do what have to do
})

Option 2 - declare the middleware and use where you want

// defining this middleware somewhere in the code
const catchAsync = async(req, res, next) => {
    if (!req.query.code) throw new Error('NoCodeProvided');
    const code = req.query.code;
    const creds = btoa(`${CLIENT_ID}:${CLIENT_SECRET}`);
    var response = await fetch(`https://discordapp.com/api/oauth2/token?grant_type=authorization_code&code=${code}&redirect_uri=${redirect}`,
      {
        method: 'POST',
        headers: {
          Authorization: `Basic ${creds}`,
        },
      });
    var json = await response.json();

    req.session.token = json.access_token

    console.log(req.session.token)
    // 
    // and you can do whatever want to do
    // but have to call next
    //
    next()
}

router.get('/callback', catchAsync, (req, res) => {
    // do what have to do
})

router.get('/loggedin', catchAsync, (req, res) => {
    // do what have to do
})
Sándor Bakos
  • 485
  • 6
  • 10