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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@hardwework/use-ful-auth

v0.1.2

Published

Simple way to dispatch the user based on it's status (logged in/not logged in)

Downloads

77

Readme


sidebar_position: 5

useAuth

Installation

Install the library with

npm install @hardwework/use-auth
import { generateApiClient } from "@hardwework/use-query";
import { AuthProvider } from "@hardwework/use-auth";
...
const apiClient = generateApiClient({
  baseUrl = "https://my.api.com/api/v1",
  authorizationHeader = "Authorization",
  authorizationPrefix = "Bearer"
})
// Or build your own apiClient whatever you want

const authUrl = "https://my.api.com/api/v1/auth";
...
root.render(
  <React.StrictMode>
    <AuthProvider apiClient={apiClient} authUrl={authUrl}>
      <App />
    </AuthProvider>
  </React.StrictMode >
);

useAuth

Parameters

| Parameter | Type | Default | Description | | ------------------ | -------------------- | ----------- | --------------------------------------------------------------------------------------------------------------- | | url | string | | Endpoint url | | method | string | GET | Request method (GET, POST...) | | executeImmediately | boolean | false | Sets whether the call should be executed when the component is created or wait for the call to executeQuery() | | onSuccess | (response) => void | () => { } | Function executed after a successful query | | onUnauthorized | (response) => void | () => { } | Function executed after an unsuccessful query if the response code is 401 | | onError | (response) => void | () => { } | Function executed after an unsuccessful query if the response code is not 401 |

Returned parameters

| Parameter | Type | Description | | ------------ | --------------------- | ------------------------------------------------------------------------------------------- | | isLogged | boolean | true if the user is logged, false otherwise | | isLoading | boolean | true while the query is being executed, false otherwise, even if it has not yet started | | isError | boolean | true while the query finished unsuccessfully, false otherwise | | isSuccess | boolean | true while the query finished successfully, false otherwise | | data | any | The query response if it finished successfully, undefined otherwise | | error | any | The query response if it finished unsuccessfully, undefined otherwise | | executeQuery | (data?: {}) => void | Trigger the query with optional body as parameter |

AuthRoute

Parameters

| Parameter | Type | Default | Description | | ------------------ | -------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | children | React | | The component to render | | forLoggedUser | boolean | false | If true the route is accessible only for logged users, if false the route is accessible only for not logged users | | action | function | | Function executed after the user is logged or not logged. The function is executed only if the user is logged or not logged, not if the user is redirected to the login page or home page. | | minimumLoadingTime | number | 1000 | Minimum time in milliseconds that the loading page is shown. If the loading time is less than the minimum loading time, the loading page is shown for the minimum loading time. | | loader | React | | Component to show while the user is logged or not logged. If not specified, the default loader is shown. |

Examples

Example 1: useAuth

import { useAuth } from "@hardwework/use-auth";

const Login = () => {
  const { isLogged, isLoading, isError, isSuccess, data, error, executeQuery } =
    useAuth({
      url: "/user/",
      method: "GET",
      executeImmediately: false,
      onSuccess: (response) => {
        console.log("Login success");
      },
      onUnauthorized: (response) => {
        console.log("Login failed");
      },
      onError: (response) => {
        console.log("Login failed");
      },
    });

  const checkUserStatus = () => {
    executeQuery();
  };

  return (
    <div>
      <button onClick={checkUserStatus}>Login</button>
    </div>
  );
};

Example 2: useAuth

import React, { useEffect } from "react";
import { useAuth } from "@hardwework/use-auth";

const Login = () => {
  const { isLogged, isLoading, isError, isSuccess, data, error, executeQuery } =
    useAuth({
      url: "/user/",
      method: "GET",
      executeImmediately: true,
      onSuccess: (response) => {
        console.log("Login success");
      },
      onUnauthorized: (response) => {
        console.log("Login failed");
      },
      onError: (response) => {
        console.log("Login failed");
      },
    });

  return (
    <div>
      {isLoading ? (
        <div>Loading...</div>
      ) : isLogged ? (
        <div>Logged</div>
      ) : (
        <div>Not logged</div>
      )}
    </div>
  );
};

Example 3: AuthRoute

import { AuthRoute } from "@hardwework/use-auth";

const App = () => {
  return (
    <AuthRoute
      minimumLoadingTime={1000} // Minimum time in milliseconds that the loading page is shown
      forLoggedUser={false} // If true the route is accessible only for logged users
      loader={<CustomLoader />} // leave it undefined if you want to use the built in Loader
      action={() => {
        // Function executed when the user is redirected
        navigate("/");
      }}
    >
      // Your view
    </AuthRoute>
  );
};