• Stars
    star
    130
  • Rank 276,868 (Top 6 %)
  • Language
    TypeScript
  • Created over 5 years ago
  • Updated almost 3 years ago

Reviews

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

Repository Details

Guard middleware for React Router navigation

Logo

react-router-guards

Guard middleware for React Router navigation

React Router Guards provides a middleware API for React Router, allowing you to perform complex logic between the call for navigation and the final render of a route.

Table of Contents

Requirements

This package has the following peer dependencies:

Installation

With npm:

$ npm install react-router-guards

With yarn:

$ yarn add react-router-guards

Then with a module bundler like webpack, use as you would anything else:

// using ES6 modules
import { GuardProvider, GuardedRoute } from 'react-router-guards';

// using CommonJS modules
const GuardProvider = require('react-router-guards').GuardProvider;
const GuardedRoute = require('react-router-guards').GuardedRoute;

Basic usage

Here is a very basic example of how to use React Router Guards.

import React from 'react';
import { BrowserRouter } from 'react-router-dom';
import { GuardProvider, GuardedRoute } from 'react-router-guards';
import { About, Home, Loading, Login, NotFound } from 'pages';
import { getIsLoggedIn } from 'utils';

const requireLogin = (to, from, next) => {
  if (to.meta.auth) {
    if (getIsLoggedIn()) {
      next();
    }
    next.redirect('/login');
  } else {
    next();
  }
};

const App = () => (
  <BrowserRouter>
    <GuardProvider guards={[requireLogin]} loading={Loading} error={NotFound}>
      <Switch>
        <GuardedRoute path="/login" exact component={Login} />
        <GuardedRoute path="/" exact component={Home} meta={{ auth: true }} />
        <GuardedRoute path="/about" exact component={About} meta={{ auth: true }} />
        <GuardedRoute path="*" component={NotFound} />
      </Switch>
    </GuardProvider>
  </BrowserRouter>
);

Check out our demos for more examples!

Concepts

Navigation lifecycle

With the addition of guard middleware, the navigation lifecycle has changed.

Guard functions

Guard functions are the middleware between navigation and rendering.

Page components

Page components are used for setting loading and error pages.

Guard Provider

The GuardProvider component is a high-level wrapper for your entire routing solution.

Guarded Route

The GuardedRoute component acts as a replacement for the default Route component provided by React Router, allowing for routes to use guard middleware.

Demos

We've included some demos below to help provide more context on how to use this package!

Basic

Demo + Source

The basic demo showcases some basic functionality of route guard API with an auth example.

Intermediate

Demo | Source

The intermediate demo uses the PokรฉAPI to showcase how to use route guards for fetching data from an API.

Contributing

We welcome all contributions to our projects! Filing bugs, feature requests, code changes, docs changes, or anything else you'd like to contribute are all more than welcome! More information about contributing can be found in the contributing guidelines.

Code of Conduct

Upstatement strives to provide a welcoming, inclusive environment for all users. To hold ourselves accountable to that mission, we have a strictly-enforced code of conduct.

About Upstatement

Upstatement is a digital transformation studio headquartered in Boston, MA that imagines and builds exceptional digital experiences. Make sure to check out our services, work, and open positions!

More Repositories

1

routes

Simple routing for WordPress
PHP
188
star
2

jigsaw

Simple ways to make admin customizations for WordPress
PHP
156
star
3

jquery-total-storage

A jQuery plugin to manage local storage and cookies simultaneously in a simple interface
JavaScript
151
star
4

skela-wp-theme

An Upstatement-flavored starter theme for WordPress
PHP
63
star
5

puppy

Starter kit and delivery system for building static prototypes with Twig
HTML
48
star
6

stream-manager

Curate streams of WordPress posts.
PHP
39
star
7

zoneboard

A Simple JSON-based Dashboard Replacement
CSS
30
star
8

style-guide-twig

Style Guide Template for Twig/Timber
HTML
29
star
9

quickshare

Simple way of implementing social media share buttons
JavaScript
26
star
10

ups-dock

A front-end stack for Docker-based projects at Upstatement
HTML
23
star
11

mesh

Bootstrap content into a WordPress site
PHP
16
star
12

snd_layout_editor

Homepage layout editor demo created during SND Makes: Boston
CSS
12
star
13

mobile-preview

WordPress plugin to preview the current view on a mobile device
PHP
12
star
14

eslint-config

Upstatement's ESLint Config
JavaScript
12
star
15

upbase

โ›”๏ธ DEPRECATED
CSS
11
star
16

ups-public-library

A knowledge base and learning tool for web designers who want to write more code.
SCSS
8
star
17

prettier-config

Upstatement's Prettier Config
Shell
7
star
18

generator-style-guide-twig

Yeoman Generator to quickly scaffold the Upstatement Style Guide for Twig/Timber
CSS
5
star
19

react-hooks

Repository of reusable custom React hooks
TypeScript
5
star
20

puppy-lib

An adorable library for building a static site generator
JavaScript
4
star
21

craft-starter

An Upstatement-flavored starter project for Craft 3
PHP
4
star
22

ups-editorial-wp-plugin

A plugin for customizing the editor experience to suit Upstatement-built WordPress projects.
JavaScript
3
star
23

ups-mixin-lib

Bespoke Sass mixin library from Upstatement
CSS
3
star
24

mobile-stories-wp-theme

A WordPress theme for exploring mobile story telling
PHP
2
star
25

sndMakes4

This is the SND Makes repo for Team 4.
PHP
2
star
26

craft-netlify-trigger

A Craft CMS plugin to allow content editors to trigger static site builds on Netlify.
PHP
2
star
27

presentations

Presentations for Upstatement
JavaScript
1
star
28

boston-built

A repository for the born-again bostonbuilt.org (Puppy)
HTML
1
star
29

rails_firebase_auth_demo

Ruby
1
star