Skip to content

#5 feat: Added auth header to API transport - #5

Draft
Chriztiaan wants to merge 1 commit into
feat/initfrom
feat/auth
Draft

Chriztiaan wants to merge 1 commit into
feat/initfrom
feat/auth

Conversation

@Chriztiaan

@Chriztiaan Chriztiaan commented Jul 22, 2026

Copy link
Copy Markdown
Collaborator

Summary

POST /api/data previously accepted any request. This PR gates it behind a bearer JWT: the backend verifies the token on every write, and the frontend attaches one to every upload. The verification logic sits behind a single pluggable interface so adopters can swap the demo verifier for a real identity provider (Supabase, Clerk, etc.) by editing one file.

How it works

The demo keeps its one-token shape: the client already fetches a PowerSync JWT from /api/auth/token in fetchCredentials() for sync. Writes now reuse that same token as the bearer credential, and the backend verifies it against its own public key (the same key it signs with, exposed via a new getPublicJwk() helper).

Docs

  • auth-verifiers.md — adopter guide for swapping the verifier to Supabase or Clerk: worked verifier snippets, env vars, frontend getToken() wiring, sync-token options, and a verification checklist.

AI disclosure

This PR was created with the help of Claude Code. Help constitutes assistance in research, planning, and rough outline of implementation. Beyond having a hand in the implementation, I have also manually tested this work.

@kobiebotha kobiebotha changed the title feat: Added auth header to API transport #5 feat: Added auth header to API transport Aug 24, 2026
This was referenced Sep 17, 2026
ckritzinger pushed a commit to ckritzinger/powersync-temp-write-api that referenced this pull request Sep 18, 2026
ckritzinger pushed a commit to ckritzinger/powersync-temp-write-api that referenced this pull request Sep 18, 2026
Three things the merge of powersync-ja#5 and powersync-ja#7 got textually clean but semantically wrong,
plus the regeneration the spec fix forces.

- openapi.yaml: /api/data/batch now declares `security: bearerAuth` and a 401
  response. powersync-ja#5 mounts requireAuth at router.use('/data', ...), which already
  covers the sub-path, so the spec was contradicting the implementation.

- OpenAPITransport: 401 recovery moves from postTransaction into an onResponse
  middleware. It was written per-method in powersync-ja#5, so the batch endpoint powersync-ja#7 added
  threw on 401 without ever clearing the cached token — wedging the batch path
  until a reload. The bearer header was already attached centrally; the
  rejection now is too, so endpoints added later inherit both.

- data.ts: the /batch handler logs the verified writer, matching what powersync-ja#5 does
  for single-transaction writes.

Regenerated types follow from the spec change.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant