OIDC Callback: axios calls not working inside router.get

Viewed 124

I cannot get my api to work with my express server with OAuth2 logic, I always get an error since I had res.render i commented it out now the whole part is router.get does not work for axios.post inside it. Here is the code snippet:

mport {Request, Response, Router} from 'express';
import {writeFile, readFile, unlinkSync, unlink} from 'fs';
import {URLSearchParams} from 'url';
import axios from 'axios';
import qs from 'querystring';


const router: Router = Router();

...
//OIDC Callback
router.get('/callback', async function ({ query }, res, next) {
    console.log("In Callback...");
    if (query.error) {
      const description = query.error_description;
      return res.render('error', {
        error: description
      });
    }
  
    try {
      const code = query.code;
      const basicAuthHeader = Buffer.from(`${settings.clientId}:${settings.clientSecret}`).toString('base64');
  
      console.log("Inside callback...token details: "+ settings.clientSecret);
      const main_token = (await axios.post(metaData.token_endpoint, qs.stringify({
        grant_type: 'authorization_code',
        redirect_uri: settings.callbackUrl,
        code,
        scope: settings.scopes
      }), {
        headers: {
          'Accept': 'application/json',
          'Authorization': `Basic ${basicAuthHeader}`,
          'Content-Type': 'application/x-www-form-urlencoded'
        }
      })).data;
       const checkAURL = await needsAURL();
  
      if (checkAURL) {
        
        res.redirect(checkAURL);
      } else {
       // res.render('index', settings);
       console.log("inside else...");
      }
    } catch (e) {
      console.log("Error..Need organization access");
      // return res.render('error', {
      //   error: e
      // });
    }
  });  

I am pretty sure it is because of express router settings but I cannot fix this. If i make a axios.get inside router.get it works. But in this case I cannot do that.

Did I use express router correctly? Because I have never used it before.

0 Answers
Related