GitHub API authentication using a callback method
npm install @octokit/auth-callback> GitHub API authentication using a callback method


Browsers | Load `` |
|---|---|
Node | Install with npm install @octokit/auth-callback ` |
> [!IMPORTANT]
> As we use conditional exports, you will need to adapt your tsconfig.json by setting "moduleResolution": "node16", "module": "node16".
>
> See the TypeScript docs on package.json "exports".
> See this helpful guide on transitioning to ESM from @sindresorhus
`js
let token;
const auth = createCallbackAuth({ callback: () => token });
await auth();
// {
// type: 'unauthenticated'
// }
token = "secret123";
await auth();
// {
// type: 'token',
// token: 'secret123',
// tokenType: 'oauth'
// }
`
The createCallbackAuth method accepts a single options parameter
name | type | description |
|---|---|---|
options.callback | function | Required. A method that returns or resolves with a token string. |
The async auth() method does not accept any arguments
The async auth() method resolves to one of two possible authentication objects
1. Unauthenticated if the callback() returns or resolves a falsy valuecallback()
2. Token authentication if the returns or resolves with a string value
name | type | description |
|---|---|---|
type | string | "unauthenticated" |
name | type | description |
|---|---|---|
type | string | "token" |
token | string | The personal access token |
tokenType | string | One of: 1. |
or auth.hook(request, options)auth.hook() hooks directly into the request life cycle. It amends the request to authenticate correctly based on the request URL.
The request option is an instance of @octokit/request. The route/options parameters are the same as for the request() method.
auth.hook() can be called directly to send an authenticated request
`js`
const { data: user } = await auth.hook(request, "GET /user");
Or it can be passed as option to request().
`js
const requestWithAuth = request.defaults({
request: {
hook: auth.hook,
},
});
const { data: user } = await requestWithAuth("GET /user");
``
See CONTRIBUTING.md