Convert Figma logo to code with AI

mjrussell logoredux-auth-wrapper

A React Higher Order Component (HOC) for handling Authentication and Authorization with Routing and Redux

2,195
241
2,195
33

Top Related Projects

Declarative routing for React

The official, opinionated, batteries-included toolset for efficient Redux development

WAI-ARIA compliant React autosuggest component

33,863

Build forms in React, without the tears 😭

Quick Overview

Redux Auth Wrapper is a higher-order component (HOC) library for handling authentication and authorization in React applications using Redux. It provides a flexible and composable way to protect routes, components, and actions based on user authentication state and permissions.

Pros

  • Easy integration with existing Redux and React Router setups
  • Highly customizable and configurable for various authentication scenarios
  • Supports both synchronous and asynchronous authentication checks
  • Provides helpful utility functions for common auth-related tasks

Cons

  • Requires a good understanding of Redux and HOCs
  • May add complexity to smaller projects where simpler auth solutions could suffice
  • Documentation could be more comprehensive, especially for advanced use cases
  • Not actively maintained (last update was in 2018)

Code Examples

  1. Protecting a route:
import { connectedRouterRedirect } from 'redux-auth-wrapper/history4/redirect'

const userIsAuthenticated = connectedRouterRedirect({
  redirectPath: '/login',
  authenticatedSelector: state => state.user !== null,
  wrapperDisplayName: 'UserIsAuthenticated'
})

const ProtectedComponent = userIsAuthenticated(MyComponent)
  1. Redirecting authenticated users:
import { connectedReduxRedirect } from 'redux-auth-wrapper/history4/redirect'

const userIsNotAuthenticated = connectedReduxRedirect({
  redirectPath: (state, ownProps) => locationHelper.getRedirectQueryParam(ownProps) || '/dashboard',
  allowRedirectBack: false,
  authenticatedSelector: state => state.user === null,
  wrapperDisplayName: 'UserIsNotAuthenticated'
})

const LoginPage = userIsNotAuthenticated(Login)
  1. Combining multiple auth wrappers:
import { compose } from 'redux'

const userIsAuthenticatedAndAdmin = compose(
  userIsAuthenticated,
  userIsAdmin
)

const AdminOnlyComponent = userIsAuthenticatedAndAdmin(AdminComponent)

Getting Started

  1. Install the package:

    npm install --save redux-auth-wrapper
    
  2. Create an auth wrapper:

    import { connectedRouterRedirect } from 'redux-auth-wrapper/history4/redirect'
    
    const userIsAuthenticated = connectedRouterRedirect({
      redirectPath: '/login',
      authenticatedSelector: state => state.user !== null,
      wrapperDisplayName: 'UserIsAuthenticated'
    })
    
  3. Apply the wrapper to your component or route:

    const ProtectedComponent = userIsAuthenticated(MyComponent)
    
    <Route path="/protected" component={ProtectedComponent} />
    

Competitor Comparisons

Declarative routing for React

Pros of react-router

  • More comprehensive routing solution for React applications
  • Active development and large community support
  • Seamless integration with React components and hooks

Cons of react-router

  • Steeper learning curve for complex routing scenarios
  • May require additional setup for authentication handling

Code Comparison

react-router:

import { BrowserRouter, Route, Switch } from 'react-router-dom';

<BrowserRouter>
  <Switch>
    <Route path="/home" component={Home} />
    <Route path="/login" component={Login} />
    <PrivateRoute path="/dashboard" component={Dashboard} />
  </Switch>
</BrowserRouter>

redux-auth-wrapper:

import { UserAuthWrapper } from 'redux-auth-wrapper';

const UserIsAuthenticated = UserAuthWrapper({
  authSelector: state => state.user,
  redirectAction: routerActions.replace,
  wrapperDisplayName: 'UserIsAuthenticated'
});

<Route path="/dashboard" component={UserIsAuthenticated(Dashboard)} />

Summary

react-router offers a more comprehensive routing solution with active development and community support. It integrates seamlessly with React components and hooks. However, it may have a steeper learning curve for complex scenarios and require additional setup for authentication.

redux-auth-wrapper focuses specifically on authentication wrapping for routes, providing a simpler solution for this specific use case. It integrates well with Redux but may have limitations in terms of broader routing capabilities compared to react-router.

The choice between the two depends on the project's specific needs, with react-router being more suitable for complex routing requirements and redux-auth-wrapper for simpler authentication-focused scenarios.

The official, opinionated, batteries-included toolset for efficient Redux development

Pros of Redux Toolkit

  • Provides a comprehensive set of tools for efficient Redux development
  • Simplifies common Redux use cases with utilities like createSlice and createAsyncThunk
  • Includes built-in support for immutable state updates and Redux DevTools

Cons of Redux Toolkit

  • More opinionated approach, which may not suit all project structures
  • Steeper learning curve for developers new to Redux concepts
  • Larger bundle size compared to minimal Redux setups

Code Comparison

Redux Toolkit:

