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

newpaper-api

v2.8.234

Published

## Overview

Readme

Auth Context and API Integration README

Overview

This project provides a React context and set of hooks to manage authentication in your React applications. It facilitates API requests with Bearer token authentication, session handling, and login redirection. The context also supports custom hooks for GET, POST, PATCH, and DELETE requests.

Table of Contents

Installation

To integrate this into your React project, copy the provided code into your project and ensure the necessary dependencies are installed.

Install necessary packages:

npm install newpaper-api

Usage

1. AuthProvider

This provider manages authentication state and provides the loginPageUrl and apiBaseUrl to the components that consume this context. Wrap your App Components with {children} like so :

Example:

import { AuthProvider } from "newpaper-api";

<AuthProvider
  loginPageUrl="https://login.example.com"
  apiBaseUrl="https://api.example.com"
>
  <YourAppComponents />
</AuthProvider>;

2. LoginButton

The LoginButton component provides a pre-built button for redirecting users to the login page.

Example:

a. useAuthSession

This hook tells us if the user should log or not.

import { LoginButton, useAuthSession } from "newpaper-api";

// Your Component
const { shouldLogin } = useAuthSession();

{
  shouldLogin && <LoginButton />;
}
{
  !shouldLogin && <AppContent />;
}

3. API Hooks

b. useGet

This hook makes a GET request with the Bearer token automatically included in the request header.

const { data, loading, error, refetch } = useGet({
  path: "/data-endpoint",
  options: { queryString: "?param=value" },
});

c. usePost

The usePost hook is for sending POST requests. It returns a submit function to be called with the request body.

const { loading, error, submit } = usePost({ path: "/submit-data" });

submit({ key: "value" }, (response) => {
  console.log("Success", response);
});

d. usePatch

Use this hook for making PATCH requests.

const { loading, error, submit } = usePatch({ path: "/update-data" });

submit({ updateKey: "newValue" }, (response) => {
  console.log("Update Success", response);
});

e. useDelete

Use this hook for making DELETE requests.

const { loading, error, submit } = useDelete({ path: "/delete-data" });

submit({}, (response) => {
  console.log("Deleted", response);
});

Utility Functions

1. openLogin

This function opens a login popup to redirect users to the authentication page.

openLogin({ loginPageUrl: "https://login.example.com" });

2. getBToken

Retrieves the Bearer token (bearer_token) from the URL query parameters.

const token = getBToken();

3. isTokenExpired

Checks if a JWT token has expired.

const expired = isTokenExpired(token);
if (expired) {
  // Redirect user to login
}

Error Handling

Each hook (useGet, usePost, usePatch, and useDelete) returns an error state, which contains any errors encountered during the request. This should be handled in your UI, typically by showing an error message.

if (error) {
  console.error(error.message);
}

Examples

Using useGet to Fetch Data

const { data, loading, error, refetch } = useGet({ path: "/user/profile" });

if (loading) {
  return <p>Loading...</p>;
}

if (error) {
  return <p>Error: {error.message}</p>;
}

return <div>{data?.name}</div>;

Using usePost to Submit Data

const { submit, loading, error } = usePost({ path: "/user/register" });

const handleRegister = () => {
  submit({ username: "newUser" }, (data) => {
    console.log("Registration success:", data);
  });
};

return (
  <div>
    <button onClick={handleRegister} disabled={loading}>
      Register
    </button>
    {error && <p>Error: {error.message}</p>}
  </div>
);

Handling Login Session

const { shouldLogin } = useAuthSession();

if (shouldLogin) {
  openLogin({ loginPageUrl: "https://login.example.com" });
}

License

This project is licensed under the MIT License.