# Leantel for AI coding assistants

Leantel provides managed services for apps. Follow this file when a person asks you to add Leantel to their app. It
lists which file to read for each tool, what to ask for, which rules you must never break, and how to check your work.

## Tools

Each tool has one file. Read it in full before you change any code.

| Tool | Status | Follow |
|---|---|---|
| Leantel Auth: sign-in, sign-up, accounts, API protection, webhooks | Available | https://leantel.dev/auth.md |
| Leantel Notify: email, SMS and push | Coming soon | Don't integrate yet. Tell the person it isn't available. |

These files are plain Markdown (`text/markdown`). Read them rather than the HTML docs pages.

## Rules you must never break

1. **Never invent credentials, URLs or IDs.** Run `npx leantel init` (step 2), ask the person for the values in step 3,
   or tell them where to find them.
2. **Read endpoints from discovery** (`<issuer>/.well-known/openid-configuration`). Don't hard-code paths, and use the issuer
   exactly as given: no trailing slash, no other host.
3. **Never build a sign-in or sign-up form that collects passwords.** People sign in on Leantel's hosted pages; the app
   redirects there. Don't add password fields, password hashing or "forgot password" flows to the app.
4. **Never put the client secret in browser or mobile code,** in a file that gets committed, or in a `NEXT_PUBLIC_`/`VITE_`
   style variable. Server code reads it from the environment.
5. **Never store tokens in `localStorage` or `sessionStorage`.** Server apps keep tokens server-side behind an HttpOnly
   cookie; single-page apps keep them in memory. Only the PKCE verifier, `state` and `nonce` may sit in `sessionStorage`
   until the callback.
6. **Use a standard OpenID Connect library** (named in each quickstart). Don't hand-write the OAuth flow or JWT checks.
   Always PKCE `S256`, `state` and `nonce`.
7. **Take the tenant from the token's `tenant_id`,** never from a request parameter, header or body.
8. **Check every API request's token locally** (signature, `RS256`, `typ: at+jwt`, issuer, audience, expiry, permission).
   Never call Leantel to check a token, and never skip a check to make an error go away.
9. **Verify webhook signatures** before reading the body, and ignore events you've already handled.
10. **Don't change Leantel settings by guessing.** Tell the person exactly what to set in the console (redirect URLs, sign-out
    URL, sign-in start URL, permissions, roles, webhook URL).
11. **Don't explain or reimplement standard OAuth.** Where a detail matters, auth.md links the RFC.

## Procedure

1. **Work out the app type** from the code:

   | You find | Pattern | App type to create |
   |---|---|---|
   | Server routes that render pages, or a frontend plus its own backend | Backend signs in, browser gets a session cookie | Web app with a backend |
   | A browser-only app (React, Vue, Svelte) with no server of its own | Browser signs in with PKCE, tokens in memory | Single-page app |
   | iOS, Android, Flutter, React Native or desktop | System browser with PKCE | Mobile or desktop app |
   | A backend that calls an API with no person signed in | Client credentials | Backend service |
   | Only an API that other apps call | Check tokens only | No app needed; ask for the issuer and audience |

   If there's a backend, prefer the backend pattern even when the frontend is a single-page app.

2. **React (Vite) or Next.js: run `npx leantel init` yourself** in the app's folder, then follow the
   [React](https://leantel.dev/quickstart/react.md) or [Next.js](https://leantel.dev/quickstart/nextjs.md) quickstart in place
   of steps 3 and 4. Tell the person first: "A browser window will open; sign in to Leantel there and pick or create a workspace."
   `init` creates the app and its API and writes `.env` (Vite) or `.env.local` (Next.js) with every value, secrets
   included, and never prints a secret: don't read the env file back into the chat. If no browser opens (a remote or
   sandboxed shell), run `npx leantel init --no-browser` and give the person the URL it prints. Exit code 0 means done,
   1 failed (read the message), 2 the person cancelled.

3. **Other stacks: ask the person for these values**, using this message:

   > To connect Leantel, open the Leantel console, create an app of type "<type>", and send me:
   > the issuer, the client ID, the API audience, and the redirect URL you registered (it must be `<your callback URL>`).
   > Paste the client secret straight into your `.env` file as `LEANTEL_CLIENT_SECRET`, not into this chat.
   > Also add `<your home URL>` under Sign-out redirect URLs and `<your sign-in URL>` as the sign-in start URL.

   Use the variable names in auth.md section 1 exactly. Add them to `.env.example` with empty values. Check the redirect URL
   against the rules in auth.md section 1 before you send the message.

4. **Follow the guide** for the pattern: auth.md sections 3–5 or 9, plus the quickstart for the stack if there is one
   ([Go](https://leantel.dev/docs/quickstart/go), [TypeScript](https://leantel.dev/docs/quickstart/typescript),
   [JavaScript](https://leantel.dev/docs/quickstart/javascript), [Python](https://leantel.dev/docs/quickstart/python),
   [Java and Spring](https://leantel.dev/docs/quickstart/java-spring), [Kotlin and Spring](https://leantel.dev/docs/quickstart/kotlin-spring)). For another stack, use that stack's best-known
   OpenID Connect client and JWT libraries and apply the same parameters and checks.

5. **Protect the API** (auth.md section 6). Every route checks the token. Everyone who signs up gets `app:use` through the
   role `member`, which Set up Auth creates. For a route that needs more, ask the person to create the permission in the
   console (APIs and permissions), named `resource:action`, for example `orders:read`, and a role that grants it (Roles).
   People get permissions only through roles.

6. **Webhooks, if the app keeps its own user records** (auth.md section 8): add the endpoint, then ask the person to set the
   webhook URL and paste the signing secret into `.env` as `LEANTEL_WEBHOOK_SECRET`.

7. **Handle errors** as auth.md section 11 lists, and store only what section 12 allows.

8. **Check your work** (auth.md sections 13 and 14) before saying you're done:
   - The app builds and its existing tests pass.
   - Signing in goes to Leantel's page and returns signed in; signing out returns to the sign-out URL.
   - An API call without a token gets 401; on a route that needs a permission, a token without it gets 403.
   - No secret appears in source control, browser bundles or logs (`git grep` for the secret's first characters).
   - Add or update tests for the token checks: valid token passes, wrong audience fails, missing permission fails.

9. **Report back** in a short list: what you changed, the environment variables you added, every console setting the person
   still has to make, and any checklist item you couldn't verify.
