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
6 changes: 5 additions & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,11 @@ developers, not for us. Every comment ships: keep it short, plain and true.
the JSDoc examples, in memory. Each page is listed in `documents` in
`scripts/docs-pages.mjs`, which the site also reads for type hovers, and
a page that builds on Getting started lists the files it uses as `shared`.
A new page with titled examples fails the check until it's listed.
A new page with titled examples fails the check until it's listed, and a
listed page without any fails too. Blocks with `app="name"` compile as
their own app, with the page's other files: use it for a second version of
a file, such as `src/main.tsx` for another mode, or for a file that can't
share the page's main app.
- The README's quick start is its own small app: each block marked
`<!-- file: src/auth.ts -->` is a file, compiled together like a page's
(`README.md` in `documents`). A README example marked
Expand Down
48 changes: 48 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,53 @@
# Changelog

## 0.1.0-rc.3

Fixes for Expo SDK 53 to 57 and for redirects on React Native, and a
rewritten docs site. Session kit's API is unchanged.

### Fixed

- **Expo SDK 53 to 57:** signing in, refreshing, revoking and `updateUser`
failed on React Native 0.79 to 0.86. Their `AbortController` has no
`throwIfAborted` or `reason`, which the client relied on. It now keeps
the abort reason itself, so a sign-in cut short by a newer one still
rejects with `SESSION_CHANGED`. Expo SDK 58 wasn't affected.
- **`createAuthFetch` on React Native:** redirects are refused there too.
The redirect mode is now passed in `init`, which Expo's `fetch` reads.
Before Expo SDK 58, `fetch` follows redirects anyway, so a response from
another origin or path rejects with a `TypeError`, as a refused redirect
does in browsers. The redirected request has already gone out by then.
- **`createAuthFetch` keeps a `Request`'s `referrerPolicy` and
`referrer`.** Copying the request to add the token reset them.

### Docs

- The JSDoc names more of the cases behind each error code: what
`REFRESH_FAILED`, `INVALID_SESSION`, `USER_VALIDATION_FAILED`,
`SESSION_CHANGED`, `PERSISTENCE_FAILED` and `INVALID_OPTIONS` cover, and
what `credentials.renew` and `updateUser` reject with.
- `keychainAccessible` no longer says a locked iOS keychain never signs the
user out: a refresh that can't save its tokens does.
- `sessionBearer` says that `fetch` follows redirects on React Native before
Expo SDK 58.
- The API reference shows each export's whole description and its `@see`
links, and lists the members of the client and server types with their
descriptions, which say what each method rejects with.
- `sessionBearer` says that a 401 it doesn't send again fails the call as
an `HttpError`, and `updateUser` that an `AuthError` its function throws
keeps its own code.
- The docs site is rewritten. Every page was checked against the source,
and every code sample on it compiles, the setup guides' config files
included. The sidebar is grouped into Get started, Setups, Guides, Going
further and Reference. Effect has three pages (`/docs/effect`,
`/docs/effect/browser` and `/docs/effect/server`), and Expo has an
"Unlock with biometrics" guide
(`/docs/setups/expo-locking`). Every old page URL still works. Some
section anchors changed, such as the old `/docs/effect` steps; no link in
the docs, the README or the examples points at one.
- Compatibility states the TypeScript floor (5.4, and 5.9 for Effect), the
framework's own Node.js and React floors, and which runtimes CI tests.

## 0.1.0-rc.2

For effetch `0.1.0-rc.4`, whose main entry now exports only the `Fetch`
Expand Down
4 changes: 3 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ example:
resolution, the server-rendering guides under Bundler resolution, and the
Expo guide under Bundler resolution without DOM types, as Metro resolves.
`scripts/docs-pages.mjs` lists each page, for `scripts/check-docs.mjs`
and for the site's type hovers.
and for the site's type hovers. Blocks with `app="name"` compile as their
own app, with the page's other files, such as a second version of a file
for another mode.
- **The reference.** The API reference pages are generated from the JSDoc
by `pnpm run docs:reference`, and the check fails when they're out of
date. Run it after changing a public export or its JSDoc.
Expand Down
26 changes: 9 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,16 @@ server with TanStack Start, Next.js, and React Router, and comes with an

## Installation

Install the package and a schema library:

```sh
npm install @monarcode/session-kit zod
```

