# E-mail and passwords

> Sign-up with or without a password, e-mail confirmation, sign-in, recovery, magic links and codes, and changing e-mail or password safely.

Applies to: the Sypnia engine (open source, self-hosted) and Sypnia Cloud (paid service).  
Source: https://data.docs.sypnia.com/auth/email-and-passwords/

For agents: E-mail auth flows: signUp (password optional: 'e-mail first' sends a link that leads to create-your-password), confirmation required by default, signInWithPassword, signInWithOtp (one e-mail with a magic link and a 6-digit code), verifyOtp, resetPasswordForEmail (always 200), updateUser for password (recent sign-in or reauthentication nonce) and e-mail (confirmed on both addresses). Password policy: min length 10, optional character rules, breached-password check. Passwords hashed with argon2id.

## Sign-up

Two ways, both with `POST /auth/v1/signup`:

|                         | You send              | What happens                                                                                                                            |
| ----------------------- | --------------------- | --------------------------------------------------------------------------------------------------------------------------------------- |
| **E-mail and password** | `{ email, password }` | The user exists, unconfirmed. The e-mail asks to confirm.                                                                               |
| **E-mail first**        | `{ email }`           | The e-mail asks to confirm **and create a password**. The link opens your «create your password» screen with a session ready to set it. |

```ts
await sypnia.auth.signUp({ email: 'ana@example.com', options: { data: { name: 'Ana' }, emailRedirectTo: 'https://myapp.com/welcome' } });
```

`options.data` goes to `user_metadata`. `emailRedirectTo` must match one of the project's **redirect URLs**: exact addresses or patterns such as `https://*.myapp.com/**`.

Confirmation is required by default (`require_email_confirmation`). Turn it off only for internal tools: an unconfirmed address can belong to someone else.

## Confirmation links and codes

Every auth e-mail carries a **link** and a **6-digit code**, so a person can confirm on another device by typing the code.

- Links and codes work **once**, for **one hour** (configurable from 5 minutes to 24 hours), and are stored only as hashes.
- A code allows 5 attempts; then a new e-mail is needed.
- `GET /auth/v1/verify?token_hash=…&type=signup&redirect_to=…` confirms and redirects to your app with the session in the address fragment (`#access_token=…&refresh_token=…`). `@sypnia/data` reads it and clears it from the address bar.

## Sign-in

```ts
await sypnia.auth.signInWithPassword({ email, password });   // POST /auth/v1/token?grant_type=password
await sypnia.auth.signInWithOtp({ email });                  // POST /auth/v1/otp: link + code, no password
await sypnia.auth.verifyOtp({ email, token: '483920', type: 'email' });
```

A wrong password and an unknown e-mail get the same answer, `invalid_credentials`, so nobody can test which e-mails have accounts.

## Forgotten password

`resetPasswordForEmail(email, { redirectTo })` always answers 200. If the account exists, the e-mail carries a recovery link; it signs the person in with a short **recovery session** whose only use is `updateUser({ password })`.

## Changing password or e-mail

- **Password**: allowed if the person signed in less than 10 minutes ago; otherwise call `reauthenticate()` (a code by e-mail) and pass it as `nonce`. Every other session of the user is ended, and a «your password changed» e-mail goes out.
- **E-mail**: a confirmation goes to **both** the old and the new address; the change applies when both are confirmed. A user without an e-mail confirms only the new one.

## Password policy

| Setting                 | Default                                                                                                                                |
| ----------------------- | -------------------------------------------------------------------------------------------------------------------------------------- |
| Minimum length          | 10 (8 to 128)                                                                                                                          |
| Required characters     | none; optional lower, upper, digit, symbol                                                                                             |
| Breached-password check | on: the first 5 characters of the SHA-1 hash are checked against Have I Been Pwned (k-anonymity: the password never leaves the engine) |

Passwords are hashed with **argon2id** (19 MiB memory, 2 iterations).
