Skip to content
Merged
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
23 changes: 19 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
# sveltekit-auth

An Orunbase starter baseline, being built: Auth.js's SvelteKit authentication example on Cloudflare Workers, on the Cirrus baseline conventions.
An Orunbase starter baseline, being built: Auth.js's [SvelteKit example](https://github.com/nextauthjs/sveltekit-auth-example)
on Cloudflare Workers + Static Assets through
[`@sveltejs/adapter-cloudflare`](https://svelte.dev/docs/kit/adapter-cloudflare),
on the Cirrus baseline conventions. ISC, © 2022-2024 Balázs Orbán
(`apps/web/LICENSE`).

Upstream: https://github.com/nextauthjs/sveltekit-auth-example (ISC, © 2022-2024 Balázs Orbán)
Upstream: https://github.com/nextauthjs/sveltekit-auth-example

The plan — what the example is, how each piece maps to Cloudflare, the
milestones and the risks — is the epic in
## Running it

```bash
pnpm install
pnpm typecheck && pnpm lint && pnpm test && pnpm build
pnpm --filter @app/web secret:dev # a generated AUTH_SECRET in apps/web/.env (git-ignored)
pnpm --filter @app/web dev # http://localhost:3000
pnpm --filter @app/web preview # wrangler dev over the built Worker
```

A sign-in provider appears on `/signin` once its keys are in
`apps/web/.env` (`.env.example` lists them). The plan and the as-built
status are the epic in
[`specs/epics/sveltekit-auth`](specs/epics/sveltekit-auth/README.md).
Empty file removed apps/.gitkeep
Empty file.
36 changes: 36 additions & 0 deletions apps/web/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Local secrets, upstream's .env.example: copy to .env (git-ignored), which
# `vite dev` and `wrangler dev` both read, or run
# `pnpm --filter @app/web secret:dev`, which writes a generated AUTH_SECRET
# into .env and keeps it on a second run. (A .dev.vars file, if there is one,
# takes the place of .env for wrangler.) Nothing here is read at build time:
# deployed, the Worker reads every value at request time from its own
# secrets, which the deploy lane pushes.
#
# Required: the key Auth.js encrypts its session cookie with. Any long random
# string; on UNIX systems `openssl rand -hex 32`.
AUTH_SECRET=

# Optional: a sign-in provider appears on /signin once all its keys are set.
# These are the nine providers upstream's .env.example names; every other
# provider's keys are in src/server/providers.ts.
# GitHub: https://github.com/settings/developers → New OAuth App, callback
# http://localhost:3000/auth/callback/github (and your deployed origin's).
AUTH_GITHUB_ID=
AUTH_GITHUB_SECRET=
AUTH_GOOGLE_ID=
AUTH_GOOGLE_SECRET=
AUTH_DISCORD_ID=
AUTH_DISCORD_SECRET=
AUTH_LINKEDIN_ID=
AUTH_LINKEDIN_SECRET=
AUTH_FACEBOOK_ID=
AUTH_FACEBOOK_SECRET=
AUTH_TWITTER_ID=
AUTH_TWITTER_SECRET=
AUTH_TWITCH_ID=
AUTH_TWITCH_SECRET=
AUTH_PINTEREST_ID=
AUTH_PINTEREST_SECRET=
AUTH_AUTH0_ID=
AUTH_AUTH0_SECRET=
AUTH_AUTH0_ISSUER=
1 change: 1 addition & 0 deletions apps/web/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
engine-strict=true
23 changes: 23 additions & 0 deletions apps/web/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
.DS_Store
node_modules
/build
/.svelte-kit
/package
.env
.env.*
!.env.example

# Ignore files for PNPM, NPM and YARN
pnpm-lock.yaml
package-lock.json
yarn.lock

# The deploy contract and its generated pages, formatted by their own tools
# (tooling/catalog/gen-component-docs.py writes docs/), and the committed
# wrangler config, which keeps its comments as written.
component.yaml
docs/
wrangler.jsonc
.dev.vars
.dev.vars.*
.wrangler
5 changes: 5 additions & 0 deletions apps/web/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"semi": false,
"plugins": ["prettier-plugin-svelte"],
"overrides": [{ "files": "*.svelte", "options": { "parser": "svelte" } }]
}
15 changes: 15 additions & 0 deletions apps/web/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
ISC License

Copyright (c) 2022-2024, Balázs Orbán

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
35 changes: 35 additions & 0 deletions apps/web/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# apps/web — the site

