@storm-gate/client
v0.1.0
Published
Browser SDK for the Storm-Gate auth service.
Maintainers
Readme
@storm-gate/client
Browser SDK for the Storm-Gate auth service. Replaces hand-rolled axios + cookie + JWT plumbing in consumer apps.
Install
npm install @storm-gate/client axiosaxios is a peer dependency — you control the version.
Quickstart
import { createStormGateClient } from '@storm-gate/client';
export const auth = createStormGateClient({
baseURL: process.env.REACT_APP_STORM_GATE_URL,
rememberMeMaxAge: 7 * 24 * 3600,
defaultMaxAge: 24 * 3600,
isAuthRequiredRoute: (pathname) => pathname.startsWith('/admin'),
onUnauthenticated: () => { window.location.href = '/login'; },
});
await auth.login({ email, password, rememberMe: true });
const { user } = await auth.getMe(); // /login does not return user — call /me separately
await auth.logout();
// For your OWN backend, with the same cookie token attached:
const api = auth.createAuthedAxios({ baseURL: process.env.REACT_APP_LOCAL_API_URL });
await api.get('/articles');API
createStormGateClient(options)
Options:
baseURL(string, required) — Storm-Gate base URL.cookieName(string, default'accesstoken') — cookie name for the access token.rememberMeMaxAge(seconds, default7 * 24 * 3600) — cookie TTL whenrememberMe: true.defaultMaxAge(seconds, default24 * 3600) — cookie TTL otherwise.isAuthRequiredRoute((pathname) => boolean, default returns false) — controls whether a normalized 401 firesonUnauthenticated.onUnauthenticated(() => void, optional) — called once per session when an authed request fails.strictNormalization(boolean, defaultfalse) — whentrue, skips the 400→401 heuristic and only treats real 401s as auth failures.
Methods returned:
login({ email, password, rememberMe? })→{ accesstoken, status, limitedAccess? }register({ name, email, password, role?, application?, status? })→{ accesstoken?, status, requiresApproval?, msg? }getMe()→{ status, user }logout()→{ msg, status }refreshToken()→{ accesstoken }checkStatus({ email })→{ status, user }forgotPassword({ email })→{ msg, status }verifyResetToken(token)→{ msg, status, email }— token is in URL pathresetPassword({ token, password })→{ msg, status }— token is in URL pathcreateAuthedAxios({ baseURL, ...config })→ axios instance that auto-attaches the same cookie token
Behavior notes
- Header format: the SDK sends
Authorization: <jwt>(noBearerprefix). Storm-Gate accepts both. - 400→401 normalization: Storm-Gate currently returns 400 (not 401) for expired or invalid tokens. The SDK normalizes these so
error.response.status === 401for callers. Login/register are excluded from this heuristic since their 400s are business errors. Disable withstrictNormalization: true. - Cross-origin: the SDK uses
withCredentials: trueso the HttpOnlyrefreshtokencookie can roundtrip onrefreshToken(). Requires Storm-Gate's CORS config to allowlist your origin withcredentials: true. auth.login()does NOT returnuser. Storm-Gate's/loginresponse omits it; callauth.getMe()after if you need it.
v0.1 limitations
- HS256 only. RS256/JWKS coming in v0.2.
- Browser only — no SSR / Node cookie support. Will throw if
documentis not available. - No silent refresh loop — call
refreshToken()manually when needed.
License
ISC
