A lightweight, framework-agnostic authentication client SDK designed for scalable React (and non-React) apps using centralized login via Keycloak + Auth Service.
npm install @spidy092/auth-clientNote: This package supports both ES Modules (
import) and CommonJS (require).CommonJS Usage:
const { auth } = require('auth-client'); const api = require('auth-client/api').default; // Note: .default is required for api const { decodeToken } = require('auth-client/utils/jwt');
import { auth } from 'auth-client';
auth.setConfig({
clientKey: 'admin-ui',
authBaseUrl: 'http://auth.localhost:4000/auth',
});auth.login();For applications that coordinate login across same-origin tabs, prefer the promise-based entry point. It uses the Web Locks API when available and falls back to the storage lease for older or restricted browsers:
await auth.loginAsync();The login lease expires after 5 minutes as a crash-recovery upper bound. It is
not a user-facing wait: a waiting tab should offer an immediate takeover
action that calls auth.clearLoginLease() before starting a new login.
auth.handleCallback(); // Call this on /callback pageauth.logout();const unsubscribe = auth.subscribeToAuthEvents((event) => {
if (event.type === 'LOGIN_COMPLETED') {
// Re-establish this tab's session with auth.restoreSession().
}
});
// Call when the component or application is disposed.
unsubscribe();Login start/completion and logout events use the configured logoutChannelName
and carry metadata only. Applications must restore their own session; tokens
are never sent through the cross-tab transport.
const token = auth.getToken();import { AuthProvider } from 'auth-client/react/AuthProvider';
<AuthProvider>
<App />
</AuthProvider>import { useAuth } from 'auth-client/react/useAuth';
const { user, token, login, logout } = useAuth();import api from 'auth-client/api';
api.get('/me'); // sends Authorization headerimport { decodeToken, isTokenExpired } from 'auth-client/utils/jwt';- Token handling (in-memory + localStorage)
- CSRF-safe login with state param
- Auto API auth header via Axios
- React support via context and hooks
- No HttpOnly cookies — safe from XSS if you sandbox
localStorage - Handles CSRF via
state - Designed for refresh via backend
/refresh
npm pack
npm install ../auth-client-1.0.0.tgzMIT