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-provider

v1.0.5

Published

Simple Authorization Context Provider for React Router with Automatic Redirection

Downloads

6

Readme

react-router-auth-provider

npm version

A simple authorization context provider for react-router with redirection.

Install

npm install --save react-router-auth-provider

or

yarn add react-router-auth-provider

Usage

react-router-auth-provider consists of <AuthProvider/>, <AuthRoute/> and HOC withAuth. Easiest way to see how it works together is to head over to example

API

AuthProvider

AuthProvider is a React.Component whose purpose is to provide all the necessary authorization information and function. The props are

  • whoami: ()=>AxiosPromise(WhoAmIResponse) a function to GET whoami information. Only run once on component did mount to check if the cookies associated with this session is already logged in for not. The backend should response with 200 on success and 401 on not authorized. The json body is then passed through getAuthInfo before saving it to the context. Simplest way to do this is to just do () => axios.get('/api/whoami').
  • getAuthInfo: WhoAmIResponse=>AuthInfo a function to convert WhoAmIResponse to AuthInfo. This is optional. The default value is identity function which means whatever whoami returns will be saved to authInfo context.
  • logout: ()=>AxiosPromise<any> a function to call logout. Simplest way to do this is to do axios.get('/api/logout')
  • loadingComponent: ComponentType<any> a react component to show when the it is waiting for whoami to respond. Default is Blank.
  • redirectOnLogout: string|null a path to redirect to after logout. If null it stays on the same route. which typically if it's inside AuthRoute it will redirect to login.

AuthRoute

AuthRoute is a wrapper around react-router's Route which shows the component if user is autorized. Otherwise it will redirect the user to loginRoute. The props are

  • path:string route's path
  • component component to show if authorized.
  • loginRoute route to redirect to when user is not autorized.
  • roleCheck: (AuthInfo) => bool a function to check role. Default is ()=>true. Which means it only require login but does not perform role check.
  • permissionDeniedComponent the component to show if user is logged in but fail on roleCheck.

withAuth

A higher order component to provide grab the context from AuthProvider. withAuth provides authorization the following props from the AuthProvider.

  • isLoggedIn: bool boolean indicating if user is logged in.
  • authInfo: object authorization info object. This could contains stuff like username, roles etc.
  • onLoginSuccess: (authInfo: AuthInfo, cb?: () => any) => void a function to call when loggin is successful to trigger update of loggedIn and authInfo with optional callback.
  • onLogout: (cb?: (cb?: () => any) => any) => any a function to called with optional callback when logout to trigger update of loggedIn and authInfo.