Okta Strategy for Remix Auth

1.2.0 · active · verified Wed Apr 22

This package provides an authentication strategy for integrating Okta with Remix applications through the `remix-auth` library. It extends the `OAuth2Strategy` to handle Okta's specific OAuth 2.0 and OpenID Connect flows, supporting both Node.js and Cloudflare runtimes. The current stable version is 1.2.0, with updates generally following `remix-auth`'s release cadence and Okta API changes. `remix-auth-okta` enables developers to quickly set up user authentication against an Okta account, managing the redirect to Okta for login and processing the callback. Its key differentiators include tight integration with the `remix-auth` ecosystem, offering a standardized approach to adding Okta authentication, and flexibility to support both Okta's hosted login page and custom login forms within the Remix application.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates the core setup of `remix-auth-okta`. It initializes the `OktaStrategy` using environment variables for sensitive credentials, configures a basic `Authenticator` instance with a session storage, and defines the post-authentication callback to process user profile data from Okta.

// app/utils/auth.server.ts
import { Authenticator } from "remix-auth";
import { OktaStrategy } from "remix-auth-okta";
import { createCookieSessionStorage } from "@remix-run/node"; // Example for session storage

// Define your user type that will be stored in the session
interface AppUser { id: string; email: string; }

// Configure session storage (replace with your actual session setup)
const sessionStorage = createCookieSessionStorage({
  cookie: {
    name: "_session",
    httpOnly: true,
    secure: process.env.NODE_ENV === "production", // Use secure cookies in production
    secrets: [process.env.SESSION_SECRET ?? "s3cr3t"], // Must provide a secret
    maxAge: 60 * 60 * 24 * 7, // 7 days
  },
});

// Create an instance of the authenticator
export const authenticator = new Authenticator<AppUser>(sessionStorage);

// Initialize the Okta Strategy with environment variables
const oktaStrategy = new OktaStrategy(
  {
    issuer: process.env.OKTA_ISSUER ?? 'YOUR_OKTA_ISSUER_MISSING',
    clientID: process.env.OKTA_CLIENT_ID ?? 'YOUR_OKTA_CLIENT_ID_MISSING',
    clientSecret: process.env.OKTA_CLIENT_SECRET ?? 'YOUR_OKTA_CLIENT_SECRET_MISSING',
    callbackURL: process.env.OKTA_CALLBACK_URL ?? 'http://localhost:3000/auth/okta/callback',
  },
  async ({ accessToken, refreshToken, extraParams, profile }) => {
    // This callback runs after a successful Okta authentication.
    // Here, you would typically find or create a user in your database
    // based on the profile information (e.g., profile.email).
    console.log("Okta Profile:", profile);
    // Return a user object that will be stored in the session.
    return { id: profile.id, email: profile.email ?? 'unknown@example.com' };
  }
);

// Register the strategy with a unique name (e.g., "okta")
authenticator.use(oktaStrategy, "okta");

view raw JSON →