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

react-router-auth

v1.0.1

Published

A utility library for React Router v4 for managing authentication based routing

Downloads

1,219

Readme

A utility library for React Router v4 for managing authentication based routing

NPM Version Build Status Code Coverage

This library is based off of the code from the React Router v4 Docs. The purpose of this library is to make it easy to handle redirecting users for routes that require the user to either be authenticated or unauthenticated.

Install

npm install --save react-router-auth

OR

yarn add react-router-auth

Usage

AuthRoute

Use this component if you have a route that requires the user to be authenticated for them to be able to access it.

e.g. to access user profile page

import React, { Component } from 'react'
import { AuthRoute } from 'react-router-auth'
import UserProfile from './UserProfile'

class Example extends Component {
  render () {
    return (
      <AuthRoute path="/profile" component={UserProfile} redirectTo="/login" authenticated={this.props.authenticated} />
    )
  }
}

In this example, if the user is authenticated while they try to access the /profile route, then the UserProfile component will be rendered. If the user is not authenticated then it will redirect them to the /login route.

Props

| Name | Type | Description | |---------------|-----------------|--------------------------------------------------------| | authenticated | boolean | Whether the user is authenticated or not | | redirectTo | string | The route to redirect the user to if not authenticated | | component | React Component | The component that requires authentication |

UnauthRoute

Use this component if you have a route that a user should only be able to access if they aren't already authenticated.

e.g. to access the login / signup pages

import React, { Component } from 'react'
import { UnauthRoute } from 'react-router-auth'
import Login from './Login'

class Example extends Component {
  render () {
    return (
      <UnauthRoute path="/login" component={Login} redirectTo="/feed" authenticated={this.props.authenticated} />
    )
  }
}

In this example, if the user is authenticated while they try to access the login route, they will be redirected to the /feed route. If the user is not authenticated, then the Login component will be rendered.

Props

| Name | Type | Description | |---------------|-----------------|----------------------------------------------------| | authenticated | boolean | Whether the user is authenticated or not | | redirectTo | string | The route to redirect the user to if authenticated | | component | React Component | The component that requires authentication |

Usage with Redux

The easiest way to use these components with Redux is by creating your own components to wrap the components from this library with Redux's connect HOC and passing in authenticated as a prop.

Example:

// ConnectedAuthRoute.js
import { connect } from 'react-redux'
import { AuthRoute } from 'react-router-auth'

const mapStateToProps = state => ({
  // In this example the auth reducer has a key
  // called authenticated which determines if the
  // user is authenticated or not
  authenticated: state.auth.authenticated, 
})

export default connect(mapStateToProps)(AuthRoute)

Now if you want to use this in any of your components, you don't need to pass in the authenticated prop as the component is already hooked up to determine the authenticated state from the Redux store.

import React, { Component } from 'react'
import UserProfile from './UserProfile'
// Import our connected AuthRoute component
import ConnectedAuthRoute from './ConnectedAuthRoute'

class Example extends Component {
  render () {
    return (
      {/* we don't need to pass in the authenticated prop anymore */}
      <ConnectedAuthRoute path="/profile" component={UserProfile} redirectTo="/login" />
    )
  }
}

License

MIT © joelseq