On this page

Authentication

Cognito

Amazon Cognito provider setup and usage.

Get your Cognito Credentials

To integrate with Cognito, you need to set up a User Pool and an App client in the Amazon Cognito Console.

Follow these steps:

  1. Go to the Cognito Console and create a User Pool.
  2. Under App clients, create a new App client (note the Client ID and Client Secret if enabled).
  3. Go to Domain and set a Cognito Hosted UI domain (e.g., your-app.auth.us-east-1.amazoncognito.com).
  4. In App client settings, enable:
    • Allowed OAuth flows: Authorization code grant
    • Allowed OAuth scopes: openid, profile, email
  5. Add your callback URL (e.g., http://localhost:3000/api/auth/callback/cognito).
Note
  • User Pool is required for Cognito authentication.
  • Make sure the callback URL matches exactly what you configure in Cognito.

Configure the provider

Configure the cognito key in socialProviders key of your auth instance.

auth.ts
import { entroAuth } from "@entro314labs/entro-auth";

export const auth = entroAuth({
  socialProviders: {
    cognito: {
      clientId: process.env.COGNITO_CLIENT_ID as string,
      clientSecret: process.env.COGNITO_CLIENT_SECRET as string,
      domain: process.env.COGNITO_DOMAIN as string, // e.g. "your-app.auth.us-east-1.amazoncognito.com"
      region: process.env.COGNITO_REGION as string, // e.g. "us-east-1"
      userPoolId: process.env.COGNITO_USERPOOL_ID as string,
      identityProvider: "Google", // optional: skip the hosted UI picker
    },
  },
})

Sign In with Cognito

To sign in with Cognito, use the signIn.social function from the client.

auth-client.ts
import { createAuthClient } from "@entro314labs/entro-auth/client"

const authClient = createAuthClient()

const signIn = async () => {
  const data = await authClient.signIn.social({
    provider: "cognito"
  })
}

To override the preselected identity provider per call (for example, to render a "Sign in with Okta" button that bypasses the Cognito picker), pass the identity_provider key through additionalParams:

await authClient.signIn.social({
  provider: "cognito",
  additionalParams: { identity_provider: "Okta" },
})

Additional Options:

  • scope: Additional OAuth2 scopes to request (combined with default permissions).

    • Default: "openid" "profile" "email"
    • Common Cognito scopes:
      • openid: Required for OpenID Connect authentication
      • profile: Access to basic profile info
      • email: Access to user’s email
      • phone: Access to user’s phone number
      • aws.cognito.signin.user.admin: Grants access to Cognito-specific APIs
  • Note: You must configure the scopes in your Cognito App Client settings. available scopes

  • getUserInfo: Custom function to retrieve user information from the Cognito UserInfo endpoint.

  • identityProvider: Preselect a Cognito-configured identity provider to skip the hosted-UI picker. Valid values are "COGNITO", a SAML/OIDC provider name on the User Pool, or one of "Google", "Facebook", "LoginWithAmazon", "SignInWithApple". Per-call overrides via additionalParams.identity_provider take precedence.

  • refreshAccessToken: Custom function to refresh tokens. It receives the stored refresh token.

Refresh tokens

Cognito returns a refresh token after a successful authorization code grant. Later refresh-token grants return new access and ID tokens. Cognito only returns a new refresh token when refresh token rotation is enabled in the app client; otherwise, the original refresh token remains valid and Entro Auth keeps using it.

auth.api.getAccessToken refreshes an expired access token automatically when the provider account has a refresh token and a known accessTokenExpiresAt. It returns the valid access token and ID token. If you need the refresh token in the response, use the /refresh-token endpoint instead.

In database-less setups, Entro Auth stores provider account data, including OAuth token material, in the encrypted account_data cookie when storeAccountCookie is enabled. Token refresh responses set an updated cookie, so server-side callers must forward the returned Set-Cookie header to the browser. Cognito JWTs can be large; Entro Auth chunks oversized account cookies, but browsers and proxies can still enforce total header limits. Use database-backed account storage for large token payloads or production flows that need durable token storage.

Note

For more information about Amazon Cognito's scopes and API capabilities, refer to the official documentation.