Core TypeScript SDK (@authsetu/sdk-core)
The @authsetu/sdk-core library provides low-level client architecture, token lifecycle management, event streams, and feature tier enforcement for Node.js, browser, and custom framework environments.
Installation
bash
npm install @authsetu/sdk-core---
1. Initializing AuthSetuClient
Create a singleton client instance configured with your project's Publishable or Secret Key.
typescript
import { AuthSetuClient } from "@authsetu/sdk-core";
const client = new AuthSetuClient({
publishableKey: "pk_test_123456789",
baseUrl: "https://api.authsetu.com",
autoRefreshTokens: true,
storage: "localStorage", // 'localStorage' | 'sessionStorage' | 'cookie'
});
await client.initialize();---
2. Token Lifecycle & Auto Refresh Loop
AuthSetuClient manages JWT access tokens and refresh tokens under the hood:
typescript
// Retrieve valid access token (automatically refreshes if expired)
const accessToken = await client.tokenManager.getValidToken();
// Force token refresh manually
const newTokens = await client.tokenManager.refreshToken();
console.log("Current Access Token:", accessToken);typescript
// Subscribe to authentication state changes
client.events.on("sessionExpired", () => {
console.warn("User session expired. Prompting re-login...");
});
client.events.on("tokenRefreshed", ({ accessToken }) => {
console.log("Access token silently refreshed:", accessToken);
});
client.events.on("organizationSwitched", (org) => {
console.log("Switched active organization to:", org.name);
});---
4. Feature Tier & Plan Limit Guards
Check subscription tier features and limits programmatically using checkPlanFeature and checkPlanUsageLimit:
typescript
import { checkPlanFeature, checkPlanUsageLimit, PLAN_LIMITS } from "@authsetu/sdk-core";
const userPlan = "pro"; // 'free' | 'starter' | 'pro' | 'enterprise'
// Check boolean feature permission
const canAccessSAML = checkPlanFeature(userPlan, "samlSso");
console.log("SAML Access:", canAccessSAML);
// Check usage limits (e.g. max team members)
const usageCheck = checkPlanUsageLimit(userPlan, "maxMembers", 15);
if (usageCheck.isExceeded) {
console.warn(`Limit reached: ${usageCheck.current} / ${usageCheck.limit}`);
}---
5. PKCE Challenge Generation for OAuth
Generate secure Proof Key for Code Exchange (PKCE) challenges for native or web OAuth flows:
typescript
import { generatePKCEChallenge, generateState } from "@authsetu/sdk-core";
const state = generateState();
const { codeVerifier, codeChallenge, codeChallengeMethod } = await generatePKCEChallenge();
// Store codeVerifier in session storage before redirecting to identity provider
sessionStorage.setItem("pkce_verifier", codeVerifier);
console.log("Code Challenge:", codeChallenge);