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
15 changes: 13 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
DATABASE_URL
APP_NAME=
WORKER_NAME=
WORKER_DOMAIN=
DATABASE_NAME=
DATABASE_ID=

CLOUDFLARE_ACCOUNT_ID=
CLOUDFLARE_API_TOKEN=
PORT=
OBSERVABILITY_ENABLED=
COMPATIBILITY_DATE=
COMPATIBILITY_FLAGS=

GOOGLE_CLIENT_ID=
GOOGLE_CLIENT_SECRET=
PORT=
9 changes: 7 additions & 2 deletions .github/composite-actions/install/action.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: "Install"
description: "Sets up Node.js and runs install"
description: "Set up Vite+ and install locked dependencies"

runs:
using: composite
Expand All @@ -8,6 +8,11 @@ runs:
with:
bun-version: latest

- uses: voidzero-dev/setup-vp@v1.21.2
with:
node-version: "24"
run-install: false

- name: Setup Git User
shell: bash
run: |
Expand All @@ -16,4 +21,4 @@ runs:

- name: Install Dependencies
shell: bash
run: bun install
run: vp install --frozen-lockfile
33 changes: 29 additions & 4 deletions .github/workflows/quality.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
uses: ./.github/composite-actions/install

- name: Run lint
run: bun run lint
run: vp run lint

format-check:
name: Format Check
Expand All @@ -32,8 +32,33 @@ jobs:
- name: Install
uses: ./.github/composite-actions/install

- name: Run lint
run: bun run format:check
- name: Check format
run: vp run format:check

test:
name: D1 and Worker Tests
runs-on: ubuntu-latest
steps:
- name: Checkout branch
uses: actions/checkout@v4

- name: Install
uses: ./.github/composite-actions/install

- name: Check migrations
run: vp run db:check

- name: Run tests
run:
vp test --reporter=default --reporter=json
--outputFile=/tmp/yopem-auth-tests.json

- name: Save test report
if: always()
uses: actions/upload-artifact@v4
with:
name: yopem-auth-tests
path: /tmp/yopem-auth-tests.json

typecheck:
name: Type Check
Expand All @@ -46,4 +71,4 @@ jobs:
uses: ./.github/composite-actions/install

- name: Run typecheck
run: bun run typecheck
run: vp run typecheck
7 changes: 2 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,17 @@ node_modules
.env.development.local
.env.test.local
.env.production.local
.dev.vars

# Testing
coverage

# Cloudflare
.wrangler
wrangler.jsonc
# Alchemy
.alchemy

# Build Outputs
out/
build
dist
start

# Debug
npm-debug.log*
Expand Down
1 change: 1 addition & 0 deletions .node-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
24
39 changes: 0 additions & 39 deletions .prettierignore

This file was deleted.

39 changes: 39 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<!--VITE PLUS START-->

# Using Vite+, the Unified Toolchain for the Web

This project is using Vite+, a unified toolchain built on top of Vite, Rolldown,
Vitest, tsdown, Oxlint, Oxfmt, and Vite Task. Vite+ wraps runtime management,
package management, and frontend tooling in a single global CLI called `vp`.
Vite+ is distinct from Vite, and it invokes Vite through `vp dev` and
`vp build`. Run `vp help` to print a list of commands and `vp <command> --help`
for information about a specific command.

Docs are local at `node_modules/vite-plus/docs` or online at
https://viteplus.dev/guide/.

## Built-in Commands vs Scripts

`vp <name>` runs a built-in command. `vp run <name>` runs a `package.json`
script or a `vite.config.ts` task. Scripts cannot overwrite built-ins, so
`vp dev` and `vp run dev` may do different things. Check `package.json` and
`vite.config.ts` first, and run `vp run <name>` when the project defines a
script or task with that name.

## Tool Versions

Run `vp toolchain` to show versions and relationships in the active Vite+
release. Add a tool name to select part of the graph. For example, run
`vp toolchain vite`. Use `--global` to ignore the local `vite-plus` package. Use
`vp why <package>` to show the package-manager dependency graph.

## Review Checklist

- [ ] Run `vp install` after pulling remote changes and before getting started.
- [ ] Run `vp check` and `vp test` to format, lint, type check and test changes.
- [ ] Check if there are `vite.config.ts` tasks or `package.json` scripts
necessary for validation, run via `vp run <script>`.
- [ ] If setup, runtime, or package-manager behavior looks wrong, run
`vp env doctor` and include its output when asking for help.

<!--VITE PLUS END-->
29 changes: 0 additions & 29 deletions Dockerfile

This file was deleted.

136 changes: 92 additions & 44 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,66 +1,114 @@
# Yopem Auth

