npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@authgate/browser

v0.5.1

Published

Browser-side helpers for AuthGate (logout, CSRF forwarding)

Readme

@authgate/browser

Minimal browser-side helpers for applications using AuthGate.

This package provides explicit, framework-agnostic primitives for integrating browser-based UIs (SSR or SPA) with an AuthGate-backed authentication system.

It intentionally avoids hidden behavior, background state mutation, or framework-specific abstractions.


Design goals

  • Explicit behavior (no magic, no background auth)
  • Browser-only responsibility (cookies, CSRF, refresh)
  • Framework-agnostic (React, Vue, Svelte, vanilla JS)
  • Composable primitives + optional convenience helpers
  • Zero dependencies

Features

  • Read AuthGate CSRF token from browser cookies
  • Perform a CSRF-protected logout request
  • Explicit browser-side session refresh with audience selection
  • Optional fetch wrapper with single-retry refresh semantics
  • Clear success / failure signaling
  • Optional redirect on logout
  • No runtime dependencies

Installation

npm install @authgate/browser

Usage

Read CSRF token

import { getCSRFToken } from "@authgate/browser";

const csrf = getCSRFToken();

Returns the value of the authgate_csrf cookie, or null if not present.

This function only reads the CSRF token.
It does not generate or validate it.


Logout

import { logout } from "@authgate/browser";

const result = await logout();

This will:

  • Send a POST /auth/logout request
  • Attach the CSRF token via X-CSRF-Token
  • Include credentials (cookies)

The function returns an explicit result:

type LogoutResult =
  | { ok: true }
  | { ok: false; reason: "missing_csrf" | "request_failed" | "unauthorized" };

Applications that do not need to react programmatically may safely ignore the return value.


Logout with redirect

await logout({ redirectTo: "/" });

If the logout request succeeds, the browser is redirected to the given path.

Redirecting is an optional side-effect and does not define success.


Session refresh (explicit)

refreshSession

import { refreshSession } from "@authgate/browser";

const refreshed = await refreshSession("app");

Attempts to refresh the current AuthGate session by calling:

POST /auth/refresh

with an explicit audience declaration.

Audience

The audience determines which access token is minted (e.g. "app", "admin").

  • The client explicitly requests an audience
  • AuthGate validates the requested audience against the user’s roles
  • Requests for unauthorized audiences fail with 401

If no audience is provided, "app" is used by default.

Behavior

  • Returns true if refresh succeeded
  • Returns false if refresh failed for any reason

This function:

  • does not retry
  • does not redirect
  • does not throw
  • does not modify application state

It is intended for applications that want manual control over refresh logic.


Fetch wrapper (optional convenience)

authFetch

import { authFetch } from "@authgate/browser";

const res = await authFetch("/api/data");

authFetch is an optional convenience wrapper around fetch with AuthGate-aware refresh behavior.

Behavior

  1. Performs the request with credentials
  2. If the response is not 401, returns it directly
  3. If the response is 401:
    • Attempts refreshSession() with the same audience
    • If refresh succeeds, retries the original request once
    • Otherwise, returns the original 401 response

Audience-aware requests

await authFetch("/admin/api/users", {}, { audience: "admin" });
  • The same audience is used for the refresh attempt
  • Unauthorized audiences fail cleanly without retry loops

Important properties

  • At most one retry
  • No redirects
  • No background refresh
  • No swallowed failures

Applications remain fully in control of UX decisions.


Example (React / SPA)

const res = await authFetch("/api/me");

if (res.status === 401) {
  setUser(null);
}

Admin request:

const res = await authFetch(
  "/admin/api/users",
  {},
  { audience: "admin" },
);

Security model

  • CSRF tokens are not generated by this package
  • CSRF validation is enforced by AuthGate
  • Refresh tokens are never exposed to JavaScript
  • All authentication state is owned by AuthGate
  • Audiences are explicitly requested and server-validated

This package only forwards existing browser state explicitly.


What this package does NOT do

  • No authentication logic
  • No credential storage
  • No background token refresh
  • No session management
  • No authorization or role handling
  • No implicit redirects
  • No framework-specific helpers

This package exists solely to reduce boilerplate and prevent integration mistakes while preserving full application control.


Compatibility

  • Works with any backend protected by AuthGate
  • Supports SSR, SPA, and hybrid architectures

License