> For the complete documentation index, see [llms.txt](https://seekspeed.gitbook.io/seekspeed-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://seekspeed.gitbook.io/seekspeed-docs/security/auth-rls.md).

# Auth & RLS

Three auth flows, one row-level-security model.

## Flows

1. **Email + password** — standard Supabase auth.
2. **Google OAuth** — `redirect_uri` is always same-origin (`${window.location.origin}/auth/callback`); intended-path is stored in `sessionStorage` and restored after the session hydrates.
3. **Solana wallet** — sign a server-issued nonce with Phantom/Solflare; the server verifies with `tweetnacl` and mints a Supabase session via a deterministic internal email. See [Solana Wallet Auth](/seekspeed-docs/security/solana.md).

## RLS pattern

Every table that holds user data is gated on `workspaces.owner_id = auth.uid()`. Example for `agents`:

```sql
CREATE POLICY "owner reads agents"
  ON public.agents FOR SELECT TO authenticated
  USING (workspace_id IN (SELECT id FROM public.workspaces WHERE owner_id = auth.uid()));

CREATE POLICY "owner writes agents"
  ON public.agents FOR INSERT TO authenticated
  WITH CHECK (workspace_id IN (SELECT id FROM public.workspaces WHERE owner_id = auth.uid()));

-- and the same for UPDATE / DELETE
```

The `service_role` is the only role that can bypass RLS, and it's only used inside server functions for narrow administrative paths (cleanup jobs, share-token verification).

## Roles

Roles are stored in a dedicated `user_roles` table — never on the profile or workspace — and checked via a `SECURITY DEFINER` helper:

```sql
create function public.has_role(_user_id uuid, _role app_role)
returns boolean language sql stable security definer set search_path = public as $$
  select exists (
    select 1 from public.user_roles where user_id = _user_id and role = _role
  )
$$;
```

This prevents privilege-escalation by tampering with profile rows and avoids RLS recursion when policies need to consult role membership.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://seekspeed.gitbook.io/seekspeed-docs/security/auth-rls.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
