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, clear, transmit and automatically refresh JWT authentication tokens.
Applies a request interceptor to your axios instance.
The interceptor automatically adds an access token header (default: Authorization
) to all requests.
It stores accessToken
and refreshToken
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 has expired, a request to refresh and store a new access token is automatically performed before the request proceeds.
setAuthTokens()
clearAuthTokens()
// api.ts
import { IAuthTokens, TokenRefreshRequest, applyAuthTokenInterceptor } from 'axios-jwt'
import axios from 'axios'
const BASE_URL = 'https://api.example.com'
// 1. Create an axios instance that you wish to apply the interceptor to
export const axiosInstance = axios.create({ baseURL: BASE_URL })
// 2. Define token refresh function.
const requestRefresh: TokenRefreshRequest = async (refreshToken: string): Promise<string> => {
// Important! Do NOT use the axios instance that you supplied to applyAuthTokenInterceptor (in our case 'axiosInstance')
// because this will result in an infinite loop when trying to refresh the token.
// Use the global axios client or a different instance
const response = await axios.post(`${BASE_URL}/auth/refresh_token`, { token: refreshToken })
return response.data.access_token
}
// 3. Add interceptor to your axios instance
applyAuthTokenInterceptor(axiosInstance, { requestRefresh })
// login.ts
import { isLoggedIn, setAuthTokens, clearAuthTokens, getAccessToken, getRefreshToken } from 'axios-jwt'
import { axiosInstance } from './api'
// 4. Post email and password and get tokens in return. Call setAuthTokens with the result.
const login = async (params: ILoginRequest) => {
const response = await axiosInstance.post('/auth/login', params)
// save tokens to storage
setAuthTokens({
accessToken: response.data.access_token,
refreshToken: response.data.refresh_token
})
}
// 5. Clear the auth tokens from localstorage
const logout = () => clearAuthTokens()
// Check if refresh token exists
if (isLoggedIn()) {
// assume we are logged in because we have a refresh token
}
// Get access to tokens
const accessToken = getAccessToken()
const refreshToken = getRefreshToken()
applyAuthTokenInterceptor(axiosInstance, {
requestRefresh, // async function that takes a refreshToken and returns a promise the resolves in a fresh accessToken
header = "Authorization", // header name
headerPrefix = "Bearer ", // header value prefix
})
import { applyAuthTokenInterceptor } from 'axios-jwt';
import axios from 'axios';
const BASE_URL = 'https://api.example.com'
// 1. Create an axios instance that you wish to apply the interceptor to
const axiosInstance = axios.create({ baseURL: BASE_URL })
// 2. Define token refresh function.
const requestRefresh = (refresh) => {
// Notice that this is the global axios instance, not the axiosInstance! <-- important
return axios.post(`${BASE_URL}/auth/refresh_token`, { refresh })
.then(response => resolve(response.data.access_token))
};
// 3. Apply interceptor
applyAuthTokenInterceptor(axiosInstance, { requestRefresh }); // Notice that this uses the axiosInstance instance. <-- important
// 4. Logging in
const login = async (params) => {
const response = await axiosInstance.post('/auth/login', params)
// save tokens to storage
setAuthTokens({
accessToken: response.data.access_token,
refreshToken: response.data.refresh_token
})
}
// 5. Logging out
const logout = () => clearAuthTokens()
// Now just make all requests using your axiosInstance instance
axiosInstance.get('/api/endpoint/that/requires/login').then(response => { })
FAQs
Axios interceptor to store, use, and refresh tokens for authentication.
The npm package axios-jwt receives a total of 1,225 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.