Auth.js's [SvelteKit example](https://github.com/nextauthjs/sveltekit-auth-example)
(ISC, © 2022-2024 Balázs Orbán, `LICENSE` beside this file) on SvelteKit 2
and Svelte 5, running on Cloudflare Workers + Static Assets through
[`@sveltejs/adapter-cloudflare`](https://svelte.dev/docs/kit/adapter-cloudflare):

- `/` — the home page; the header says who is signed in, with a sign-in or
sign-out button
- `/signin` — a button per connected provider, or, with none connected,
what to connect; its form action is upstream's, Auth.js's `signIn`
- `/protected` — its content only for a signed-in visitor, "Access Denied"
for anyone else
- `/signout` — upstream's action, Auth.js's `signOut`
- `/auth/*` — Auth.js's own routes (`/auth/session`, `/auth/providers`,
`/auth/callback/<provider>`, …), answered by the handle in
`src/hooks.server.ts`

Auth.js is configured per request (`src/auth.ts`) from the Worker's own
secrets: `AUTH_SECRET` seals the session cookie, and each provider in
`src/server/providers.ts` is offered only when every key it takes is set.
The site's name — the home page's heading and every page's `<title>` — is
in `src/site.ts`.

```bash
pnpm secret:dev # a generated AUTH_SECRET in .env (kept on a second run)
pnpm dev # vite dev on http://localhost:3000, reading .env
pnpm build # vite build → .svelte-kit/cloudflare (_worker.js + the static output)
pnpm preview # wrangler dev over the built Worker
pnpm test # vitest
```

How it is deployed — per environment, on merge to `main`, its secrets
pushed to the Worker first — is the repository's README and
`component.yaml` beside this file.
121 changes: 121 additions & 0 deletions apps/web/component.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
apiVersion: sourceplane.io/v1
kind: Component
metadata:
name: web
description: The Auth.js sign-in example (SvelteKit on Cloudflare Workers + Static Assets via @sveltejs/adapter-cloudflare, per environment)
spec:
type: cloudflare-workers-assets-turbo
lifecycle: production
owner: platform
# Its pages, attached to it in the workspace's catalog: what the Docs
# library shows for it, and what the catalog's docs and runbook checks read.
# Generated from what this manifest and its sources declare.
docs:
overview: docs/overview.md
pages:
- { path: docs/architecture.md, role: architecture, title: Architecture }
- { path: docs/runbook.md, role: runbook, title: Runbook }
# `app`, not `web`: a domain and a component share one name space in the
# catalog, and a domain called `web` hid this component from it.
domain: app
path: apps/web
# Its own lane per environment: the framework build is the longest job in
# the plan, and a shared lane would wait on it.
lane: isolated
# The Worker's SECRETS, each read at request time ($env/dynamic/private),
# never inlined. The sign-in providers' client ids and secrets — the nine
# upstream's .env.example names, GitHub first — stored per environment as
# the product's secrets (README → Sign-in providers). BEST-EFFORT,
# wire-now-seed-later: a key nobody has stored is skipped when the job
# resolves its secrets (no env var, no failure), and a provider is offered
# only once all its keys are set (src/server/providers.ts), so a fresh
# product deploys and serves before its owner has an OAuth app at all.
# AUTH_SECRET, if stored, replaces the one the first deploy generates.
optionalSecretEnv:
AUTH_SECRET: "secret://sveltekit-auth/sveltekit-auth/{{ .environment }}/AUTH_SECRET"
AUTH_GITHUB_ID: "secret://sveltekit-auth/sveltekit-auth/{{ .environment }}/AUTH_GITHUB_ID"
AUTH_GITHUB_SECRET: "secret://sveltekit-auth/sveltekit-auth/{{ .environment }}/AUTH_GITHUB_SECRET"
AUTH_GOOGLE_ID: "secret://sveltekit-auth/sveltekit-auth/{{ .environment }}/AUTH_GOOGLE_ID"
AUTH_GOOGLE_SECRET: "secret://sveltekit-auth/sveltekit-auth/{{ .environment }}/AUTH_GOOGLE_SECRET"
AUTH_DISCORD_ID: "secret://sveltekit-auth/sveltekit-auth/{{ .environment }}/AUTH_DISCORD_ID"
AUTH_DISCORD_SECRET: "secret://sveltekit-auth/sveltekit-auth/{{ .environment }}/AUTH_DISCORD_SECRET"
AUTH_LINKEDIN_ID: "secret://sveltekit-auth/sveltekit-auth/{{ .environment }}/AUTH_LINKEDIN_ID"
AUTH_LINKEDIN_SECRET: "secret://sveltekit-auth/sveltekit-auth/{{ .environment }}/AUTH_LINKEDIN_SECRET"
AUTH_FACEBOOK_ID: "secret://sveltekit-auth/sveltekit-auth/{{ .environment }}/AUTH_FACEBOOK_ID"
AUTH_FACEBOOK_SECRET: "secret://sveltekit-auth/sveltekit-auth/{{ .environment }}/AUTH_FACEBOOK_SECRET"
AUTH_TWITTER_ID: "secret://sveltekit-auth/sveltekit-auth/{{ .environment }}/AUTH_TWITTER_ID"
AUTH_TWITTER_SECRET: "secret://sveltekit-auth/sveltekit-auth/{{ .environment }}/AUTH_TWITTER_SECRET"
AUTH_TWITCH_ID: "secret://sveltekit-auth/sveltekit-auth/{{ .environment }}/AUTH_TWITCH_ID"
AUTH_TWITCH_SECRET: "secret://sveltekit-auth/sveltekit-auth/{{ .environment }}/AUTH_TWITCH_SECRET"
AUTH_PINTEREST_ID: "secret://sveltekit-auth/sveltekit-auth/{{ .environment }}/AUTH_PINTEREST_ID"
AUTH_PINTEREST_SECRET: "secret://sveltekit-auth/sveltekit-auth/{{ .environment }}/AUTH_PINTEREST_SECRET"
AUTH_AUTH0_ID: "secret://sveltekit-auth/sveltekit-auth/{{ .environment }}/AUTH_AUTH0_ID"
AUTH_AUTH0_SECRET: "secret://sveltekit-auth/sveltekit-auth/{{ .environment }}/AUTH_AUTH0_SECRET"
AUTH_AUTH0_ISSUER: "secret://sveltekit-auth/sveltekit-auth/{{ .environment }}/AUTH_AUTH0_ISSUER"
subscribe:
environments:
- name: dev
profile: verify
- name: stage
profile: verify
profileRules:
- profile: deploy
when:
triggerRef: github-push-main
- name: prod
profile: verify
profileRules:
- profile: deploy
when:
triggerRef: github-push-main
parameters:
nodeVersion: "22"
pnpmVersion: "10.12.1"

