0

I am implemetnig a slack bot using bolt.js that perform github operation, and the bot requires the permission from user, so it implements gitub oauth to get the access token. I want to store the access token by user id. However ther is scoping issue for this code and I am not sure if it's JS problem or the express route problem.

"use strict";

const db = require("../db.js");
const { receiver } = require("../setting.js");
const axios = require("axios");

module.exports = async (params, body) => {
  console.log("Slack command user id " + body.user_id);
  let reply = {
    blocks: [
      {
        type: "section",
        text: {
          type: "mrkdwn",
          text: "Please sign in *<https://github.com/login/oauth/authorize/?client_id=cf2be89c01948ea2324d&scope=user%20repo|here>*!:bow:",
        },
      },
    ],
    text: "Please sign in",
  };

  let handler = async ({ query: { code } }, res) => {
    let user_id = body.user_id;
    let request_url = "https://github.com/login/oauth/access_token";
    let request_body = {
      client_id: process.env.GITHUB_CLIENT_ID,
      client_secret: process.env.GITHUB_SECRET,
      code,
    };

    let headers = { Accept: "application/json" };

    let _res = await axios.post(request_url, request_body, { headers });
    let access_token = _res.data.access_token;
    console.log("Oauth get user id " + user_id);
    let github_ref = db.collection("github").doc(user_id);
    let doc = await github_ref.get();
    if (!doc.exists) {
      console.log("doc does not exist");
      await github_ref.set({ access_token });
    } else {
      console.log("doc exist");
      await github_ref.update({ access_token });
    }

    //Need to change this url
    res.redirect("https://www.google.com/");
  };

  //Oauth
  receiver.app.get("/slack/oauth-callback", handler);

  return reply;
};

When a user use signin command, this function will get called. However if there are two different users use signin command. The second user will get there user id wrong.

Console output:

What I expected

Slack command user id U025P12Q6F7
Oauth get user id U025P12Q6F7

Slack command user id U029ANN9LJV
Oauth get user id U029ANN9LJV

What acutally happened

Slack command user id U025P12Q6F7
Oauth get user id U025P12Q6F7

Slack command user id U029ANN9LJV
Oauth get user id U025P12Q6F7

As you can see the second user get their oauth user_id wrong.

This is what I have in setting.js

const { App, ExpressReceiver } = require("@slack/bolt");
const receiver = new ExpressReceiver({ signingSecret: process.env.SLACK_SIGNING_SECRET });

module.exports = { App, receiver };

I have spent hours on this and any help is appreciated !

  • Could it be that the module is getting cached, due to multiple invocations? https://stackoverflow.com/questions/9210542/node-js-require-cache-possible-to-invalidate – Suyash Gaur Jul 28 '21 at 16:35

0 Answers0