@xterr/react-authentication
v1.8.7
Published
React Authentication based on simple JWT or OAuth 2.0 flow
Maintainers
Readme
React Authentication
Universal Authentication with support for simple JWT and OAuth 2.0 authentication
Installation
npm install --save @xterr/react-authenticationor:
yarn add @xterr/react-authenticationGetting Started
Configure the library wrapping your application in AuthProvider:
import React from 'react';
import { AuthProvider } from '@xterr/react-authentication';
import App from './App';
const App = () => (
<AuthProvider>
<App/>
</AuthProvider>
)Protect a route
Example of a component that can protect other components and redirect the user to a login page if the user is not authenticated:
import React from 'react';
import { useAuth } from '@xterr/react-authentication';
import { Navigate } from 'react-router';
const ProtectedRoute = ({redirectTo, children}) => {
const {authState: {isAuthenticated}} = useAuth();
return (isAuthenticated ? React.Children.only(children) : <Navigate to={redirectTo} replace/>);
};