Any [Standard Schema](https://standardschema.dev) library describes users.
The examples use Zod. Peer dependencies such as React and your router are
optional: install the ones your setup uses.
optional: install the ones your setup uses. Session kit needs TypeScript 5.4
or newer (Effect asks for 5.9), and
[Compatibility](https://session-kit.pages.dev/docs/reference/compatibility)
lists the supported versions.

Expand All @@ -52,7 +55,7 @@ and read the [release notes](./CHANGELOG.md) before upgrading.

Your backend keeps its sign-in and refresh endpoints. Session kit takes the
tokens they return and manages the session from there. A React app needs
three files.
three files, plus your own sign-in form.

**1. Create the client.** It checks every user against your schema, saves the
session, and calls your `refresh` before the access token expires:
Expand Down Expand Up @@ -163,25 +166,14 @@ function Home() {
To call your API, `createAuthFetch` from `@monarcode/session-kit/http` adds
the access token to each request and refreshes it after a 401.
[Getting started](https://session-kit.pages.dev/docs/getting-started) builds
this app step by step against a real test backend, and the guides below add
this app step by step against a real test backend, and the setup guides add
your router's guards or server rendering.

## Pick your setup

Each setup has a guide:

| Setup | Guide |
| -------------------------------------------------------------- | ----------------------------------------------------------------------------------- |
| React without a router | [Getting started](https://session-kit.pages.dev/docs/getting-started) |
| TanStack Router, and TanStack Start as a single-page app (SPA) | [TanStack Router](https://session-kit.pages.dev/docs/setups/tanstack-router) |
| TanStack Start with server rendering | [TanStack Start](https://session-kit.pages.dev/docs/setups/tanstack-start) |
| React Router: declarative, data, framework SPA | [React Router](https://session-kit.pages.dev/docs/setups/react-router) |
| React Router, framework mode with server rendering | [React Router with SSR](https://session-kit.pages.dev/docs/setups/react-router-ssr) |
| Next.js 16, App Router | [Next.js](https://session-kit.pages.dev/docs/setups/next) |
| Expo Router on iOS and Android, SDK 53+ | [Expo Router](https://session-kit.pages.dev/docs/setups/expo) |
| Effect and effetch, with any setup | [Effect and effetch](https://session-kit.pages.dev/docs/effect) |

All but the first have a runnable app in [`examples/`](./examples).
Each setup, from plain React to Next.js, Expo Router and Effect, has a guide:
see [Pick your setup](https://session-kit.pages.dev/docs/installation#pick-your-setup).
Every setup but plain React has a runnable app in [`examples/`](./examples).

## Contributing

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@monarcode/session-kit",
"version": "0.1.0-rc.2",
"version": "0.1.0-rc.3",
"description": "Schema-driven, type-safe sessions for React, with TanStack Router, React Router, Expo Router, Next.js, TanStack Start, and Effect support",
"keywords": [
"auth",
Expand Down
31 changes: 25 additions & 6 deletions scripts/check-docs.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
installedAtLeast,
markedExamples,
PAGES,
pageApp,
pageApps,
root,
} from "./docs-pages.mjs";
import { renderReference } from "./reference-docs.mjs";
Expand Down Expand Up @@ -95,7 +95,9 @@ checkPagesListed();
checkLinks();

if (problems.length > 0) {
console.error(problems.map((problem) => `✗ ${problem}`).join("\n"));
// A page's main files compile again in each of its named apps.
const unique = [...new Set(problems)];
console.error(unique.map((problem) => `✗ ${problem}`).join("\n"));
process.exit(1);
}
console.log(
Expand Down Expand Up @@ -370,6 +372,7 @@ function checkDocument({
fixtures,
shared,
requires = {},
appRequires = {},
}) {
const missing = Object.entries(requires).find(
([name, minimum]) => !installedAtLeast(name, minimum),
Expand All @@ -381,9 +384,22 @@ function checkDocument({
return false;
}
const report = (message) => problems.push(message);
const files = pageApp({ path, dom, fixtures, shared }, report);
if (files.length === 0) return true;
compileExamples(path, files, resolutions, { together: true });
const apps = pageApps({ path, dom, fixtures, shared }, report);
for (const [name, files] of apps) {
const needs = Object.entries(appRequires[name] ?? {}).find(
([dependency, minimum]) => !installedAtLeast(dependency, minimum),
);
if (needs) {
console.log(
`- ${path} app "${name}" skipped: it needs ${needs[0]} ${needs[1]} or later`,
);
continue;
}
if (files.length > 0)
compileExamples(name ? `${path}#${name}` : path, files, resolutions, {
together: true,
});
}
return true;
}

Expand All @@ -395,6 +411,9 @@ function checkDocument({
*/
function compileExamples(label, files, resolutions, { together = false } = {}) {
const directory = join(scratch, label.replace(/[^\w]+/g, "-"));
// A page's named app is labelled `path#name`.
const app = label.includes("#") ? label.slice(label.indexOf("#") + 1) : "";
const where = app ? `, app "${app}"` : "";
const virtual = new Map(
files.map((file) => [join(directory, file.path), file]),
);
Expand Down Expand Up @@ -439,7 +458,7 @@ function compileExamples(label, files, resolutions, { together = false } = {}) {
diagnostic.start ?? 0,
);
problems.push(
`${relative(root, file.origin)}:${file.line + line} (${file.path}, ${resolution}): TS${diagnostic.code} ${message}`,
`${relative(root, file.origin)}:${file.line + line} (${file.path}${where}, ${resolution}): TS${diagnostic.code} ${message}`,
);
}
}
Expand Down
7 changes: 7 additions & 0 deletions scripts/docs-pages.d.mts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface PageFile {
origin: string;
line: number;
path: string;
app?: string | undefined;
dom: boolean;
code: string;
fixture?: boolean | undefined;
Expand All @@ -21,12 +22,14 @@ export interface PageDocument {
| { from: string; files: string[]; directories?: string[] | undefined }
| undefined;
requires?: Record<string, string> | undefined;
appRequires?: Record<string, Record<string, string>> | undefined;
}

export const root: string;
export const PAGES: string;
export const MARKERS: { md: RegExp; mdx: RegExp };
export const TITLE: RegExp;
export const APP: RegExp;
export const documents: PageDocument[];

export function markedExamples(path: string, dom: boolean): PageFile[];
Expand All @@ -35,6 +38,10 @@ export function pageApp(
document: Pick<PageDocument, "path" | "dom" | "fixtures" | "shared">,
report: (message: string) => void,
): PageFile[];
export function pageApps(
document: Pick<PageDocument, "path" | "dom" | "fixtures" | "shared">,
report: (message: string) => void,
): Map<string, PageFile[]>;
export function compilerSettings(
resolution: "NodeNext" | "Bundler",
dom: boolean,
Expand Down
Loading
Loading