@drvalue-oss/iam-react
v0.1.1
Published
React hooks + Zustand auth store + axios client for drvalue IAM
Downloads
296
Maintainers
Readme
@drvalue-oss/iam-react
React adapter for drvalue IAM. Provides:
createAuthStore<U>()— typed Zustand auth storeuseTokenRefresh()— schedules background refresh just before expiry, cross-tab synceduseSessionCheck()— periodic + on-focus session validationsetupApiClient()— axios instance with Bearer injection and single-flight 401 refresh
Re-exports everything from @drvalue-oss/iam-core so a single import covers most consumers.
Install
pnpm add @drvalue-oss/iam-reactPeer dep: react ^18 || ^19.
Wiring
// stores/auth.ts
import { createAuthStore } from '@drvalue-oss/iam-react';
interface MyUser {
id: string;
email: string;
name: string;
companyId?: string;
}
export const useAuthStore = createAuthStore<MyUser>();// lib/api.ts
import { setupApiClient } from '@drvalue-oss/iam-react';
export const api = setupApiClient({
baseURL: process.env.NEXT_PUBLIC_API_URL!,
onUnauthorized: () => {
// multi-tier example: admin pages bounce to /admin/login
const isAdmin = window.location.pathname.startsWith('/admin');
window.location.href = isAdmin ? '/admin/login' : '/login';
},
});// app/providers.tsx
'use client';
import { useTokenRefresh, useSessionCheck } from '@drvalue-oss/iam-react';
import { useAuthStore } from '@/stores/auth';
export function AuthProviders({ children }: { children: React.ReactNode }) {
const logout = useAuthStore((s) => s.logout);
useTokenRefresh();
useSessionCheck({
onSessionExpired: () => {
logout();
window.location.href = '/login?reason=expired';
},
});
return <>{children}</>;
}