> ## Documentation Index
> Fetch the complete documentation index at: https://docs.directgvr.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Add OAuth2 sign-in

> Let players sign in to your launcher with Google or another OAuth2 provider.

The launcher skin supports third-party sign-in with OAuth2. It comes set up for Google, and you can adapt it to other providers.

## Turn on OAuth

Set `useOAuth` to `true` in `public/app.yaml`. The launcher then shows its OAuth sign-in component, which gets an access token from the provider using one of these methods:

| Method | Description | Browser support |
| - | - | - |
| `POPUP` | Shows the provider's sign-in page in a popup window. | Any |
| `FRAME` | Shows the sign-in page in an iframe. Add the authorization URL's domain to the host name allowlist. | CEF only |
| `BROWSER` | Opens the player's browser at the sign-in page. | Any |

## How sign-in works

1. The OAuth client builds the authorization URL from your configuration.
2. The launcher shows the provider's sign-in page using the method you chose.
3. The player signs in, and the provider redirects to the callback URL you registered with it.
4. The launcher gets the authorization `code` or `access_token` from the callback:
   * **Popup or frame:** the callback page, `oauth.html`, sends the result to the launcher window and closes the popup.
   * **Browser:** the callback URL points to the launcher's internal web server. When it's requested with a `code`, the launcher fires a `SkinApp.NewAuthCode` notification and redirects the browser to `webServer.oauth.successUrl` or `webServer.oauth.errorUrl`.

## Choose a callback URL

### Localhost callback

If your provider allows a localhost callback, set the ports the launcher's web server can use:

```yaml app.yaml theme={null}
webServer:
  listenPort:
    - 5001
    - 5002
    - 5003
```

The launcher uses the first open port. If none are open, it shows an error when it starts. Register each as an allowed callback, in the form `http://127.0.0.1:<port>/`.

To get the address and port the launcher picked, use `app.expandString("{contentUrl}")` when you build the authorization URL.

### Callback on your own domain

Some providers don't allow localhost callbacks. With CEF, the launcher can internally redirect a request for a URL on your own domain to `oauth.html`. This is called resource rewriting. Register a URL on a domain you own, then set it up in `loadOAuthConfig()` in `src/redux/OAuth.js`:

```javascript theme={null}
const rewriteUrl = skinApp.expandString("{contentUrl}oauth.html");

webGet.redirectAdd("oauth-example", webGetRedirect.RESOURCE, false,
  "^(https:\\/\\/example\\.com\\/auth)", rewriteUrl,
  webGetPcreCompileOption.NONE, webGetPcreSubstituteOption.NONE);
```

### Browser method

With the browser method, the provider redirects to the `redirectUrl` in your OAuth client. Point it to an endpoint on the launcher's internal web server, and set the same endpoint as `webServer.oauth.redirectUrl`. For example, if `redirectUrl` is `app.expandString("{contentUrl}auth2")`, set `webServer.oauth.redirectUrl` to `auth2`:

```yaml app.yaml theme={null}
webServer:
  listenPort:
    - 4545
  oauth:
    successUrl: "{contentUrl}oauth/success.html"
    errorUrl: "{contentUrl}oauth/failure.html"
    redirectUrl: auth2
  externalPaths:
    - oauth/*
```

## Set up Google sign-in

The skin comes with a sample Google configuration. Replace it with your own.

### Create the Google client

1. In the Google Cloud console, go to **APIs & Services → Credentials**.
2. Click **Create Credentials → OAuth client ID**.
3. Set **Application type** to **Web application**.
4. Under **Authorized redirect URIs**, add the callback URLs you chose.
5. Copy the client ID.

While your app is in development, add each tester to the list of users on the app's **OAuth consent screen** page.

### Configure the skin

Edit `GOOGLE_OAUTH_CONFIG` in `src/constants/OAuthConfig.js`:

```javascript theme={null}
const GOOGLE_OAUTH_CONFIG = new OAuthConfig({
  clientId: "<your-client-id>.apps.googleusercontent.com",
  storageKey: "<your storage key>",
  scope: "openid profile",
  redirectUrl: "https://example.com/auth"
});
const GOOGLE_OAUTH_OPENID_CONFIG = "https://accounts.google.com/.well-known/openid-configuration";
```

* Set `clientId` to your client ID.
* Set `redirectUrl` to the callback URL you registered.

Google requires a newer Chrome than the CEF version in DirectGVR. Set the CEF user agent in `app.yaml`:

```yaml app.yaml theme={null}
cef:
  enabled: true
  userAgent: Chrome
```

To exchange the authorization code for refresh and access tokens with `OAuthResponseType.CODE`, you need the Google client secret. See the comments in the same file and Google's [OAuth 2.0 for desktop apps](https://developers.google.com/identity/protocols/oauth2/native-app#exchange-authorization-code). `OAuthResponseType.TOKEN` doesn't return refresh tokens, so players would sign in every time they open the launcher.

Providers implement OAuth2 differently. To change how the launcher talks to yours, edit `src/oauth/OAuthClient.js`.

## Related

* [Launcher skin](https://docs.directgvr.com/launcher-design/launcher-skin.md)
* [Host configuration](https://docs.directgvr.com/reference/client-skin-system/host-configuration.md)
* [Host web server](https://docs.directgvr.com/reference/client-skin-system/host-web-server.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.