Security News
Opengrep Emerges as Open Source Alternative Amid Semgrep Licensing Controversy
Opengrep forks Semgrep to preserve open source SAST in response to controversial licensing changes.
jwt-auth-react
Advanced tools
Authenticate users in React app with JWT based authentication
React Context and Hooks
based Authentication
Package for React Apps
npm install --save jwt-auth-react
AuthProvider
relies on the context feature of React to pass the Auth
down
to the components, so you need to make sure that AuthProvider
is a parent of the Routing components
.
You can learn more about this in the API section.
import {AuthProvider} from "jwt-auth-react";
...
<AuthProvider>
<RouteComponent />
</AuthProvider>
PrivateRoute
relies on react-router-dom same as the
Route
component of React Router.
It creates a Route to an Authentication
based component.
If the user is not authenticated, it will redirect to login
Page.
You can learn more about this in the API section.
import {BrowserRouter, Route} from "react-router-dom";
import {PrivateRoute} from "jwt-auth-react";
...
<BrowserRouter>
<Route component={LoginComponent} path={LOGIN_URL} exact/>
...
<PrivateRoute component={DashboardComponent} path={DASHBOARD_URL} loginPath={LOGIN_URL}/>
</BrowserRouter>
loginAuth
is a function api, relies on React Hooks.
It logs in the user and stores the JWT token
and expiresIn
time in minutes
.
Impliment the loginAuth
function on login pipeline i.e in login api response
.
You can learn more about this in the API section.
Example with fetch
:
import {loginAuth} from "jwt-auth-react";
const do_login = async () => {
const res = await fetch("https://api.abc.xyz/login");
if (res.status === 200){
const res_json = res.json()
const jit_token = res_json.jit;
const expiresIn = res_json.expiresIn;
loginAuth(token = jit_token, expiresIn = expiresIn);
}
}
Example with axios
:
import axios from 'axios'
import {loginAuth} from "jwt-auth-react";
const do_login = async () => {
const res = await axios.post("https://api.abc.xyz/login");
if (res.status === 200){
const res_json = res.data;
const jit_token = res_json.jit;
const expiresIn = res_json.expiresIn;
loginAuth(token = jit_token, expiresIn = expiresIn);
}
}
logoutAuth
is a function api, relies on React Hooks.
It logouts the current user and clear all token.
Impliment the logoutAuth
function on logout pipeline ex. on Logout Button Click.
You can learn more about this in the API section.
import react from 'react';
import {logoutAuth} from "jwt-auth-react";
const logoutComponent = () => {
const logoutPipeline = () => {
logoutAuth()
}
return <button onClick={logoutPipeline}>Logout</button>
}
logoutAuth
is a function api. It produces the authentication header
string for logged in user.
It returns Bearer: xxxxxx
string
Example with fetch
:
import {authHeader} from "jwt-auth-react";
const do_something = async () => {
const myInit = {
method: 'GET',
headers: {
'Authentication': authHeader()
}
}
const res = await fetch("https://api.abc.xyz/something", myInit);
if (res.status === 200){
const res_json = res.json()
...
}
}
Example with axios
:
import axios from "axios";
import {authHeader} from "jwt-auth-react";
const do_something = async () => {
const res = await axios.get("https://api.abc.xyz/something",
headers: {
'Authentication': authHeader()
});
if (res.status === 200){
const res_json = res.json()
...
}
}
Need help? Feel free to contact me @ in2arkadipb13@gmail.com
Apache-2.0 © darkmatter18
FAQs
Authenticate users in React app with JWT based authentication
The npm package jwt-auth-react receives a total of 15 weekly downloads. As such, jwt-auth-react popularity was classified as not popular.
We found that jwt-auth-react demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Opengrep forks Semgrep to preserve open source SAST in response to controversial licensing changes.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.