• Stars
    star
    270
  • Rank 152,189 (Top 3 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created almost 4 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

⚛️ Authenticate your react applications easily with react-query.

react-query-auth

NPM

Authenticate your react applications easily with React Query.

Introduction

Using React Query has allowed us to significantly reduce the size of our codebase by caching server state. However, we still need to consider where to store user data, which is a type of global application state that we need to access from many parts of the app, but is also a server state since it is obtained from a server. This library makes it easy to manage user authentication, and can be adapted to work with any API or authentication method.

Installation

$ npm install @tanstack/react-query react-query-auth

Or if you use Yarn:

$ yarn add @tanstack/react-query react-query-auth

Usage

To use this library, you will need to provide it with functions for fetching the current user, logging in, registering, and logging out. You can do this using the configureAuth function:

import { configureAuth } from 'react-query-auth';

const { useUser, useLogin, useRegister, useLogout } = configureAuth({
  userFn: () => api.get('/me'),
  loginFn: (credentials) => api.post('/login', credentials),
  registerFn: (credentials) => api.post('/register', credentials),
  logoutFn: () => api.post('/logout'),
});

With these hooks, you can then add authentication functionality to your app. For example, you could use the useUser hook to access the authenticated user in a component.

You can also use the useLogin, useRegister, and useLogout hooks to allow users to authenticate and log out.

function UserInfo() {
  const user = useUser();
  const logout = useLogout();

  if (user.isLoading) {
    return <div>Loading ...</div>;
  }

  if (user.error) {
    return <div>{JSON.stringify(user.error, null, 2)}</div>;
  }

  if (!user.data) {
    return <div>Not logged in</div>;
  }

  return (
    <div>
      <div>Logged in as {user.data.email}</div>
      <button disabled={logout.isLoading} onClick={logout.mutate({})}>
        Log out
      </button>
    </div>
  );
}

The library also provides the AuthLoader component that can be used to handle loading states when fetching the authenticated user. You can use it like this:

function MyApp() {
  return (
    <AuthLoader
      renderLoading={() => <div>Loading ...</div>}
      renderUnauthenticated={() => <AuthScreen />}
    >
      <UserInfo />
    </AuthLoader>
  );
}

NOTE: All hooks and components must be used within QueryClientProvider.

API Reference:

configureAuth:

The configureAuth function takes in a configuration object and returns a set of custom hooks for handling authentication.

The configureAuth configuration object:

A configuration object that specifies the functions and keys to be used for various authentication actions. It accepts the following properties:

  • userFn: A function that is used to retrieve the authenticated user. It should return a Promise that resolves to the user object, or null if the user is not authenticated.

  • loginFn: A function that is used to log the user in. It should accept login credentials as its argument and return a Promise that resolves to the user object.

  • registerFn: A function that is used to register a new user. It should accept registration credentials as its argument and return a Promise that resolves to the new user object.

  • logoutFn: A function that is used to log the user out. It should return a Promise that resolves when the logout action is complete.

  • userKey: An optional key that is used to store the authenticated user in the react-query cache. The default value is ['authenticated-user'].

The configureAuth returned object:

configureAuth returns an object with the following properties:

  • useUser: A custom hook that retrieves the authenticated user. It is a wrapper around useQuery that uses the userFn and userKey specified in the configAuth configuration. The hook accepts the same options as useQuery, except for queryKey and queryFn, which are predefined by the configuration.

  • useLogin: A custom hook that logs the user in. It is a wrapper around useMutation that uses the loginFn specified in the configuration. The hook accepts the same options as useMutation, except for mutationFn, which is set by the configuration. On success, the hook updates the authenticated user in the React Query cache using the userKey specified in the configuration.

  • useRegister: A custom hook that registers a new user. It is a wrapper around useMutation that uses the registerFn specified in the configuration. The hook accepts the same options as useMutation, except for mutationFn, which is set by the configuration. On success, the hook updates the authenticated user in the React Query cache using the userKey specified in the configuration.

  • useLogout: A custom hook that logs the user out. It is a wrapper around useMutation that uses the logoutFn specified in the configuration. The hook accepts the same options as useMutation, except for mutationFn, which is set by the configuration. On success, the hook removes the authenticated user from the React Query cache using the userKey specified in the configuration.

  • AuthLoader:

    A component that can be used to handle loading states when fetching the authenticated user. It accepts the following props:

    • renderLoading: A function that is called when the authenticated user is being fetched. It should return a React node that is rendered while the user is being fetched.

    • renderUnauthenticated: A function that is called when the authenticated user is not authenticated. It should return a React node that is rendered when the user is not authenticated.

    • renderError: A function that is called when an error is thrown during the authentication request. It should return a React node that is rendered when the error occurs. It receives the Error object which can be used during rendering. Defaults to (error: Error) => <>{JSON.stringify(error)}</>.

    • children: A React node that is rendered when the authenticated user is successfully fetched.

If you need a more custom loading implementation, you can create your own AuthLoader component and use the useUser hook to fetch the authenticated user and handle the loading and error states yourself.

Examples:

To try out the library, check out the examples folder.

Contributing

  1. Clone this repo
  2. Create a branch: git checkout -b your-feature
  3. Make some changes
  4. Test your changes
  5. Push your branch and open a Pull Request

LICENSE

MIT

More Repositories

1

bulletproof-react

🛡️ ⚛️ A simple, scalable, and powerful architecture for building production ready React applications.
TypeScript
20,739
star
2

awesome-codebases

A collection of awesome open-source codebases worth exploring.
468
star
3

nextjs-jwt-authentication

A proof of concept app for demonstrating authentication of Next.js app with JWT.
JavaScript
271
star
4

blitzjs-realworld-app

Content Sharing App
TypeScript
45
star
5

express-server-jwt

A simple express server that handles JWT authentication
JavaScript
41
star
6

strapi-plugin-sync-roles-permissions

Store user roles and permissions configuration as a JSON file and then import and reuse it any time.
JavaScript
39
star
7

epic-stack-with-user-impersonation

An example Remix application showcasing how to implement user impersonation in the Epic Stack.
TypeScript
26
star
8

react-multistep-wizard

🧙 React wizard component
TypeScript
11
star
9

MERN-starter

Starter boilerplate for creating full stack MERN apps
JavaScript
7
star
10

react-scrollable-pagination

📜 Paginate your application's data by scrolling...
JavaScript
5
star
11

e-shop-with-mf

Micro frontend based E-shop app composed with Module Federation
TypeScript
4
star
12

data-visualization

Data Visualization with D3 and React
JavaScript
3
star
13

mobx-async-toolkit

Toolkit for handling async operations in MobX stores
TypeScript
3
star
14

crypto-watcher

App for tracking cryptocurrencies built with React Native
JavaScript
2
star
15

alan2207

2
star
16

tailwindcss-static-starter

TailwindCSS Static Starter
HTML
2
star
17

multistep-formik

Example app showing how to handle multistep forms with Formik.
JavaScript
2
star
18

interest

Fullstack Pinterest clone app
JavaScript
1
star
19

book-trading-app

Fullstack app for trading books
JavaScript
1
star
20

stock-market-monitor

Fullstack app for tracking stocks.
JavaScript
1
star
21

voting-app

Fullstack voting app
JavaScript
1
star
22

Towers-of-Hanoi-Solver

Simulation of solving Towers of Hanoi.
JavaScript
1
star
23

apexcharts-react

Unofficial react wrapper for apexcharts.js
JavaScript
1
star