@xterr/react-authentication
v1.8.7
Published
React Authentication based on simple JWT or OAuth 2.0 flow
Downloads
3
Maintainers
Readme
React Authentication
Universal Authentication with support for simple JWT and OAuth 2.0 authentication
Installation
npm install --save @xterr/react-authentication
or:
yarn add @xterr/react-authentication
Getting 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/>);
};