# `BaileysEx.Auth.FilePersistence`
[🔗](https://github.com/jeffhuen/baileys_ex/blob/main/lib/baileys_ex/auth/file_persistence.ex#L1)

Baileys-compatible multi-file auth persistence mirroring `useMultiFileAuthState`.

Use `BaileysEx.Auth.NativeFilePersistence` for the recommended durable file
backend in Elixir-first applications. Use this module when you need the
Baileys-shaped JSON file layout and helper semantics.

Treat this backend as a compatibility bridge for migrating existing
Baileys JS sidecar deployments onto BaileysEx. It is intentionally kept
separate from the native durable backend so the Elixir-first path can remain
idiomatic, and it can be retired in a future major release once users no
longer depend on the Baileys JSON helper contract.

This module does not migrate existing auth directories automatically. If you
switch an existing linked device from this backend to the native backend,
migrate once with `BaileysEx.Auth.PersistenceMigration` or re-pair on the new
backend.

The built-in file lock here is `:global.trans`, which coordinates file access
inside one BEAM cluster. Treat one auth directory as owned by one runtime at a
time; this module is not a distributed storage protocol.

# `multi_file_auth_state`

```elixir
@type multi_file_auth_state() :: %{
  state: BaileysEx.Auth.State.t(),
  connect_opts: keyword(),
  save_creds: (BaileysEx.Auth.State.t() | map() -&gt; :ok | {:error, term()})
}
```

Helper map returned by `use_multi_file_auth_state/1`.

`connect_opts` is ready to merge into `BaileysEx.connect/2`, and `save_creds`
persists the latest auth-state snapshot back into the Baileys-compatible JSON
directory.

# `delete_keys`

```elixir
@spec delete_keys(atom(), term()) :: :ok | {:error, term()}
```

Deletes a Signal key record by type and ID from the default directory.

# `delete_keys`

```elixir
@spec delete_keys(Path.t(), atom(), term()) :: :ok | {:error, term()}
```

Deletes a Signal key record by type and ID from the specified directory.

# `load_credentials`

```elixir
@spec load_credentials() :: {:ok, BaileysEx.Auth.State.t()} | {:error, term()}
```

Loads the core credentials state from the default configured directory.

# `load_credentials`

```elixir
@spec load_credentials(Path.t()) :: {:ok, BaileysEx.Auth.State.t()} | {:error, term()}
```

Loads the core credentials state from the given path.

# `load_keys`

```elixir
@spec load_keys(atom(), term()) :: {:ok, term()} | {:error, term()}
```

Loads a Signal key record by type and ID from the default directory.

# `load_keys`

```elixir
@spec load_keys(Path.t(), atom(), term()) :: {:ok, term()} | {:error, term()}
```

Loads a Signal key record by type and ID from the given directory.

# `save_credentials`

```elixir
@spec save_credentials(BaileysEx.Auth.State.t()) :: :ok | {:error, term()}
```

Saves the core credentials state into the default directory.

# `save_credentials`

```elixir
@spec save_credentials(Path.t(), BaileysEx.Auth.State.t()) :: :ok | {:error, term()}
```

Saves the core credentials state into the given directory path.

# `save_keys`

```elixir
@spec save_keys(atom(), term(), term()) :: :ok | {:error, term()}
```

Saves a Signal key record by type and ID into the default directory.

# `save_keys`

```elixir
@spec save_keys(Path.t(), atom(), term(), term()) :: :ok | {:error, term()}
```

Saves a Signal key record by type and ID into the specified directory.

# `use_multi_file_auth_state`

```elixir
@spec use_multi_file_auth_state(Path.t()) ::
  {:ok, multi_file_auth_state()} | {:error, term()}
```

Loads the auth state and returns the runtime options needed to mirror
Baileys' multi-file auth helper with the built-in file-backed Signal store.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
