@spidy092/auth-client
v3.0.5
Published
Scalable frontend auth SDK for centralized login using Keycloak + Auth Service.
Maintainers
Readme
auth-client SDK
A lightweight, framework-agnostic authentication client SDK designed for scalable React (and non-React) apps using centralized login via Keycloak + Auth Service.
📦 Installation
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');
🔧 Setup
import { auth } from 'auth-client';
auth.setConfig({
clientKey: 'admin-ui',
authBaseUrl: 'http://auth.localhost:4000/auth',
});🚀 Usage
Login
auth.login();Handle Callback
auth.handleCallback(); // Call this on /callback pageLogout
auth.logout();Get Token
const token = auth.getToken();🧠 React Integration
Provider
import { AuthProvider } from 'auth-client/react/AuthProvider';
<AuthProvider>
<App />
</AuthProvider>Hook
import { useAuth } from 'auth-client/react/useAuth';
const { user, token, login, logout } = useAuth();📡 Authenticated API
import api from 'auth-client/api';
api.get('/me'); // sends Authorization header🧪 Utilities
import { decodeToken, isTokenExpired } from 'auth-client/utils/jwt';✅ Built-in Features
- Token handling (in-memory + localStorage)
- CSRF-safe login with state param
- Auto API auth header via Axios
- React support via context and hooks
🔐 Security
- No HttpOnly cookies — safe from XSS if you sandbox
localStorage - Handles CSRF via
state - Designed for refresh via backend
/refresh
📦 To Publish Locally
npm pack
npm install ../auth-client-1.0.0.tgz🏁 License
MIT