workspaceDir: ../..
appDir: .

turboFilter: "@app/web"
# The committed config: worker name, compatibility date and flags, the
# Worker entry and the ASSETS binding over the adapter's output. Nothing
# to render: no binding but ASSETS (sessions are Auth.js's JWT cookies).
wranglerConfig: wrangler.jsonc

# @sveltejs/adapter-cloudflare emits the Worker as
# .svelte-kit/cloudflare/_worker.js and the static output (_app/* hashed
# assets, static/) beside it — both deploy in one `wrangler deploy`.
workerEntrypoint: .svelte-kit/cloudflare/_worker.js
assetsDir: .svelte-kit/cloudflare

# Per-env worker name: sveltekit-auth-web-{dev,stage,prod}.
workerName: sveltekit-auth-web
environmentAwareWorkerName: true

# The account's workers.dev subdomain; exported to the smoke step as
# ${WORKERS_DEV_SUBDOMAIN} and used to build ${DEPLOYED_URL}.
workersDevSubdomain: your-workers-subdomain

installCommand: pnpm install --frozen-lockfile
# Build, then hand the Worker its secrets. No secret reaches the build
# (`env -u`): the Worker reads each at request time. Then the providers'
# keys the environment has go to the Worker (`wrangler secret bulk`, the
# values over stdin) before the composition deploys it, and AUTH_SECRET
# with them when the Worker does not carry one yet — a fresh 32-byte key
# the FIRST deploy of each environment generates and every later deploy
# keeps (tooling/wire/worker-secrets.mjs --generate). Only the deploy lane
# holds a Cloudflare credential, so a verify lane builds the same and
# pushes nothing.
buildCommand: env -u AUTH_SECRET -u AUTH_GITHUB_ID -u AUTH_GITHUB_SECRET -u AUTH_GOOGLE_ID -u AUTH_GOOGLE_SECRET -u AUTH_DISCORD_ID -u AUTH_DISCORD_SECRET -u AUTH_LINKEDIN_ID -u AUTH_LINKEDIN_SECRET -u AUTH_FACEBOOK_ID -u AUTH_FACEBOOK_SECRET -u AUTH_TWITTER_ID -u AUTH_TWITTER_SECRET -u AUTH_TWITCH_ID -u AUTH_TWITCH_SECRET -u AUTH_PINTEREST_ID -u AUTH_PINTEREST_SECRET -u AUTH_AUTH0_ID -u AUTH_AUTH0_SECRET -u AUTH_AUTH0_ISSUER pnpm exec turbo run build --filter=@app/web && node ../../tooling/wire/worker-secrets.mjs --worker "sveltekit-auth-web-${ORUN_ENVIRONMENT}" --config wrangler.jsonc --generate AUTH_SECRET --secret AUTH_GITHUB_ID --secret AUTH_GITHUB_SECRET --secret AUTH_GOOGLE_ID --secret AUTH_GOOGLE_SECRET --secret AUTH_DISCORD_ID --secret AUTH_DISCORD_SECRET --secret AUTH_LINKEDIN_ID --secret AUTH_LINKEDIN_SECRET --secret AUTH_FACEBOOK_ID --secret AUTH_FACEBOOK_SECRET --secret AUTH_TWITTER_ID --secret AUTH_TWITTER_SECRET --secret AUTH_TWITCH_ID --secret AUTH_TWITCH_SECRET --secret AUTH_PINTEREST_ID --secret AUTH_PINTEREST_SECRET --secret AUTH_AUTH0_ID --secret AUTH_AUTH0_SECRET --secret AUTH_AUTH0_ISSUER

