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

@authcore/react

v0.8.0

Published

React SDK for AuthCore — AuthProvider, useAuth, ProtectedRoute

Readme

@authcore/react

React SDK for AuthCore. Includes AuthProvider, useAuth hook, and ProtectedRoute. Works with any backend — not just AuthCore.

Install

npm install @authcore/react

Usage

Setup

Wrap your app with AuthProvider:

import { AuthProvider } from '@authcore/react'

function App() {
  return (
    <AuthProvider baseUrl="http://localhost:3000/auth" mode="api">
      <Main />
    </AuthProvider>
  )
}

useAuth Hook

import { useAuth } from '@authcore/react'

function Main() {
  const { user, isAuthenticated, isLoading, error, signIn, signUp, signOut } = useAuth()

  if (isLoading) return <p>Loading...</p>
  if (error) return <p>Error: {error}</p>

  if (!isAuthenticated) {
    return (
      <button onClick={() => signIn('[email protected]', 'password')}>
        Sign In
      </button>
    )
  }

  return (
    <div>
      <p>Hello, {user?.email}</p>
      <button onClick={() => signOut()}>Sign Out</button>
    </div>
  )
}

Extended user type

If your backend returns extra fields on the user object, pass a type parameter to useAuth:

import type { PublicUser } from '@authcore/types'

interface MyUser extends PublicUser {
  avatarUrl: string
  displayName: string
}

function Profile() {
  const { user } = useAuth<MyUser>()
  return <img src={user?.avatarUrl} />
}

Pair this with transformUser on <AuthProvider> to map your backend's response shape.

ProtectedRoute

Renders children only when authenticated:

import { ProtectedRoute } from '@authcore/react'

<ProtectedRoute
  fallback={<p>Loading...</p>}
  onUnauthenticated={() => navigate('/login')}
>
  <Dashboard />
</ProtectedRoute>

Password Reset & Email Verification

const { forgotPassword, resetPassword, verifyEmail } = useAuth()

await forgotPassword('[email protected]')
await resetPassword(token, 'new-password')
await verifyEmail(token)

RBAC Hooks

import { useRole, useHasRole } from '@authcore/react'

function AdminPanel() {
  const role = useRole()                       // 'admin', 'user', etc. or null
  const isAdmin = useHasRole('admin')          // true/false
  const isStaff = useHasRole(['admin', 'editor'])

  if (!isAdmin) return <p>Access denied</p>
  return <p>Welcome, {role}</p>
}

Invitation

const { invite, acceptInvitation } = useAuth()

await invite('[email protected]', 'editor')
const { user } = await acceptInvitation(token, 'mypassword123')

Custom Backend Integration

@authcore/react works with any backend. Use the response transformer props to adapt your backend's JSON shape.

Different response shapes

// Backend returns: { data: { user }, access_token: "..." }
<AuthProvider
  baseUrl="https://api.example.com"
  transformAuthResponse={(raw) => {
    const r = raw as { data: { user: MyUser }; access_token: string }
    return { user: r.data.user, token: r.access_token }
  }}
  transformUser={(raw) => (raw as { data: MyUser }).data}
/>

Different error shapes

// Backend returns: { message: "Unauthorized" }
<AuthProvider
  baseUrl="https://api.example.com"
  transformError={(body, status) => {
    const err = body as { message?: string }
    return err.message ?? `Request failed with status ${status}`
  }}
/>

Custom HTTP client (Axios, etc.)

import axios from 'axios'

const client = {
  get: <T>(path: string) =>
    axios.get<T>(`https://api.example.com${path}`).then(r => r.data),
  post: <T>(path: string, body?: unknown) =>
    axios.post<T>(`https://api.example.com${path}`, body).then(r => r.data),
}

<AuthProvider httpClient={client}>
  <App />
</AuthProvider>

API Reference

<AuthProvider>

| Prop | Type | Default | Description | |------|------|---------|-------------| | baseUrl | string | required | Auth API base URL | | mode | 'api' \| 'cookie' | 'api' | api uses Bearer tokens, cookie uses httpOnly cookies | | storageKey | string | 'authcore_token' | localStorage key for the token (api mode) | | persistSession | boolean | true | Persist token in localStorage (api mode) | | routes | object | see below | Override default endpoint paths | | transformAuthResponse | (raw: unknown) => { user, token? } | — | Map sign-in/sign-up/accept-invitation response | | transformUser | (raw: unknown) => TUser | — | Map /me response | | transformError | (body: unknown, status: number) => string | — | Map error body to message string | | httpClient | { get, post } | — | Replace the built-in fetch client entirely |

Default route paths:

| Route | Default | |-------|---------| | register | /register | | login | /login | | logout | /logout | | me | /me | | verifyEmail | /verify-email | | forgotPassword | /forgot-password | | resetPassword | /reset-password | | invite | /invite | | acceptInvitation | /accept-invitation |

useAuth<TUser>() Return Value

| Property | Type | Description | |----------|------|-------------| | user | TUser \| null | Current user or null | | isLoading | boolean | True while restoring session | | isAuthenticated | boolean | True if user is logged in | | error | string \| null | Last error message, or null | | signUp(email, password) | Promise<AuthResponse<TUser>> | Register a new account | | signIn(email, password) | Promise<AuthResponse<TUser>> | Sign in | | signOut() | Promise<void> | Sign out | | forgotPassword(email) | Promise<void> | Request password reset email | | resetPassword(token, password) | Promise<void> | Reset password | | verifyEmail(token) | Promise<void> | Verify email address | | invite(email, role?) | Promise<void> | Invite a new user | | acceptInvitation(token, password) | Promise<AuthResponse<TUser>> | Accept an invitation | | refreshUser() | Promise<void> | Re-fetch current user from /me |

<ProtectedRoute>

| Prop | Type | Description | |------|------|-------------| | children | ReactNode | Content to show when authenticated | | fallback | ReactNode | Shown while loading (default: null) | | onUnauthenticated | () => void | Called when user is not authenticated |

Cookie Mode

When your backend and frontend share the same domain:

<AuthProvider baseUrl="/auth" mode="cookie">
  <App />
</AuthProvider>

In cookie mode, the SDK uses credentials: 'include' and does not store tokens in localStorage.

Using Types Directly

import type { PublicUser } from '@authcore/types'
import type { AuthResponseTransformers } from '@authcore/core-web'

License

MIT