Registry / auth-security / react-token-auth

react-token-auth

JSON →
library2.3.8jsnpmunverified

The `react-token-auth` library, currently at version 2.3.8, offers a specialized solution for managing JWT `accessToken` and `refreshToken` pairs within React applications. Its primary function is to abstract the complexities of token storage (e.g., `localStorage` for web or custom async storage for React Native), synchronize the application's authentication state, and automatically handle token expiration and refreshing. A key feature is its `onUpdateToken` callback, which allows developers to integrate with their backend's token refresh endpoint, and its mechanism to prevent concurrent token update requests. While the author acknowledges that cookie-based sessions are generally preferred for web security, `react-token-auth` addresses specific use cases where client-side JWT storage is a requirement. The library provides a concise API including `createAuthProvider` to configure the system, `useAuth` to access the authentication state in components, and `authFetch` to automatically attach tokens to requests. The project appears actively maintained with regular updates and ships with TypeScript type definitions, making it suitable for modern React development practices.

npm install react-token-auth
INSTALL
IMPORT
SIG · REACT-TOKEN-AUTH
R
react-token-auth
auth-securityjavascriptv2.3.8
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

createAuthProvider
import { createAuthProvider } from 'react-token-auth';
const { createAuthProvider } = require('react-token-auth');
Primarily designed for ESM and TypeScript; CommonJS require() pattern is incorrect for modern usage.
useAuth
import { useAuth } from 'react-token-auth';
Destructured from the return value of `createAuthProvider` but typically imported directly for convenience.
login
import { login } from 'react-token-auth';
Similarly, `login`, `logout`, and `authFetch` are exposed directly for module-level usage after `createAuthProvider` is called.

This quickstart demonstrates the core functionality: initializing the auth provider, implementing login/logout, using the `useAuth` hook for conditional rendering, and making authenticated API calls with `authFetch`.

