Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions docs/design.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,11 @@ part in it.
The app's `convex/http.ts` calls `routeAll`, which routes the site's action, one `pathPrefix: "/"`
route per method Convex routes, because every request reaches a Cloudflare Worker's asset router: a
file answers GET, another method on a file answers 405, and a request that matches no file goes to
the app's server. A server-rendered app with Convex Auth adds one exact `POST` route per `/auth`
path: the Convex Auth component holds the `/auth` prefix, where it serves the key set, and Convex
picks the longest matching prefix, so only an exact route reaches the app there. Convex answers
`HEAD` through the `GET` handler and drops the body.
the app's server. A server-rendered app with Convex Auth adds no route for its `/auth` paths: Convex
Auth 2.0.0's core serves no HTTP routes, so the catch-all reaches the app's server there. Convex
picks an exact route first, then the longest matching prefix, so an OAuth component's `httpPrefix`,
such as `/auth/github`, still takes its callback. Convex answers `HEAD` through the `GET` handler
and drops the body.

```ts
import { routeAll } from "@samebase/convex-hosting";
Expand Down Expand Up @@ -530,7 +531,7 @@ The session pieces moved from the adapter's `convex-auth` entry to the auth pack
| The gate | Auth package: `redirectWithoutSession` and `RedirectWithoutSession`; the app gives the redirect options | Only the server render has the cookie; the browser follows the provider |
| The session state in components | Auth package: `SignedIn` and `SignedOut` | Convex's provider reports loading during the server render and the hydration |
| The cookie session lines in `src/server.ts` | Convex Auth's own API, written in the app | A helper for HTTP actions belongs upstream |
| The `/auth/*` routes | Convex Auth's own API (`setupConvexAuthServer`), answered by `src/server.ts`; `convex/http.ts` forwards the three `POST`s | The handlers are Convex Auth's; the paths are the app's, and the cookies belong to the site's origin |
| The `/auth/*` routes | Convex Auth's own API (`setupConvexAuthServer`), answered by `src/server.ts`; the catch-all of `routeAll` reaches it | The handlers are Convex Auth's; the paths are the app's, and the cookies belong to the site's origin |
| The Convex Auth setup files | App, with Convex Auth's own API | The providers and the users table are the app's |
| Deploy and preview commands | Hosting package: `convex-hosting deploy`, in the app's scripts and CI | The upload must run between the build and the push |

Expand All @@ -552,7 +553,7 @@ or ChessMerge goes; the list is checked at every release. At 0.8.0 and 0.5.0 tha
| adapter `vite`: `convexSsr()` | `vite.config.ts` of server-rendered apps | The `ssr` environment settings that make Start's server build run in Convex's runtime, the `node:stream` stub included |
| adapter `server` | `src/server.ts` of server-rendered apps | Start's own stream handler, typed as TanStack's `RequestHandler<Register>`, so `fetch(request, { context })` takes the app's registered context; pages stream as on a Worker |
| adapter `server-build` (types) | `convex/http.ts` of server-rendered apps | The type of the built server module: `{ fetch(request) }`, the shape of the app's `src/server.ts` |
| auth `vite`: `convexAuth()` | `vite.config.ts` of both SSR examples, next to `convexSsr()` or Cloudflare's plugin | Resolves the Convex Auth 2.0.0-beta.0 file that holds the SSR provider, which its exports map does not expose, and keeps one copy of Convex Auth in development |
| auth `vite`: `convexAuth()` | `vite.config.ts` of both SSR examples, next to `convexSsr()` or Cloudflare's plugin | Resolves the Convex Auth 2.0.0 file that holds the SSR provider, which its exports map does not expose, and keeps one copy of Convex Auth in development |
| auth `.`: `sessionBeforeLoad` | The root route | The same `beforeLoad` in every app with cookie sessions |
| auth `.`: `SessionProvider` | The root component | Convex Auth's provider with the server token, plus the release of `expectAuth` for a signed-out visitor |
| auth `.`: `redirectWithoutSession`, `RedirectWithoutSession` | The `_authed` layout | The server render has the cookie; the browser follows the provider; two moments, two pieces |
Expand Down
2 changes: 1 addition & 1 deletion docs/domains.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Cloudflare, make the `CNAME` DNS only; Convex documents a proxied record as an
30 minutes.

Do not choose the domain under "Override Environment Variables". That changes `CONVEX_SITE_URL`
everywhere in the app, and Convex Auth uses it as the token issuer and in its redirect URLs.
everywhere in the app, and Convex Auth's OAuth components use it in their callback URLs.

To move a domain from another host, such as a Worker's custom domain, add the `TXT` record first,
then replace the old record with the `CNAME` right away. While the name has no record, resolvers
Expand Down
3 changes: 2 additions & 1 deletion docs/workers.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,5 @@ with:
the production build variables, and the project's preview deploy key in Previews Base. Builds
reads the Node and pnpm versions from the repository's `.node-version` and `packageManager`.

The example's README describes the Convex side: the signing keys of production and of previews.
The Convex side needs nothing else: each deployment, previews included, signs the session tokens
with its own key.
33 changes: 13 additions & 20 deletions examples/tanstack-ssr-workers.patch
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
diff --git a/convex/convex.config.ts b/convex/convex.config.ts
index 1831f4b4fa5703fe1332d9cf45fb2308a57be703..dcc4d5f09735722e1164710651a0c69d58a01869 100644
index f1de5f3eb4076bb0989528ea47c6b3d6c3f0727c..9fafe59d4ef5e805e2b6a168a6d646b985d1c278 100644
--- a/convex/convex.config.ts
+++ b/convex/convex.config.ts
@@ -2,7 +2,6 @@ import { defineApp } from "convex/server";
import { v } from "convex/values";
@@ -2,11 +2,9 @@ import { defineApp } from "convex/server";
import auth from "@convex-dev/auth/convex.config";
import anonymous from "@convex-dev/auth/components/anonymous/convex.config";
import authPassword from "@convex-dev/auth/components/password/convex.config";
import authUsername from "@convex-dev/auth/components/username/convex.config";
-import hosting from "@samebase/convex-hosting/convex.config";

// A push fails while either variable is missing on the deployment.
const app = defineApp({
@@ -13,5 +12,4 @@ app.use(auth, {
env: { AUTH_PRIVATE_KEY: app.env.AUTH_PRIVATE_KEY, AUTH_JWKS: app.env.AUTH_JWKS },
});
app.use(anonymous);
const app = defineApp();
app.use(auth);
app.use(authPassword);
app.use(authUsername);
-app.use(hosting);
export default app;
diff --git a/convex/http.ts b/convex/http.ts
deleted file mode 100644
index 4ae60e6c413b09f9d22481a9cba1b67b633b2529..0000000000000000000000000000000000000000
index c9e6b0a32b33e4806a813d1d311e98ee44334735..0000000000000000000000000000000000000000
--- a/convex/http.ts
+++ /dev/null
@@ -1,21 +0,0 @@
@@ -1,16 +0,0 @@
-import { httpRouter } from "convex/server";
-import { routeAll } from "@samebase/convex-hosting";
-import { components } from "./_generated/api.js";
Expand All @@ -34,13 +32,8 @@ index 4ae60e6c413b09f9d22481a9cba1b67b633b2529..00000000000000000000000000000000
-
-// Every method, every path: a file for a path that has one (another method than GET or HEAD on it
-// answers 405), the app's server (src/server.ts) for every other path, as a Cloudflare Worker with
-// static assets does.
-const handler = routeAll({ http, component: components.hosting, assets, worker: server });
-// Exact routes for the three auth paths: the Convex Auth component holds the /auth prefix, where it
-// serves the key set, and the longest prefix wins, so the catch-all above never sees them.
-for (const path of ["/auth/signin", "/auth/refresh", "/auth/signout"]) {
- http.route({ path, method: "POST", handler });
-}
-// static assets does. The /auth routes are the app's server's too: Convex Auth serves no HTTP routes.
-routeAll({ http, component: components.hosting, assets, worker: server });
-
-export default http;
diff --git a/package.json b/package.json
Expand Down Expand Up @@ -87,7 +80,7 @@ index 011481b677588aa747f29f9a0945abe4b382e3bd..8eb172e64d333d2718fca213710cf8ea
}
}
diff --git a/src/server.ts b/src/server.ts
index c4c4df479192d2a3feb175659b9de1420d5a8b8f..a6c3d84c461c20ddc45b4eaeb577c5e92abdad29 100644
index eb6502e41bcd04c3e1b8f20df3ce2fb5f88e0046..f13b1405e9ccc54710aecb61f29acd62d16b7bd0 100644
--- a/src/server.ts
+++ b/src/server.ts
@@ -1,5 +1,5 @@
Expand Down
34 changes: 16 additions & 18 deletions examples/tanstack-ssr-workers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ own Convex project.

## Kinds of pages

The demo data is a to-do list in `convex/todos.ts`, after the one in `samebase/base`: each anonymous
user gets a readable name when it is created, todos have an author and a done state, and a public
site needs caps on text length and todos per user.
The demo data is a to-do list in `convex/todos.ts`, after the one in `samebase/base`: each user is
named by the username they signed up with, todos have an author and a done state, and a public site
needs caps on text length and todos per user.

- `/about`: a static file from the build. `vite.config.ts` has TanStack Start prerender this page
only, into `dist/client/about/index.html`, and Cloudflare serves it like any other file: `/about`
Expand All @@ -37,7 +37,8 @@ site needs caps on text length and todos per user.
`todos.list`, the form adds one with `todos.add`, and a checkbox toggles `todos.toggle`. The page
sits behind the pathless layout in `src/routes/_authed.tsx`: without a session, the server
redirects to `/signin`, and the browser does the same when the session ends.
- `/signin`: a button that signs in anonymously and opens `/todos`.
- `/signin`: a username and a password, with a button that signs in and one that signs up, and then
opens `/todos`.
- The header on every page: the root route's loader queries `users.me`, so the first HTML response
of a rendered page shows `Signed in as <name>` with a sign-out button, or a link to `/signin`. The
build has no session, so on `/about` the header fills in after React hydrates.
Expand All @@ -64,10 +65,16 @@ none. `expectAuth` holds queries only before the first authentication, so sign-o
The cookies are `Secure` when the request came over HTTPS, and not in `pnpm run dev`, which serves
plain HTTP.

The example signs in anonymously: each sign-in creates a user. Password sign-in changes the provider
in `convex/convex.config.ts` and `convex/auth.ts`, the `signIn: [...]` list in `src/server.ts`, and
the button on `/signin`. The other routes stay the same. OAuth sign-in adds a provider's component,
its functions, its `completeSignIn*` function in the `signIn` list, and a button, as that package's
The provider also subscribes to the app's `isAuthenticated` query while signed in, so a session that
ends on the deployment signs the browser out at once, and the gate leaves `/todos`.

The example signs in with Convex Auth's username and password method: its `authPassword` and
`authUsername` components in `convex/convex.config.ts`, and its sign-up and sign-in functions in
`convex/auth.ts` and in the `signIn: [...]` list of `src/server.ts`. Another method changes those
three places and the form on `/signin`. The other routes stay the same. The deployment signs the
tokens with its own key (`convexAuthProvider()` in `convex/auth.config.ts`), so no deployment needs
a key pair. OAuth sign-in adds a provider's component, its functions, its `completeSignIn*` function
in the `signIn` list, and a button, as that package's
[OAuth sign-in](../../packages/convex-auth-tanstack-start/README.md#oauth-sign-in) says.

## Where code goes
Expand All @@ -93,14 +100,10 @@ From `examples/tanstack-ssr-workers`:

```sh
pnpm install
npx convex dev --skip-push # creates your dev deployment and .env.local, no push
npx @convex-dev/auth # new deployment only: sets AUTH_PRIVATE_KEY and AUTH_JWKS on it
npx convex dev # pushes, then work as usual
npx convex dev # creates your dev deployment and .env.local, pushes, keeps running
pnpm run dev # in a second terminal
```

Production needs its own key pair on the production deployment before the first deploy.

The Cloudflare Vite plugin passes the values of `.env.local` to the local Worker as variables and
copies them into `dist/server/.dev.vars`; `wrangler deploy` does not upload that file.

Expand All @@ -117,11 +120,6 @@ Workers Builds runs `pnpm run deploy` (`wrangler deploy`) on `main` and `pnpm ru
(`wrangler preview`) on other branches. Both read the configuration that the build wrote to
`dist/server/wrangler.json`.

The preview deployments need the signing keys as the project's defaults for preview deployments,
which Convex copies into each new preview before the first push:
`npx convex env default set --type preview AUTH_PRIVATE_KEY ...` and the same for `AUTH_JWKS`. Use a
pair of their own, not the production pair.

## Commands

- `pnpm run dev`: Vite's dev server, with the Worker in Cloudflare's runtime, and `npx convex dev`
Expand Down
3 changes: 2 additions & 1 deletion examples/tanstack-ssr-workers/convex/_generated/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,6 @@ export declare const internal: FilterApi<

export declare const components: {
auth: import("@convex-dev/auth/_generated/component.js").ComponentApi<"auth">;
authAnonymous: import("@convex-dev/auth/components/anonymous/_generated/component.js").ComponentApi<"authAnonymous">;
authPassword: import("@convex-dev/auth/components/password/_generated/component.js").ComponentApi<"authPassword">;
authUsername: import("@convex-dev/auth/components/username/_generated/component.js").ComponentApi<"authUsername">;
};
2 changes: 0 additions & 2 deletions examples/tanstack-ssr-workers/convex/_generated/server.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ import type { DataModel } from "./dataModel.js";
type Env = {
readonly CONVEX_CLOUD_URL: string;
readonly CONVEX_SITE_URL: string;
readonly AUTH_JWKS: string;
readonly AUTH_PRIVATE_KEY: string;
};

/**
Expand Down
15 changes: 3 additions & 12 deletions examples/tanstack-ssr-workers/convex/auth.config.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
import type { AuthConfig } from "convex/server";
import { env } from "./_generated/server.js";
import { convexAuthProvider } from "@convex-dev/auth/server";

// The core signs tokens for the issuer CONVEX_SITE_URL and the audience "convex", and serves the
// key set under its /auth mount.
// The deployment signs the tokens with its own key, so the app keeps no key pair.
export default {
providers: [
{
type: "customJwt",
applicationID: "convex",
issuer: env.CONVEX_SITE_URL,
jwks: `${env.CONVEX_SITE_URL}/auth/.well-known/jwks.json`,
algorithm: "RS256",
},
],
providers: [convexAuthProvider()],
} satisfies AuthConfig;
11 changes: 6 additions & 5 deletions examples/tanstack-ssr-workers/convex/auth.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { setupConvexAuth } from "@convex-dev/auth/server";
import { setupAnonymous } from "@convex-dev/auth/methods/anonymous/server";
import { setupUsernamePassword } from "@convex-dev/auth/methods/username-password/server";
import { components, internal } from "./_generated/api.js";

const core = setupConvexAuth({ component: components.auth });
export const { signOut, refreshSession } = core;
export const { signOut, refreshSession, isAuthenticated } = core;

export const { signInAnonymously } = setupAnonymous(core, {
component: components.authAnonymous,
}).attachUserCallbacks({ createUser: internal.users.createUserAnonymous });
export const { signUpWithUsernamePassword, signInWithUsernamePassword } = setupUsernamePassword(
core,
{ component: components.authPassword, usernameComponent: components.authUsername },
).attachUserCallbacks({ createUser: internal.users.createUser });
17 changes: 6 additions & 11 deletions examples/tanstack-ssr-workers/convex/convex.config.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import { defineApp } from "convex/server";
import { v } from "convex/values";
import auth from "@convex-dev/auth/convex.config";
import anonymous from "@convex-dev/auth/components/anonymous/convex.config";
import authPassword from "@convex-dev/auth/components/password/convex.config";
import authUsername from "@convex-dev/auth/components/username/convex.config";

// A push fails while either variable is missing on the deployment.
const app = defineApp({
env: { AUTH_PRIVATE_KEY: v.string(), AUTH_JWKS: v.string() },
});
app.use(auth, {
httpPrefix: "/auth",
env: { AUTH_PRIVATE_KEY: app.env.AUTH_PRIVATE_KEY, AUTH_JWKS: app.env.AUTH_JWKS },
});
app.use(anonymous);
const app = defineApp();
app.use(auth);
app.use(authPassword);
app.use(authUsername);
export default app;
5 changes: 3 additions & 2 deletions examples/tanstack-ssr-workers/convex/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export default defineSchema({
"by_user",
["userId"],
),
// One document for each Convex Auth user. The auth components keep the accounts and sessions.
users: defineTable({ name: v.string() }),
// One document for each Convex Auth user. The auth components keep the usernames, the passwords,
// and the sessions.
users: defineTable({}),
});
Loading
Loading