A lightweight authentication api with Google OAuth integration using
[OpenAuth](https://openauth.js.org).
Authentication API with Google OAuth, [OpenAuth](https://openauth.js.org),
Cloudflare Workers, and D1. [Alchemy v2](https://alchemy.run) manages Worker
bindings, database migrations, and deployment. No Wrangler configuration or
`.dev.vars` file is needed.

Worker handlers, database queries, and validation use Effect. The database layer
uses the
[Effect v4 D1Client](https://effect.website/docs/v4/api/sql-d1/D1Client). Effect
Schema validates configuration, Google profiles, subjects, and database records.
Drizzle uses the same D1Client for typed Effect queries. It also defines tables
and generates migrations. D1Client batches keep user and account writes atomic.
OpenAuth uses a small Promise adapter at the library boundary.

## Setup

Requirements: Vite+ (`vp`), Bun, a Cloudflare account, and Google OAuth
credentials. The configured DNS zone and D1 database must be in the Cloudflare
account that you use. All deployment settings and credentials come from `.env`.

```sh
vp install
cp .env.example .env
```

## Getting Started
Fill `.env` with Google OAuth credentials, Cloudflare credentials, and the
settings from `.env.example`. Alchemy loads and validates this file before it
changes resources. Binding definitions stay in `alchemy.run.ts`.

### Prerequisites
If you do not use an API token, configure or renew Alchemy login:

- Bun 1.2.18 or later
- PostgreSQL database
- Cloudflare Workers account
- Google Cloud Platform account
```sh
vp run cf:login
```

### Installation
Alchemy uses its encrypted Cloudflare state store. No `ALCHEMY_PASSWORD` or
local state key is needed. The first plan, development, or deployment command
can ask to create a state-store Worker, Durable Object, and secret entries.
Approve this only when you can change the Cloudflare account. This setup does
not create those resources.

1. Clone the repository
Keep `.env`, `.alchemy/`, and Alchemy profile credentials private. Alchemy v1
state is not converted to v2 state. Keep old state and its key for backups.

```bash
git clone https://github.com/yopem/auth.git
cd auth
```
Set Google OAuth redirect URIs with the `/google/callback` path. Use the
configured production domain and local origin from `.env`.

2. Install dependencies
## Development

```bash
bun install
```sh
vp run dev
```

### Configuration
Alchemy starts Worker on the configured local port and applies SQLite migrations
to local D1. Local D1 is separate from production. Development does not copy
production records or attach the production domain.

1. Google Auth Setup:
- Create OAuth 2.0 credentials in Google Cloud Platform
- Configure the authorized redirect URI:
`https://your-domain/google/callback`
- Fill the `GOOGLE_CLIENT_ID` and `GOOGLE_CLIENT_SECRET` in your `.dev.vars`
file
Users, accounts, OpenAuth signing keys, and tokens use D1. Short token expiry
times do not need the former KV TTL workaround. Expired auth records are
excluded from reads and scans. Storage writes remove expired records.

2. Cloudflare Workers Setup:
- Create a KV database in Cloudflare Workers dashboard
- Rename `wrangler.jsonc.example` to `wrangler.jsonc`
- Fill in all required fields using the Wrangler CLI for managing secrets,
refer to the
[Cloudflare Workers Secrets documentation](https://developers.cloudflare.com/workers/configuration/secrets/#adding-secrets-to-your-project)
for more details.
## Schema changes

Edit `src/lib/db/schema.ts`, then generate a migration:

```sh
vp run db:generate
vp run db:check
```

3. Environment Variables:
- Add DATABASE_URL to your .env file because drizzle cannot read .dev.vars
Alchemy applies pending SQL migrations during development and deployment. Do not
run Drizzle migrations separately.

4. Databse Migration:
```bash
bun run db:generate
bun run db:migrate
```
This change starts with empty D1 tables. It does not copy PostgreSQL users or KV
records. Users must sign in again. Old databases and KV namespaces are not
deleted. The target D1 database must not contain conflicting tables. Review its
schema before the first deployment.

### Run the Application
## Validation

```bash
bun run dev
```sh
vp check
vp run check
vp run format:check
vp test --reporter=default --reporter=json --outputFile=/tmp/yopem-auth-tests.json
```

### Deploy
Tests use local D1 and a bundled Worker. They check JSON storage, exact expiry,
prefix scans, pagination, user/account writes, rollback, OAuth discovery,
persistent signing keys, and Google OAuth callbacks. Tests use local Google
responses to check user creation and rejected unverified email. They also check
concurrent requests. No Cloudflare credentials or `.env` file is needed for
tests. Real Google sign-in needs OAuth credentials and a browser.

```bash
bun run deploy
## Deployment

```sh
vp run deploy
```

Alchemy checks the configured D1 ID and name, adopts the database, applies
pending migrations, and deploys the configured Worker and domain. D1 deletion is
blocked by the Alchemy retention policy. Alchemy keeps the current D1
jurisdiction and replication mode.

Deployment changes live Worker code, secrets, domain bindings, and D1 tables.
Check the Cloudflare account, D1 schema, and domain owner before deployment. If
another Worker owns the configured domain, resolve the conflict before
deployment.

## License

AGPL-3.0-or-later. See [LICENSE.md](./LICENSE.md).
Loading
Loading