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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@tourmalinecore/react-tc-auth

v2.0.1

Published

A simple provider, that will allow you easily implement athentication flow in your app.

Downloads

251

Readme

Tourmaline Core jwt authentication package

A simple provider, that will allow you easily implement athentication flow in your app.

Instalation

The package can be installed via npm:

npm install @tourmalinecore/react-tc-auth --save

Or via yarn:

yarn add @tourmalinecore/react-tc-auth

Configuration

import { createAuthService } from '@tourmalinecore/react-tc-auth';

const authService = createAuthService({
  authApiRoot: 'https://example.com/auth',
  authType: 'ls',
  tokenAccessor: 'accessToken',
  refreshTokenAccessor: 'refreshToken',
  tokenValueAccessor: 'value',
  tokenExpireAccessor: 'expiresInUtc',
})

createAuthService params

| Name | Description | |-|-| | authApiRoot | API root for authentication, e.g.: yourdomain/api/auth | | authType | Implementation type of the authentication, not just storage usage. Accepts 'ls' or 'cookies' | | tokenAccessor | Name of the property of the response object representing access token | | refreshTokenAccessor | Name of the property of the response object representing refresh token | | tokenValueAccessor | Name of the property of the token objects representing the token value | | tokenExpireAccessor | Name of the property of the token objects representing the expiration time of the token in UTC |

Usage

createAuthService creates an authService object, which provides next functionality:

| Name | Description | |-|-| | getAuthToken | Get the token value from the storage | | getAuthTokenOrRefresh | Async function, gets the token from the storage, refreshes if expired | | refreshToken | Async function, calls the API to refresh the token | | loginCall | Fetch the login data with axios | | logoutCall | Fetch the logout data with axios | | setLoggedIn | Sets the token to the storage | | setLoggedOut | Removes the token from the storage | | subscribeOnTokenChange | Adds listener for token change | | unsubscribeOnTokenChange | Removes listener for token change | | AuthContext | React auth context | | AuthProvider | React context provider | | withPrivateRoute | React HOC for the private routes | | useAuth | Hook for the custom react auth context provider |

Login

function login() {
    authService
      .loginCall({
        login: 'login',
        password: 'password',
      })
      .then((response) => authService.setLoggedIn(response.data));
  }

You can use it as is just by adding await getAuthTokenOrRefresh() to your API calls to get actual access token.

Use Token

function getData() {
    const token = await authProvider.getAuthTokenOrRefresh();

    return axios({
      url: 'https://example.com/data',
      method: 'GET',
      headers: {
        'Authentication': `Bearer ${token}`,
      },
    });
  }

Or you can add an error interceptor for Axios and call await refreshToken() inside it. Do not forget to change the logged state after it (setLoggedOut, setLoggedIn), these calls will notify the storage observers:

import axios from 'axios';

export const api = axios.create({
 baseURL: 'https://example.com',
});

api.interceptors.request.use((config) => {
 const token = authService.getAuthToken();

 config.headers.Authorization = token ? `Bearer ${token}` : '';

 return config;
}, () => {});

api.interceptors.response.use(() => {}, async (error) => {
 if (
   (error.response && error.response.status === 401)
   || (error.response && error.response.status === 403)
 ) {
   await authService.refreshToken();

   return api.request(error.config);
 }

 return Promise.reject(error);
});