# oauth-app.js
> GitHub OAuth toolset for Node.js
[](https://www.npmjs.com/package/@octokit/oauth-app)
[](https://github.com/octokit/oauth-app.js/actions?workflow=Test)
Table of contents
- [Usage](#usage)
- [For OAuth Apps](#for-oauth-apps)
- [For GitHub Apps](#for-github-apps)
- [Examples](#examples)
- [`OAuthApp.defaults(options)`](#oauthappdefaultsoptions)
- [Constructor options](#constructor-options)
- [`app.on(eventName, eventHandler)`](#apponeventname-eventhandler)
- [`app.octokit`](#appoctokit)
- [`app.getUserOctokit(options)`](#appgetuseroctokitoptions)
- [`app.getWebFlowAuthorizationUrl(options)`](#appgetwebflowauthorizationurloptions)
- [`app.createToken(options)`](#appcreatetokenoptions)
- [For OAuth Web flow](#for-oauth-web-flow)
- [For OAuth Device flow](#for-oauth-device-flow)
- [`app.checkToken(options)`](#appchecktokenoptions)
- [`app.resetToken(options)`](#appresettokenoptions)
- [`app.refreshToken(options)`](#apprefreshtokenoptions)
- [`app.scopeToken(options)`](#appscopetokenoptions)
- [`app.deleteToken(options)`](#appdeletetokenoptions)
- [`app.deleteAuthorization(options)`](#appdeleteauthorizationoptions)
- [Middlewares](#middlewares)
- [`createNodeMiddleware(app, options)`](#createnodemiddlewareapp-options)
- [`createWebWorkerHandler(app, options)`](#createwebworkerhandlerapp-options)
- [`createAWSLambdaAPIGatewayV2Handler(app, options)`](#createawslambdaapigatewayv2handlerapp-options)
- [Build Custom Middlewares](#build-custom-middlewares)
- [Contributing](#contributing)
- [License](#license)
| Browsers | `@octokit/oauth-app` is not meant for browser usage. |
|---|---|
| Node | Install with `npm install @octokit/oauth-app` |
| name | type | description |
|---|---|---|
clientId
|
number
|
Required. Find Client ID on the app’s about page in settings. |
clientSecret
|
number
|
Required. Find Client Secret on the app’s about page in settings. |
clientType
|
string
|
Either "oauth-app" or "github-app". Defaults to "oauth-app".
|
allowSignup
|
boolean
|
Sets the default value for app.getWebFlowAuthorizationUrl(options).
|
redirectUrl
|
string
|
The URL in your application where users will be sent after authorization. See Redirect URLs in GitHub’s Developer Guide. |
defaultScopes
|
Array of strings
|
Only relevant when `clientType` is set to `"oauth-app"`. Sets the default `scopes` value for `app.getWebFlowAuthorizationUrl(options)`. See [available scopes](https://docs.github.com/en/developers/apps/scopes-for-oauth-apps#available-scopes) |
log
|
object
|
Used for internal logging. Defaults to console.
|
Octokit
|
Constructor
|
You can pass in your own Octokit constructor with custom defaults and plugins. The Octokit Constructor must use an authentication strategy that is compatible with[`@octokit/auth-oauth-app](https://github.com/octokit/auth-oauth-app.js/#readme). For usage with enterprise, set `baseUrl` to the hostname + `/api/v3`. Example: ```js import { Octokit } from "@octokit/core"; new OAuthApp({ clientId: "1234567890abcdef1234", clientSecret: "1234567890abcdef1234567890abcdef12345678", Octokit: Octokit.defaults({ baseUrl: "https://ghe.my-company.com/api/v3", }), }); ``` Defaults to `@octokit/oauth-app`'s own `Octokit` constructor which can be imported separately from `OAuthApp`. It's [`@octokit/core`](https://github.com/octokit/core.js) with the [`@octokit/auth-oauth-user`](https://github.com/octokit/auth-oauth-user.js/#readme) authentication strategy. |
| property | type | description |
|---|---|---|
context.name
|
string
|
Name of the event. One of: token, authorization
|
context.action
|
string
|
Action of the event. One of: created, reset, deleted
|
context.authentication
|
object
|
The OAuth authentication object. See https://github.com/octokit/auth-oauth-user.js/#authentication-object |
context.octokit
|
Octokit instance
|
Authenticated instance using the `Octokit` option passed to the constructor and [`@octokit/auth-oauth-user`](https://github.com/octokit/auth-oauth-user.js/#readme) as authentication strategy. The `octokit` instance is unauthenticated for `"token.deleted"` and `"authorization.deleted"` events. |
| name | type | description |
|---|---|---|
redirectUrl
|
string
|
The URL in your application where users will be sent after authorization. See Redirect URLs in GitHub’s Developer Guide. |
login
|
string
|
Suggests a specific account to use for signing in and authorizing the app. |
scopes
|
array of strings
|
An array of scope names (or: space-delimited list of scopes). If not provided, scope defaults to an empty list for users that have not authorized any scopes for the application. For users who have authorized scopes for the application, the user won't be shown the OAuth authorization page with the list of scopes. Instead, this step of the flow will automatically complete with the set of scopes the user has authorized for the application. For example, if a user has already performed the web flow twice and has authorized one token with user scope and another token with repo scope, a third web flow that does not provide a scope will receive a token with user and repo scope. |
state
|
string
|
An unguessable random string. It is used to protect against cross-site request forgery attacks.
Defaults to Math.random().toString(36).substr(2).
|
allowSignup
|
boolean
|
Whether or not unauthenticated users will be offered an option to sign up for GitHub during the OAuth flow. The default is true. Use false in the case that a policy prohibits signups.
|
| name | type | description |
|---|---|---|
code
|
string
|
Required. Pass the code that was passed as ?code query parameter in the authorization redirect URL.
|
state
|
string
|
Required. Pass the state that was passed as ?state query parameter in the authorization redirect URL.
|
| name | type | description |
|---|---|---|
onVerification
|
function
|
**Required**. A function that is called once the device and user codes were retrieved The `onVerification()` callback can be used to pause until the user completes step 2, which might result in a better user experience. ```js const auth = createOAuthUserAuth({ clientId: "1234567890abcdef1234", clientSecret: "1234567890abcdef1234567890abcdef12345678", onVerification(verification) { console.log("Open %s", verification.verification_uri); console.log("Enter code: %s", verification.user_code); await prompt("press enter when you are ready to continue"); }, }); ``` |
scopes
|
array of strings
|
Only relevant if `app.type` is `"oauth-app"`. Scopes are not supported by GitHub apps. Array of OAuth scope names that the user access token should be granted. Defaults to no scopes (`[]`). |
| name | type | description |
|---|---|---|
token
|
string
|
Required. |
| name | type | description |
|---|---|---|
token
|
string
|
Required. |
| name | type | description |
|---|---|---|
refreshToken
|
string
|
Required. |
| name | type | description |
|---|---|---|
target
|
string
|
Required unless targetId is set. The name of the user or organization to scope the user-to-server access token to.
|
targetId
|
integer
|
Required unless target is set. The ID of the user or organization to scope the user-to-server access token to.
|
repositories
|
array of strings
|
The list of repository names to scope the user-to-server access token to. repositories may not be specified if repository_ids is specified.
|
repository_ids
|
array of integers
|
The list of repository IDs to scope the user-to-server access token to. repositories may not be specified if repositories is specified.
|
permissions
|
object
|
The permissions granted to the user-to-server access token. See GitHub App Permissions. |
| name | type | description |
|---|---|---|
token
|
string
|
Required. |
| name | type | description |
|---|---|---|
token
|
string
|
Required. |
| name | type | description |
|---|---|---|
app
|
OAuthApp instance
|
Required. |
options.pathPrefix
|
string
|
All exposed paths will be prefixed with the provided prefix. Defaults to `"/api/github/oauth"` |
| name | type | description |
|---|---|---|
app
|
OAuthApp instance
|
Required. |
options.pathPrefix
|
string
|
All exposed paths will be prefixed with the provided prefix. Defaults to `"/api/github/oauth"` |
| name | type | description |
|---|---|---|
app
|
OAuthApp instance
|
Required. |
options.pathPrefix
|
string
|
All exposed paths will be prefixed with the provided prefix. Defaults to `"/api/github/oauth"` |
| name | type | description |
|---|---|---|
app
|
OAuthApp instance
|
Required. |
options.pathPrefix
|
string
|
All exposed paths will be prefixed with the provided prefix. Defaults to `"/api/github/oauth"` |
request
|
OctokitRequest
|
Generalized HTTP request in `OctokitRequest` type. |