> ## Documentation Index
> Fetch the complete documentation index at: https://momentic.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Cache authenticated sessions

> Wrap your login flow in an auth module so parallel tests only authenticate once.

Running the full login flow for every test hits auth provider rate limits and
wastes time. Wrap it in a module, enable caching, and Momentic reuses the saved
browser state across runs.

<Tip>
  New to auth? Start with [Authentication strategies](/guides/auth/overview) for
  the full set of options and trade-offs.
</Tip>

## Setup

1. Extract your login steps into a **Module**. The last step should verify the
   authenticated state (e.g. a **Page check** for the dashboard URL).
2. In the module config, enable **Caching globally**
   (`defaultCacheAllInvocations`) and **Treat as auth module** (`autoAuth`).
3. Set a **Default cache key** (`defaultCacheKey`, e.g. `admin-user`). Parallel
   tests sharing the key reuse the same cached session.
4. Set a **Default expiry** (`defaultCacheTtl`, in milliseconds) shorter than
   your session lifetime.
5. Remove any **Save auth state** / **Load auth state** (`authSave` /
   `authLoad`) steps. A cached auth module handles this automatically - see
   [Save and restore a state file](/guides/auth/overview#2-save-and-restore-a-state-file-authsave-authload).

The module's stored config looks like this:

```yaml log-in.module.yaml theme={null}
fileType: momentic/module/v2
id: log-in
name: Log in
autoAuth: true # Treat as auth module
defaultCacheAllInvocations: true # Caching globally
defaultCacheKey: admin-user # Default cache key
defaultCacheTtl: 3600000 # Default expiry (1 hour, in ms)
steps:
  - type:
      text: "{{ env.USERNAME }}"
      into: Email input
  - type:
      text: "{{ env.PASSWORD }}"
      into: Password input
  - click: Log in
  - checkPageContains:
      text: Dashboard # verify the login worked before the session is cached
```

Invoke it from each test's
[`before` section](/core-concepts/test-format#before-and-after-sections):

```yaml checkout.test.yaml theme={null}
before:
  - module: ../modules/log-in.module.yaml
```

<Tip>
  Use different cache keys for different user roles (e.g. `admin`, `member`) so
  state doesn't carry over between them.
</Tip>

## How it works

Momentic captures cookies, `localStorage`, and IndexedDB when the module
finishes. On subsequent runs within the expiry window, it restores that state
and skips the module's steps.

## Related

* [Modules](/core-concepts/modules#authentication-modules)
* [Email OTP](/guides/auth/email-otp)