import { createAuthProvider } from 'react-token-auth'; import React, { FormEvent, useEffect } from 'react'; import { BrowserRouter, Switch, Route, Redirect } from 'react-router-dom'; // Assuming react-router-dom for routing context // Define the shape of your session object type Session = { accessToken: string; refreshToken: string }; // 1. Create the auth provider instance export const { useAuth, authFetch, login, logout } = createAuthProvider<Session>({ getAccessToken: session => session.accessToken, // Use localStorage for web applications. Ensure it's available (e.g., client-side). storage: typeof window !== 'undefined' ? localStorage : undefined, onUpdateToken: async (token: { refreshToken: string }) => { // This function is called when the accessToken needs to be refreshed. // It must return a new session object { accessToken: string; refreshToken: string } try { const response = await fetch('/update-token', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ refreshToken: token.refreshToken }), }); if (!response.ok) { const errorData = await response.json(); console.error('Token refresh failed:', errorData); throw new Error('Failed to refresh token'); } return response.json(); } catch (error) { console.error('Network or server error during token refresh:', error); throw error; } }, // Optional: Callback when a session is loaded from storage (hydration) onHydratation: session => { console.log('Session hydrated:', session); }, }); // A dummy component for registration const Register = () => <div>Register Page</div>; // 2. Example Login Component const Login = () => { const onSubmit = async (e: FormEvent) => { e.preventDefault(); // Simulate a login API call try { const response = await fetch('/login', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ username: 'testuser', password: 'password' }), // Replace with actual credentials }); if (!response.ok) { const errorData = await response.json(); console.error('Login failed:', errorData); alert('Login failed!'); return; } const session = await response.json(); login(session); // Save the new session alert('Logged in successfully!'); } catch (error) { console.error('Network error during login:', error); alert('Network error during login!'); } }; return ( <form onSubmit={onSubmit}> <h2>Login</h2> <input type="text" placeholder="Username" /> <input type="password" placeholder="Password" /> <button type="submit">Login</button> </form> ); }; // A dummy dashboard component const Dashboard = () => { const handleLogout = () => { logout(); alert('Logged out!'); }; // Example of using authFetch to make authenticated requests const fetchData = async () => { try { const response = await authFetch('/api/protected-data'); const data = await response.json(); console.log('Protected data:', data); alert('Fetched protected data: ' + JSON.stringify(data)); } catch (error) { console.error('Failed to fetch protected data:', error); alert('Failed to fetch protected data. Maybe token expired or invalid.'); } }; return ( <div> <h2>Dashboard</h2> <button onClick={handleLogout}>Logout</button> <button onClick={fetchData}>Fetch Protected Data</button> </div> ); }; // 3. Main Router component using useAuth hook to manage routing based on auth state const AppRouter = () => { const [logged, session] = useAuth(); // Get current auth state useEffect(() => { console.log('Auth state changed:', logged, session); }, [logged, session]); return ( <BrowserRouter> <Switch> {!logged ? ( <> <Route path="/register" component={Register} /> <Route path="/login" component={Login} /> <Redirect to="/login" /> </> ) : ( <> <Route path="/dashboard" component={Dashboard} exact /> <Redirect to="/dashboard" /> </> )} </Switch> </BrowserRouter> ); }; // To run this example in a real React app, you would render <AppRouter /> in your ReactDOM.render() // For demonstration purposes, we omit the ReactDOM.render() call.
Debug
Known issues
gotchaStoring JWT tokens in `localStorage` is generally considered less secure than HTTP-only cookie sessions due to vulnerability to Cross-Site Scripting (XSS) attacks. If an attacker injects malicious JavaScript, they can access and steal the tokens.
fix
While `react-token-auth` facilitates `localStorage` usage, consider using HTTP-only cookies for web applications where possible. If `localStorage` is necessary, implement robust Content Security Policies (CSPs) and regularly audit for XSS vulnerabilities.
affects: >=1.0.0
gotchaThe `onUpdateToken` function must correctly handle API responses and always return a new session object with `accessToken` and `refreshToken`. Incorrect responses (e.g., malformed JSON, missing tokens) can lead to an unauthenticated state or errors.
fix
Ensure your `onUpdateToken` implementation includes thorough error handling for network issues and non-2xx HTTP responses. Validate the structure of the returned session object (`{ accessToken: string; refreshToken: string }`) and gracefully handle parsing failures.
affects: >=1.0.0
gotchaFailing to properly handle errors or network issues within the `onUpdateToken` callback can lead to an infinite loop of refresh attempts or leave the user in a permanently unauthenticated state without clear recovery.
fix
Implement retry logic with exponential backoff and a maximum number of retries for `onUpdateToken`. If refresh consistently fails, force a logout or redirect to the login page, informing the user about the issue.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: Cannot read properties of undefined (reading 'accessToken')
The `getAccessToken` property in `createAuthProvider` is misconfigured, or the session object passed to `login` is `null`/`undefined` or lacks the expected `accessToken` field.
fix
Verify that your `Session` type correctly matches the structure of tokens returned by your backend and that `getAccessToken` accurately points to the access token. Ensure `login()` is called with a valid session object.
Uncaught (in promise) TypeError: Failed to fetch
A network request initiated by `onUpdateToken`, `login`, `logout`, or `authFetch` failed due to network connectivity issues, incorrect URL, CORS policies, or an unreachable server.
fix
Check your network connection and server status. Verify the URLs used in `fetch` calls. Ensure your server correctly handles CORS preflight requests and has the expected endpoints (`/login`, `/update-token`, etc.).
ReferenceError: localStorage is not defined
The library is configured to use `localStorage` but is being run in a server-side rendering (SSR) environment or React Native without a compatible storage shim.
fix
When using `react-token-auth` in SSR or React Native, provide a custom `storage` implementation to `createAuthProvider` that defers to `localStorage` only on the client or uses an appropriate async storage solution.
RangeError: Maximum call stack size exceeded
Often indicative of an infinite loop within the token refresh logic. This can happen if `onUpdateToken` repeatedly fails in a way that triggers itself again without a circuit breaker or proper error exit.
fix
Review the `onUpdateToken` implementation to ensure it has robust error handling and a clear exit strategy for persistent failures, preventing recursive calls without resolution. Implement safeguards like retry limits.
Upgrade
Version history
2.3.8latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
7 hits · last 30 days
node
6
OpenAI (training)
1
Resources
react-token-auth — npm install react-token-auth · libregistry