# Smoke: the home page renders to its end (`</html>` — the layout asks
# Auth.js for the session, so a Worker without AUTH_SECRET answers 500),
# and /auth/session answers `null` for a visitor with no session — Auth.js
# itself, in the Worker, with its secret bound. Not the site's name: that
# is the owner's to change. Read-only. Each response is buffered before
# it is tested so an early match cannot SIGPIPE curl.
smokeCommand: "html=$(curl -sfL \"${DEPLOYED_URL}/\") && echo \"${html}\" | grep -q '</html>' && session=$(curl -sf \"${DEPLOYED_URL}/auth/session\") && [ \"${session}\" = \"null\" ]"
labels:
team: platform
layer: delivery
surface: web
runtime: cloudflare
36 changes: 36 additions & 0 deletions apps/web/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "@app/web",
"version": "0.0.0",
"private": true,
"description": "Auth.js's SvelteKit example on Cloudflare Workers + Static Assets",
"type": "module",
"scripts": {
"dev": "vite dev",
"build": "vite build",
"preview": "wrangler dev --config wrangler.jsonc",
"prepare": "svelte-kit sync || echo ''",
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
"typecheck": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
"lint": "prettier --check .",
"format": "prettier --write .",
"test": "vitest run",
"secret:dev": "node ../../tooling/wire/worker-secrets.mjs --dev-vars .env --generate AUTH_SECRET --secret AUTH_SECRET"
},
"dependencies": {
"@auth/sveltekit": "1.11.3"
},
"devDependencies": {
"@sveltejs/adapter-cloudflare": "7.2.9",
"@sveltejs/kit": "2.60.1",
"@sveltejs/vite-plugin-svelte": "5.1.1",
"@types/node": "^22.20.5",
"prettier": "^3.6.2",
"prettier-plugin-svelte": "^3.4.0",
"svelte": "5.55.7",
"svelte-check": "^4.3.1",
"typescript": "^5.8.3",
"vite": "6.4.2",
"vitest": "^3.2.4",
"wrangler": "^4.151.0"
}
}
13 changes: 13 additions & 0 deletions apps/web/src/app.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/// <reference types="@auth/sveltekit" />
/// <reference types="@sveltejs/adapter-cloudflare" />

// See https://svelte.dev/docs/kit/types#app.d.ts
declare global {
namespace App {
interface Platform {
env: Record<string, unknown>
}
}
}

export {}
13 changes: 13 additions & 0 deletions apps/web/src/app.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%sveltekit.assets%/favicon.ico" />
<meta name="viewport" content="width=device-width" />
%sveltekit.head%
</head>

<body>
<div>%sveltekit.body%</div>
</body>
</html>
17 changes: 17 additions & 0 deletions apps/web/src/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { SvelteKitAuth } from "@auth/sveltekit"
import { serverEnv } from "./server/env"
import { providersFor } from "./server/providers"

// Upstream's Auth.js configuration, made lazy: the providers are read per
// request from the Worker's own secrets, so a provider whose keys are stored
// appears on the next request after the deploy that pushed them, and one
// with no keys is never offered. Sessions are Auth.js's default, an
// encrypted JWT cookie sealed with AUTH_SECRET; there is no database.
export const { handle, signIn, signOut } = SvelteKitAuth(async (event) => {
const env = serverEnv(event)
return {
trustHost: true,
secret: env.AUTH_SECRET,
providers: providersFor(env),
}
})
19 changes: 19 additions & 0 deletions apps/web/src/components/external-icon.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<svg
xmlns="http://www.w3.org/2000/svg"
width="16"
height="16"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
class="inline-block ml-0.5 w-4 h-4"
><path d="M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6"
></path><polyline points="15 3 21 3 21 9"></polyline><line
x1="10"
x2="21"
y1="14"
y2="3"
></line></svg
>
Loading
Loading