@swr-login/core
v0.13.0
Published
Core primitives for swr-login v0.9: session store, event bus, method registry, credential contract
Downloads
1,024
Maintainers
Readme
@swr-login/core
Core logic for swr-login: state machine, token management, plugin system, event emitter.
Install
npm install @swr-login/coreFeatures
- ⚡ SWR-Powered — Stale-while-revalidate strategy for instant auth state
- 🔌 Plugin Architecture — Every login channel is an independent package
- 🔄 Cross-tab Sync — BroadcastChannel-based login/logout sync
- 🪶 Tiny — <3KB gzipped
- 📦 100% TypeScript — Full generics and JSDoc
Usage
import { createAuthClient } from '@swr-login/core';
const client = createAuthClient({
adapter: myAdapter,
plugins: [myPlugin],
fetchUser: async (token) => {
const res = await fetch('/api/me', {
headers: { Authorization: `Bearer ${token}` },
});
return res.json();
},
});Custom Plugin
import type { SWRLoginPlugin } from '@swr-login/core';
const MyPlugin: SWRLoginPlugin<{ token: string }> = {
name: 'my-sso',
type: 'oauth',
async login({ token }, ctx) {
const res = await fetch('/api/sso/verify', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ token }),
});
const data = await res.json();
ctx.setTokens({ accessToken: data.accessToken, expiresAt: data.expiresAt });
return data;
},
};Part of swr-login
This is the framework-agnostic core of the swr-login project. For React bindings, see @swr-login/react.
