Search code examples
github-apioctokitoctokit-jsgithub-oauth

How to instantiate Octokit for user-as-a-server oAuth?


I want my GitHub app to perform actions on behalf of a user, like creating an issue.

I am authenticating my user this way:

const { createOAuthAppAuth } = require('@octokit/auth-oauth-app');

const auth = createOAuthAppAuth({
  clientType: 'oauth-app',
  clientId: clientGithubID,
  clientSecret: clientSecretGithub,
});

const userAuthenticationFromWebFlow = await auth({
  type: 'oauth-user',
  code,
  state: userId,
});

// console.log(useAuthenticationFromWebFlow)
// {
//   "type": "token",
//   "tokenType": "oauth",
//   "clientType": "oauth-app",
//   "clientId": "Iv1.CLIENTID",
//   "clientSecret": "CLIENTSECRET",
//   "token": "ghu_bunchofchars",
//   "scopes": []
// }

It seems that the token that I get is good.

Now the problem is when I try to create an Octokit later in my code this doesn't work

octokit = new Octokit({
  authStrategy: userAuthenticationFromWebFlow,
});
await octokit.issues.createComment({...props})

What is the proper way to create an Octokit instance ?

Thank you !


Solution

  • So I just found the answer,

    may be that can help someone.

    const { createOAuthAppAuth } = require('@octokit/auth-oauth-app');
    
        octokit = new Octokit({
          authStrategy: createOAuthAppAuth,
          auth: userAuthenticationFromWebFlow.token,
        });
    

    And it works !