Authentication
This section provides a collection of recipes for building a secure authentication system with Archibald.
Sections
- Core Concepts: Deep dive into session management, providers, adapters, token handling, route protection, and SSR hydration.
- OIDC Login: The OpenID Connect authorization code flow (since v9) — flow overview, setup as in the shop template, and migration from the password flow.
- CDC Integration: A guide to integrating SAP Customer Data Cloud (CDC) as an identity provider.
- Best Practices: Security recommendations and best practices for production applications.
- API Reference: Technical reference for classes, hooks, and configuration options.
Auth Architecture Diagram
This diagram illustrates the architecture of the @archibald/auth package and its integration within the Archibald framework, covering both server-side and client-side components for web and native platforms.
Key Components
Server-Side (@archibald/auth)
AuthModule: The central module that orchestrates authentication. It's configured with providers and a strategy when registered in the mainCoreServer.AuthService: Contains the core business logic for handling login, logout, token refreshing, and user data retrieval.AuthController: Exposes the REST API endpoints (e.g.,/login,/logout,/user) that the client-side interacts with.getSession(): A utility function to access the current user's session data from anywhere in the backend (services, controllers).- Providers: Connect to a backend system (e.g., SAP Commerce Cloud, CDC) to validate credentials and fetch user data.
Client-Side (@archibald/auth)
SessionClient: A singleton service that manages the user's session state on the client.SessionClientProvider: A React Context provider, typically bootstrapped viaProviderComposer.useUser,useIsLoggedIn: Standard hooks to access the user's authentication state.RestrictedRoute: A web-specific component used to guard pages and redirect unauthenticated users.Stack.Protected: Anexpo-routerfeature used in native apps to define guarded navigation stacks.
Flow
-
Initialization:
- On the server, the
CoreServerregisters theAuthModulewith the appropriate strategy. - On the client, the
SessionClientProvider(insideProviderComposer) makes theSessionClientavailable.
- On the server, the
-
Login:
- The
useLoginhook callssessionClient.logIn(). - Which flow runs is chosen per call via
credentials.authProtocol:'oidc'(recommended since v9): the browser is redirected to the identity provider and back — see OIDC Login Flow.'password'(deprecated since v9): theSessionClient, via anAuthAdapter, sends credentials to the server'sPOST auth/loginendpoint.
- In both cases the
AuthModulevalidates via aProviderand sets secure JWE tokens in cookies or headers.
- The
-
UI Protection:
- Web: The
RestrictedRoutechecksuseIsLoggedIn()and uses<Navigate />if the user is not authenticated. - Native: The navigation layout uses
<Stack.Protected guard={isLoggedIn}>to dynamically swap available screens.
- Web: The
Key Takeaways
- Use the provided hooks: Always use the hooks provided by the
@archibald/authpackage (e.g.,useUser,useLogin,useLogOut) to interact with the user's session. - Secure token storage: Use
CookieAuthAdapterfor web applications andHeaderAuthAdapterfor native applications. - Bootstrapping: Always use
ProviderComposerin yourAppcomponent to wrap theSessionClientProvider. - Server Context: Use
getSession()in your backend services instead of passing the request object manually.