CAS Plugin for Better Auth

0.1.8 · active · verified Wed Apr 22

The `better-auth-cas` package, currently at version `0.1.8`, is an early-stage plugin designed to integrate Central Authentication Service (CAS) functionality into the `better-auth` authentication framework. It provides standard CAS sign-in and callback endpoints, handles CAS ticket validation via `serviceValidate`, and includes robust profile mapping capabilities to translate CAS user attributes into `better-auth` user and session data. A key feature is the optional `onProfileResolved` hook, enabling custom persistence logic. It emphasizes solutions for common CAS integration challenges, such as managing CAS's separate SSO session cookies to prevent "auto-login" after a local logout and addressing `service` URL allowlist mismatches in CAS deployments. While in a pre-1.0 state, implying potential API changes, its initial release (v0.1.0 on 2026-03-19) indicates active development and a focused approach to providing a ready-to-use CAS solution for `better-auth` applications, shipping with TypeScript types for better developer experience.

Common errors

Warnings

Install

Imports

Quickstart

Initializes `better-auth` with the standard CAS plugin, demonstrating how to configure the CAS base URL, callback path, and provider ID. It also highlights optional configuration for `redirectUri` and `profileMapping` using environment variables.

import { betterAuth } from 'better-auth';
import { createStandardCasPlugin } from 'better-auth-cas';

// In a production environment, casBaseUrl should come from environment variables.
const casBaseUrl = process.env.CAS_BASE_URL || 'https://cas.example.com/cas';
const callbackPath = process.env.CAS_CALLBACK_PATH || '/api/auth/cas/callback';
const providerId = process.env.CAS_PROVIDER_ID || 'cas';

export const auth = betterAuth({
  emailAndPassword: {
    enabled: true,
  },
  plugins: [
    createStandardCasPlugin({
      casBaseUrl: casBaseUrl,
      callbackPath: callbackPath,
      providerId: providerId,
      // Optional: Set redirectUri if your CAS server requires an exact match for the service URL
      // redirectUri: process.env.CAS_REDIRECT_URI || 'http://localhost:3000/api/auth/cas/callback',
      // Optional: Custom profile mapping if CAS attributes differ from defaults
      // profileMapping: {
      //   stableId: ['sAMAccountName', 'uid'],
      //   name: ['displayName'],
      //   email: ['mail'],
      //   image: ['thumbnailPhoto']
      // }
    }),
  ],
});

view raw JSON →