import { createSlice } from '@reduxjs/toolkit'

const authSlice = createSlice({
  name: 'auth',
  initialState: { isAuthenticated: false },
  reducers: {
    login: (state) => { state.isAuthenticated = true },
    logout: (state) => { state.isAuthenticated = false }
  }
})

Redux Auth Wrapper:

import { connectedRouterRedirect } from 'redux-auth-wrapper/history4/redirect'

const userIsAuthenticated = connectedRouterRedirect({
  redirectPath: '/login',
  authenticatedSelector: state => state.user.isAuthenticated,
  wrapperDisplayName: 'UserIsAuthenticated'
})

Summary

Redux Toolkit offers a more comprehensive solution for Redux state management, while Redux Auth Wrapper focuses specifically on authentication-related routing. Redux Toolkit provides a wider range of utilities and simplifies common Redux patterns, but may introduce more complexity for simple use cases. Redux Auth Wrapper offers a more targeted approach for handling authentication in Redux applications, with a focus on route protection.

WAI-ARIA compliant React autosuggest component

Pros of react-autosuggest

  • Focused on providing autocomplete functionality, making it more specialized and potentially easier to implement for this specific use case
  • Offers a high degree of customization for suggestion rendering and filtering
  • Actively maintained with regular updates and a large community

Cons of react-autosuggest

  • Limited to autocomplete functionality, whereas redux-auth-wrapper provides authentication solutions
  • May require additional setup and configuration for complex use cases
  • Potential performance issues with large datasets if not optimized properly

Code Comparison

react-autosuggest:

import Autosuggest from 'react-autosuggest';

const renderSuggestion = suggestion => (
  <div>{suggestion.name}</div>
);

<Autosuggest
  suggestions={suggestions}
  onSuggestionsFetchRequested={this.onSuggestionsFetchRequested}
  onSuggestionsClearRequested={this.onSuggestionsClearRequested}
  getSuggestionValue={getSuggestionValue}
  renderSuggestion={renderSuggestion}
  inputProps={inputProps}
/>

redux-auth-wrapper:

import { connectedRouterRedirect } from 'redux-auth-wrapper/history4/redirect'

const userIsAuthenticated = connectedRouterRedirect({
  redirectPath: '/login',
  authenticatedSelector: state => state.user !== null,
  wrapperDisplayName: 'UserIsAuthenticated'
})

export default userIsAuthenticated(ProtectedComponent)
33,863

Build forms in React, without the tears 😭

Pros of Formik

  • Simplifies form handling and validation in React applications
  • Provides a more declarative approach to form management
  • Offers built-in support for common form patterns and best practices

Cons of Formik

  • Focuses solely on form management, unlike redux-auth-wrapper's authentication focus
  • May require additional libraries for complex state management scenarios
  • Learning curve for developers new to its concepts and API

Code Comparison

redux-auth-wrapper:

import { connectedRouterRedirect } from 'redux-auth-wrapper/history4/redirect'

const userIsAuthenticated = connectedRouterRedirect({
  redirectPath: '/login',
  authenticatedSelector: state => state.user !== null,
  wrapperDisplayName: 'UserIsAuthenticated'
})

Formik:

import { Formik, Form, Field } from 'formik'

<Formik
  initialValues={{ email: '', password: '' }}
  onSubmit={(values, { setSubmitting }) => {
    // Handle form submission
  }}
>
  {({ isSubmitting }) => (
    <Form>
      <Field type="email" name="email" />
      <Field type="password" name="password" />
      <button type="submit" disabled={isSubmitting}>Submit</button>
    </Form>
  )}
</Formik>

These repositories serve different purposes: redux-auth-wrapper focuses on authentication and authorization in Redux applications, while Formik specializes in form management and validation in React. The code examples highlight their distinct use cases and implementation approaches.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

redux-auth-wrapper

npm npm dm Build Status Coverage Status Join the chat at https://gitter.im/mjrussell/redux-auth-wrapper

Decouple your Authentication and Authorization from your components!

npm install --save redux-auth-wrapper

redux-auth-wrapper is a utility library for handling authentication and authorization in react + redux applications.

Read the documentation at https://mjrussell.github.io/redux-auth-wrapper

Version 3

Version 3.x has the same external API as version 2, however it only supports React >= 16.3. It is also tested with react-router v5 and connected-react-router which replaced react-router-redux.

Version 2

Version 2.x is a big internal rewrite! It provides a massive increase in flexibility when using redux-auth-wrapper and also introduces some breaking changes. See the Migration Guide for more details if coming from 1.x. Or check out the Getting Started guide if you've never used redux-auth-wrapper before.

Looking for Version 1.x? You can browse the 1.x README here.

Submitting Issues

Having trouble? First check out the Troubleshooting section of the documentation, and then search the issues, both open and closed for your problem. If you are still having trouble or have a question on using redux-auth-wrapper, please open an issue! You can also ask on the gitter channel.

Examples

Other examples not yet updated to v2:

NPM DownloadsLast 30 Days