Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Store, transmit, refresh JWT authentication tokens for axios
Applies a request interceptor to your axios instance.
The interceptor automatically adds an access token header (default: Authorization
) to all requests.
It stores access_token
and refresh_token
in localStorage
and reads them when needed.
It parses the expiration time of your access token and checks to see if it is expired before every request. If it is expired, a request to refresh and store a new access token is automatically performed before the request proceeds.
setAuthTokens()
import {
IAuthTokens,
TokenRefreshRequest,
useAuthTokenInterceptor
} from "axios-jwt";
import axios from "axios";
// your axios instance that you wish to apply the interceptor to
import apiClient from "../apiClient";
const BASE_URL = process.env.REACT_APP_BASE_URL;
if (!BASE_URL) throw new Error("BASE_URL is not defined");
// type of response from login endpoint
export interface IAuthResponse {
access_token: string;
refresh_token: string;
}
// refresh token endpoint
const refreshEndpoint = `${BASE_URL}/auth/refresh_token`;
// transform response into IAuthTokens
// this assumes your auth endpoint returns `{"access_token": ..., "refresh_token": ...}`
export const authResponseToAuthTokens = (res: IAuthResponse): IAuthTokens => ({
accessToken: res.access_token,
refreshToken: res.refresh_token
});
// define token refresh function
const requestRefresh: TokenRefreshRequest = async (
refreshToken: string
): Promise<string> => {
// perform refresh
return (await axios.post(refreshEndpoint, { token: refreshToken })).data
.access_token;
};
// add interceptor to your axios instance
useAuthTokenInterceptor(apiClient, { requestRefresh });
import { isLoggedIn, setAuthTokens, clearAuthTokens } from "axios-jwt";
// login
const login = async (params: ILoginRequest) => {
const res: IAuthResponse = (await axios.post("/auth/login", params)).data;
// save tokens to storage
setAuthTokens(authResponseToAuthTokens(res));
};
// to reset auth tokens
const logout = () => clearAuthTokens();
// check if refresh token exists
if (isLoggedIn()) {
// assume we are logged in because we have a refresh token
}
{
requestRefresh, // async function that takes refreshToken and returns a promise for a fresh accessToken
header = "Authorization", // header name
headerPrefix = "Bearer ", // header value prefix
}
FAQs
Axios interceptor to store, use, and refresh tokens for authentication.
The npm package axios-jwt receives a total of 1,805 weekly downloads. As such, axios-jwt popularity was classified as popular.
We found that axios-jwt demonstrated a healthy version release cadence and project activity because the last version was released less than 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.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.