Haaremy SSO Auth SDK

2.0.0 · active · verified Wed Apr 22

The official Haaremy SSO Authentication SDK provides a comprehensive solution for integrating Haaremy's Single Sign-On system into JavaScript applications. It encapsulates token handling, login/logout flows, BroadcastChannel-based tab synchronization, and offers specific integrations for React and Next.js. Currently at version 2.0.0, this SDK is actively maintained with a focus on security and developer experience. Key differentiators include its 'zero localStorage' approach (tokens are memory-only), proactive token refresh, server-side replay detection for token families, and efficient offline JWT validation using JWKS caching with Ed25519 signatures. It offers distinct entrypoints for vanilla JS/framework-agnostic core logic, React hooks and components, and Next.js middleware/server-side helpers, making it adaptable to various application architectures.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up the `HmyAuthProvider` at your application root and use the `useAuth` hook in a child component to manage authentication state, display user information, handle login via `HmyLoginForm`, and perform logout. It showcases both authenticated and unauthenticated states.

import { HmyAuthProvider, useAuth, HmyLoginForm } from '@haaremy/auth/react';

const ssoUrl = process.env.NEXT_PUBLIC_SSO_URL ?? 'https://sso.haaremy.de'; // Ensure SSO URL is configured

// This component would typically be in your _app.tsx or layout.tsx
function AppWrapper({ children }) {
  return (
    <HmyAuthProvider config={{ ssoUrl: ssoUrl }}>
      {children}
    </HmyAuthProvider>
  );
}

// This component demonstrates how to use the auth state within your application
function MyAuthenticatedPage() {
  const { state, user, login, logout, getAccessToken } = useAuth();

  if (state === 'loading') {
    return <div>Authentifizierungsstatus wird geladen...</div>;
  }

  if (state === 'unauthenticated') {
    return (
      <div>
        <p>Sie sind nicht angemeldet.</p>
        <HmyLoginForm
          onSuccess={(loggedInUser) => {
            console.log('Login erfolgreich:', loggedInUser.display_name);
          }}
          onError={(error) => {
            console.error('Login fehlgeschlagen:', error.message);
          }}
        />
      </div>
    );
  }

  // State is 'authenticated'
  return (
    <div>
      <h1>Hallo, {user?.display_name}!</h1>
      <p>Ihre E-Mail: {user?.email}</p>
      <button onClick={logout}>Abmelden</button>
      <button onClick={async () => {
        const token = await getAccessToken();
        console.log('Aktueller Access Token:', token ? token.substring(0, 20) + '...' : 'Kein Token');
      }}>Token anzeigen</button>
    </div>
  );
}

// Example usage within a React component tree:
// function Root() {
//   return (
//     <AppWrapper>
//       <MyAuthenticatedPage />
//     </AppWrapper>
//   )
// }
// export default Root;

view raw JSON →