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

# Enable authentication

> Turn Portal-managed end user authentication on for an environment and configure which sign-in methods it accepts.

Authentication is configured per environment from the
[Portal Admin Dashboard](https://app.portalhq.io). Your Development and
Production environments have separate settings, separate credentials, and
separate Auth Environment IDs, so you can enable a method in Development without
affecting live users.

## Open the configuration page

Step 1: Log in to the [Portal Admin Dashboard](https://app.portalhq.io) and
select the environment you want to configure.

Step 2: Click **Configure** in the left sidebar, under Authentication.

<Frame>
  <img src="https://mintcdn.com/portal-003221ec/UBGZngsyXL4rYrpA/images/authentication/configure-page.png?fit=max&auto=format&n=UBGZngsyXL4rYrpA&q=85&s=2cd5ca27c0d5c2086f2d0d46145fe9da" alt="Authentication Configure page showing the Enabled toggle and the General, TOTP, Email Magic Link, Google OAuth, and Apple OAuth sections" width="880" height="702" data-path="images/authentication/configure-page.png" />
</Frame>

Step 3: Turn on **Enabled**. Until you do, the rest of the page stays hidden and
every Authentication API request for this environment is rejected with `401`.

<Frame>
  <img src="https://mintcdn.com/portal-003221ec/UBGZngsyXL4rYrpA/images/authentication/enable-auth.png?fit=max&auto=format&n=UBGZngsyXL4rYrpA&q=85&s=77dcfb75e84912b8e42f946d2814fde1" alt="Enable toggle in Configure page" width="1104" height="534" data-path="images/authentication/enable-auth.png" />
</Frame>

## Auth Environment ID

The **Auth Environment ID** is the value your app sends in the
`x-portal-auth-environment-id` header on every Authentication API request. Portal
generates it for you and it does not change. Copy it from this section.

<Frame>
  <img src="https://mintcdn.com/portal-003221ec/UBGZngsyXL4rYrpA/images/authentication/auth-environment-id.png?fit=max&auto=format&n=UBGZngsyXL4rYrpA&q=85&s=b87bc1fbe247e8041c39bd67b840b911" alt="Auth Environment ID section with the generated identifier and a copy button" width="1044" height="292" data-path="images/authentication/auth-environment-id.png" />
</Frame>

Each environment has its own Auth Environment ID. You will also need it when
registering redirect URIs with Google and Apple.

## General

**Auto-create wallet** records whether your app should create a wallet for a user
as soon as they finish signing in. This is a boolean that is internally used in
Portal SDKs to signal if a wallet should be created at the time an End User is
created.

**Redirect URLs** is the allow list of pages in your app that a sign-in is
allowed to return a user to. Add every callback page you use, for example
`https://example.com/callback`.

<Frame>
  <img src="https://mintcdn.com/portal-003221ec/UBGZngsyXL4rYrpA/images/authentication/redirect-urls.png?fit=max&auto=format&n=UBGZngsyXL4rYrpA&q=85&s=462c1152329e42e61b7116e4cb41cabc" alt="Redirect URLs section listing two allowed callback URLs with add and remove controls" width="1044" height="504" data-path="images/authentication/redirect-urls.png" />
</Frame>

<Warning>
  Redirect URLs are matched by exact string comparison. Wildcards and path
  prefixes are not supported, and `https://example.com/callback` does not match
  `https://example.com/callback/` or `https://example.com/callback?next=/home`. Add
  each exact URL you intend to pass as `redirectUrl`.
</Warning>

A request whose `redirectUrl` is not on this list is rejected with `401`. This is
what stops someone who has your Auth Environment ID from redirecting your users
and their sign-in tokens to a site they control.

## Choose sign-in methods

Each method has its own section with its own toggle. Enabling a method adds it to
the `allowedAuthMethods` array returned by `GET /auth/methods`.

* **Email Magic Link.** No credentials to enter here, but before you can send
  anything you need a verified sending domain and an email template. See
  [Email magic links](/resources/authentication/email-magic-links).
* **Google OAuth.** Requires a Client ID and Client secret from Google Cloud
  Console. See [Google OAuth](/resources/authentication/google-oauth).
* **Apple OAuth.** Requires a Team ID, App ID, Service ID, Key ID, and private
  key from your Apple Developer account. See
  [Apple OAuth](/resources/authentication/apple-oauth).

### Provider credentials are write-only

Portal encrypts provider secrets and never returns them. If you need to check
whether the credentials are configured or not, you can check the tag on the labels of
the form. You can still replace the value if needed.

<Frame>
  <img src="https://mintcdn.com/portal-003221ec/UBGZngsyXL4rYrpA/images/authentication/google-oauth-section.png?fit=max&auto=format&n=UBGZngsyXL4rYrpA&q=85&s=77f2bc1e430ecc89851cd13176efe98e" alt="Google OAuth section with the enable toggle and write-only Client ID and Client secret fields showing configured badges" width="1050" height="648" data-path="images/authentication/google-oauth-section.png" />
</Frame>

## Require two-factor authentication

The **TOTP** section adds a second factor to every sign-in for this environment,
whichever method the user chose.

<Frame>
  <img src="https://mintcdn.com/portal-003221ec/UBGZngsyXL4rYrpA/images/authentication/totp-section.png?fit=max&auto=format&n=UBGZngsyXL4rYrpA&q=85&s=fd88a78c01f44a0109e3e7b0575f2d3a" alt="TOTP section with the Require TOTP toggle and the TOTP issuer field" width="1050" height="502" data-path="images/authentication/totp-section.png" />
</Frame>

* **Require TOTP** turns the second factor on.
* **TOTP issuer** is the name shown next to the code in the user's authenticator
  app. Use your product name.

Turning this on changes the shape of your sign-in flow, so read
[Two-factor authentication](/resources/authentication/two-factor-authentication)
before enabling it in Production.

## Verify your configuration

Call `GET /auth/methods` with your Auth Environment ID to confirm the environment
is live and reporting the methods you expect:

```bash theme={null}
curl --request GET \
  --url https://api.portalhq.io/api/v3/auth/methods \
  --header 'x-portal-auth-environment-id: <AUTH_ENVIRONMENT_ID>'
```

```json theme={null}
{
  "data": {
    "allowedAuthMethods": ["EMAIL_MAGIC_LINK", "GOOGLE"],
    "autoCreateWallet": true
  }
}
```

A `401` here means the header does not match an environment, or **Enabled** is
still off.

## Next steps

<Card title="Email magic links" icon="envelope" href="/resources/authentication/email-magic-links">
  Verify a sending domain and build your magic link email.
</Card>

<Card title="Google OAuth" icon="google" href="/resources/authentication/google-oauth">
  Create a Google OAuth client and register Portal's redirect URI.
</Card>

<Card title="Apple OAuth" icon="apple" href="/resources/authentication/apple-oauth">
  Set up a Services ID and Sign in with Apple key.
</Card>

<Card title="End users" icon="users" href="/resources/authentication/end-users">
  Review who has signed in and manage their two-factor enrollment.
</Card>
