# auth-oauth-app.js > GitHub OAuth App authentication for JavaScript [](https://www.npmjs.com/package/@octokit/auth-oauth-app) [](https://github.com/octokit/auth-oauth-app.js/actions?query=workflow%3ATest) `@octokit/auth-oauth-app` is implementing one of [GitHub’s authentication strategies](https://github.com/octokit/auth.js). It implements authentication using an OAuth app’s client ID and secret as well as creating user access tokens GitHub's OAuth [web application flow](https://developer.github.com/apps/building-oauth-apps/authorizing-oauth-apps/#web-application-flow) and [device flow](https://docs.github.com/en/developers/apps/authorizing-oauth-apps#device-flow). - [Standalone Usage](#standalone-usage) - [Authenticate as app](#authenticate-as-app) - [Authenticate user using OAuth Web Flow](#authenticate-user-using-oauth-web-flow) - [Authenticate user using OAuth Device flow](#authenticate-user-using-oauth-device-flow) - [Usage with Octokit](#usage-with-octokit) - [`createOAuthAppAuth(options)` or `new Octokit({ auth })`](#createoauthappauthoptions-or-new-octokit-auth-) - [`auth(options)` or `octokit.auth(options)`](#authoptions-or-octokitauthoptions) - [Client ID/Client Secret Basic authentication](#client-idclient-secret-basic-authentication) - [OAuth web flow](#oauth-web-flow) - [OAuth device flow](#oauth-device-flow) - [Authentication object](#authentication-object) - [OAuth App authentication](#oauth-app-authentication) - [OAuth user access token authentication](#oauth-user-access-token-authentication) - [GitHub APP user authentication token with expiring disabled](#github-app-user-authentication-token-with-expiring-disabled) - [GitHub APP user authentication token with expiring enabled](#github-app-user-authentication-token-with-expiring-enabled) - [`auth.hook(request, route, parameters)` or `auth.hook(request, options)`](#authhookrequest-route-parameters-or-authhookrequest-options) - [Types](#types) - [Implementation details](#implementation-details) - [License](#license) ## Standalone Usage
| Browsers | ⚠️ `@octokit/auth-oauth-app` is not meant for usage in the browser. The OAuth APIs to create tokens do not have CORS enabled, and a client secret must not be exposed to the client. If you know what you are doing, load `@octokit/auth-oauth-app` directly from [cdn.skypack.dev](https://cdn.skypack.dev) ```html ``` |
|---|---|
| Node |
Install with npm install @octokit/auth-oauth-app
```js
const { createOAuthAppAuth } = require("@octokit/auth-oauth-app");
// or: import { createOAuthAppAuth } from "@octokit/auth-oauth-app";
```
|
| Browsers | ⚠️ `@octokit/auth-oauth-app` is not meant for usage in the browser. The OAuth APIs to create tokens do not have CORS enabled, and a client secret must not be exposed to the client. If you know what you are doing, load `@octokit/auth-oauth-app` and `@octokit/core` (or a compatible module) directly from [cdn.skypack.dev](https://cdn.skypack.dev) ```html ``` |
|---|---|
| Node | Install with `npm install @octokit/core @octokit/auth-oauth-app`. Optionally replace `@octokit/core` with a compatible module ```js const { Octokit } = require("@octokit/core"); const { createOAuthAppAuth, createOAuthUserAuth, } = require("@octokit/auth-oauth-app"); ``` |
| name | type | description |
|---|---|---|
clientId
|
string
|
Required. Find your OAuth app’s Client ID in your account’s developer settings.
|
clientSecret
|
string
|
Required. Find your OAuth app’s Client Secret in your account’s developer settings.
|
clientType
|
string
|
Must be set to either "oauth-app" or "github-app". Defaults to "oauth-app"
|
request
|
function
|
You can pass in your own @octokit/request instance. For usage with enterprise, set baseUrl to the API root endpoint. Example:
```js
const { request } = require("@octokit/request");
createOAuthAppAuth({
clientId: "1234567890abcdef1234",
clientSecret: "1234567890abcdef1234567890abcdef12345678",
request: request.defaults({
baseUrl: "https://ghe.my-company.com/api/v3",
}),
});
```
|
| name | type | description |
|---|---|---|
type
|
string
|
Required. Must be set to "oauth-app"
|
| name | type | description |
|---|---|---|
type
|
string
|
Required. Must be set to "oauth-user".
|
code
|
string
|
Required. The authorization code which was passed as query parameter to the callback URL from the OAuth web application flow.
|
redirectUrl
|
string
|
The URL in your application where users are sent after authorization. See redirect urls. |
state
|
string
|
The unguessable random string you provided in Step 1 of the OAuth web application flow. |
factory
|
function
|
When the `factory` option is, the `auth({type: "oauth-user", code, factory })` call with resolve with whatever the `factory` function returns. The `factory` function will be called with all the strategy option that `auth` was created with, plus the additional options passed to `auth`, besides `type` and `factory`. For example, you can create a new `auth` instance for for a user using [`createOAuthUserAuth`](https://github.com/octokit/auth-oauth-user.js/#readme) which implements auto-refreshing tokens, among other features. You can import `createOAuthUserAuth` directly from `@octokit/auth-oauth-app` which will ensure compatibility. ```js const { createOAuthAppAuth, createOAuthUserAuth, } = require("@octokit/auth-oauth-app"); const appAuth = createOAuthAppAuth({ clientType: "github-app", clientId: "lv1.1234567890abcdef", clientSecret: "1234567890abcdef1234567890abcdef12345678", }); const userAuth = await appAuth({ type: "oauth-user", code, factory: createOAuthUserAuth, }); // will create token upon first call, then cache authentication for successive calls, // until token needs to be refreshed (if enabled for the GitHub App) const authentication = await userAuth(); ``` |
| name | type | description |
|---|---|---|
type
|
string
|
Required. Must be set to "oauth-user".
|
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 = auth({ type: "oauth-user", 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 the clientType strategy option is set to "oauth-app".Array of OAuth scope names that the user access token should be granted. Defaults to no scopes ([]).
|
factory
|
function
|
When the `factory` option is, the `auth({type: "oauth-user", code, factory })` call with resolve with whatever the `factory` function returns. The `factory` function will be called with all the strategy option that `auth` was created with, plus the additional options passed to `auth`, besides `type` and `factory`. For example, you can create a new `auth` instance for for a user using [`createOAuthUserAuth`](https://github.com/octokit/auth-oauth-user.js/#readme) which implements auto-refreshing tokens, among other features. You can import `createOAuthUserAuth` directly from `@octokit/auth-oauth-app` which will ensure compatibility. ```js const { createOAuthAppAuth, createOAuthUserAuth, } = require("@octokit/auth-oauth-app"); const appAuth = createOAuthAppAuth({ clientType: "github-app", clientId: "lv1.1234567890abcdef", clientSecret: "1234567890abcdef1234567890abcdef12345678", }); const userAuth = await appAuth({ type: "oauth-user", onVerification, factory: createOAuthUserAuth, }); // will create token upon first call, then cache authentication for successive calls, // until token needs to be refreshed (if enabled for the GitHub App) const authentication = await userAuth(); ``` |
| name | type | description |
|---|---|---|
type
|
string
|
"oauth-app"
|
clientType
|
string
|
"oauth-app" or "github-app"
|
clientId
|
string
|
The client ID as passed to the constructor. |
clientSecret
|
string
|
The client secret as passed to the constructor. |
headers
|
object
|
{ authorization }.
|
| name | type | description |
|---|---|---|
type
|
string
|
"token"
|
tokenType
|
string
|
"oauth"
|
clientType
|
string
|
"oauth-app"
|
clientId
|
string
|
The clientId from the strategy options
|
clientSecret
|
string
|
The clientSecret from the strategy options
|
token
|
string
|
The user access token |
scopes
|
array of strings
|
array of scope names enabled for the token |
| name | type | description |
|---|---|---|
type
|
string
|
"token"
|
tokenType
|
string
|
"oauth"
|
clientType
|
string
|
"github-app"
|
clientId
|
string
|
The app's Client ID
|
clientSecret
|
string
|
One of the app's client secrets |
token
|
string
|
The user access token |
| name | type | description |
|---|---|---|
type
|
string
|
"token"
|
tokenType
|
string
|
"oauth"
|
clientType
|
string
|
"github-app"
|
clientId
|
string
|
The app's Client ID
|
clientSecret
|
string
|
One of the app's client secrets |
token
|
string
|
The user access token |
refreshToken
|
string
|
The refresh token |
expiresAt
|
string
|
Date timestamp in ISO 8601 standard. Example: 2022-01-01T08:00:0.000Z
|
refreshTokenExpiresAt
|
string
|
Date timestamp in ISO 8601 standard. Example: 2021-07-01T00:00:0.000Z
|