@simple-auth-kit/auth-client
v2.1.0
Published
Shared, framework-agnostic auth API client for the simple-auth-kit backend. Used by all 4 client apps (admin-nextjs, admin-react, mobile-expo, mobile-bare-rn) — the auth flow and API contract are identical across them, only the injected TokenStorage adapt
Readme
@simple-auth-kit/auth-client
Shared, framework-agnostic auth API client for simple-auth-kit
backends. Used by every client app in the repo (admin-nextjs, admin-react, mobile-expo,
mobile-bare-rn) — the auth flow and API contract are identical across them; only the
injected TokenStorage adapter differs per platform (cookies-next on web, AsyncStorage
on mobile).
Install
npm install @simple-auth-kit/auth-clientPackage: https://www.npmjs.com/package/@simple-auth-kit/auth-client
Clone the source
This is a standalone copy, published from the monorepo. To pull just this package's source without cloning the whole repo:
npx degit shahriar-ratul/simple-auth-kit/packages/auth-client auth-clientUsage
baseUrl and storage are required — this package has no default backend URL and no
built-in storage. You provide both, from your own app's config:
baseUrl: a plain string — this package doesn't read any env var itself, so how you produce that string is entirely up to your own setup. The only real question is how your build tool gets an env value into browser-run JS in the first place:| Your setup | How to expose the value | |---|---| | Node.js backend (SSR, BFF, server-only code) |
process.env.AUTH_API_URLdirectly — no build step needed | | Next.js | prefix itNEXT_PUBLIC_*— only those are inlined into the browser bundle | | Vite | prefix itVITE_*, read viaimport.meta.env.VITE_AUTH_API_URL| | CRA / plain webpack |REACT_APP_*(CRA) or whatever yourDefinePluginrule exposes | | Expo | prefix itEXPO_PUBLIC_*| | Bare React Native | no built-in env support — usereact-native-config(native build step) orreact-native-dotenv(Babel plugin) | | Plain<script>, no bundler | can't inline env vars at all — hardcode it, have your server inject it (e.g.<script>window.__ENV__={API_URL:"..."}</script>), orfetch('/config.json')at runtime |Whichever string you end up with, that's the entire integration — pass it as
baseUrlbelow.process.env.AUTH_API_URLin the snippet is illustrative, not a name this package looks for.storage: a small object implementing{ get, set, clear }(seeTokenStorageintypes.ts) — e.g. cookies on web,AsyncStorageon mobile. This package ships no implementation; you write the one that fits your platform.
import { AuthClient } from "@simple-auth-kit/auth-client";
const auth = new AuthClient({
baseUrl: process.env.AUTH_API_URL!, // wherever *your* backend is deployed — read via your own framework's env convention
storage: myTokenStorageAdapter, // your own TokenStorage implementation — platform-specific (cookies, AsyncStorage, ...)
});Pair it with any simple-auth-kit backend combo (nestjs-prisma, nestjs-drizzle,
express-prisma, express-drizzle) — the API contract is the same across all four.
baseUrl is read once, at construction, and kept on the instance — no method takes it as an
argument. Construct one AuthClient at app startup (a module-level singleton, as every app in
this repo does) and import that same instance everywhere else:
// lib/auth-client.ts — built once, when this module first loads
export const authClient = new AuthClient({ baseUrl: AUTH_API_URL, storage: cookieTokenStorage });
// every other call site — just import and use; baseUrl is never mentioned again
await authClient.login({ identifier, password });
await authClient.me();Development
npm install
npm run build # tsc -> dist/
npm run typecheck
npm run test # vitestSource
Full source and every consuming app: simple-auth-kit.
