0.0.17 • Published 3 years ago

react-query-auth-hook v0.0.17

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

React Query Auth Hook

This lib is using axios and react-query. It configures globaly the axios baseURL and the token when necessary. It handles the user entire journey.

Installation

yarn add react-query-auth-hook

or

npm install react-query-auth-hook

Usage

Configuration

// App.js
import React from "react";
import { QueryClient, QueryClientProvider } from "react-query";
import { AuthProvider } from "react-query-auth-hook";

export const queryClient = new QueryClient();

const config = {
  baseURL: "http://localhost:3000",
  tokenKey: "access_token", // The token key in the response
  endpoints: {
    user: "/me", // GET
    login: "/authentication/login", // POST
    register: "/authentication/register", // POST
  },
};

export default function App() {
  return (
    <QueryClientProvider client={queryClient}>
      <AuthProvider config={config}>
        <MyApp />
      </AuthProvider>
    </QueryClientProvider>
  );
}

Use it

You can now use the useAuth hook in the whole application:

import { useAuth } from "react-query-auth-hook";

const {
    login,
    loginIsError,
    loginIsLoading,
    loginIsSuccess,
    loginError,
    register,
    registerIsError,
    registerIsLoading,
    registerIsSuccess,
    registerError,
    isLoggedIn: !!user,
    user,
    logout,
    appIsLoaded,
} = useAuth();
0.0.17

3 years ago

0.0.12

3 years ago

0.0.13

3 years ago

0.0.14

3 years ago

0.0.15

3 years ago

0.0.16

3 years ago

0.0.11

3 years ago

0.0.10

3 years ago

0.0.9

3 years ago

0.0.8

3 years ago

0.0.7

3 years ago

0.0.6

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago