Comparing version 1.0.2 to 1.0.3
@@ -7,9 +7,9 @@ import { AxiosInstance } from "axios"; | ||
} | ||
export declare const getTokenStorageKey: () => string; | ||
export declare const isLoggedIn: () => boolean; | ||
export declare const setAuthTokens: (tokens: IAuthTokens) => void; | ||
export declare const setAccessToken: (token: string) => void; | ||
export declare const clearAuthTokens: () => void; | ||
export declare const getRefreshToken: () => string | undefined; | ||
export declare const getAccessToken: () => string | undefined; | ||
export declare type TokenRefreshRequest = (refreshToken: string) => Promise<IAuthTokens>; | ||
export declare type TokenRefreshRequest = (refreshToken: string) => Promise<Token>; | ||
export interface IAuthTokenInterceptorConfig { | ||
@@ -16,0 +16,0 @@ header?: string; |
@@ -49,5 +49,3 @@ "use strict"; | ||
var EXPIRE_FUDGE = 10; | ||
exports.getTokenStorageKey = function () { | ||
return "auth-tokens-" + process.env.NODE_ENV; | ||
}; | ||
// EXPORTS | ||
exports.isLoggedIn = function () { | ||
@@ -58,9 +56,20 @@ var token = exports.getRefreshToken(); | ||
exports.setAuthTokens = function (tokens) { | ||
return localStorage.setItem(exports.getTokenStorageKey(), JSON.stringify(tokens)); | ||
return localStorage.setItem(getTokenStorageKey(), JSON.stringify(tokens)); | ||
}; | ||
exports.setAccessToken = function (token) { | ||
var tokens = getAuthTokens(); | ||
if (!tokens) { | ||
console.warn("Trying to set new access token but no auth tokens found in storage. This should not happen."); | ||
return; | ||
} | ||
tokens.accessToken = token; | ||
exports.setAuthTokens(tokens); | ||
}; | ||
exports.clearAuthTokens = function () { | ||
return localStorage.removeItem(exports.getTokenStorageKey()); | ||
return localStorage.removeItem(getTokenStorageKey()); | ||
}; | ||
// PRIVATE | ||
var getTokenStorageKey = function () { return "auth-tokens-" + process.env.NODE_ENV; }; | ||
var getAuthTokens = function () { | ||
var tokensRaw = localStorage.getItem(exports.getTokenStorageKey()); | ||
var tokensRaw = localStorage.getItem(getTokenStorageKey()); | ||
if (!tokensRaw) | ||
@@ -119,3 +128,3 @@ return; | ||
// save tokens | ||
exports.setAuthTokens(res); | ||
exports.setAccessToken(res); | ||
return [2 /*return*/, res]; | ||
@@ -127,3 +136,3 @@ case 3: | ||
// got invalid token response for sure, remove saved tokens because they're invalid | ||
localStorage.removeItem(exports.getTokenStorageKey()); | ||
localStorage.removeItem(getTokenStorageKey()); | ||
return [2 /*return*/, Promise.reject("Got 401 on token refresh; Resetting auth token: " + err_1)]; | ||
@@ -143,3 +152,3 @@ } | ||
return function (requestConfig) { return __awaiter(_this, void 0, void 0, function () { | ||
var accessToken, newTokens, err_2; | ||
var accessToken, err_2; | ||
return __generator(this, function (_a) { | ||
@@ -158,6 +167,3 @@ switch (_a.label) { | ||
case 2: | ||
newTokens = _a.sent(); | ||
// refresh ok. proceed | ||
if (newTokens) | ||
accessToken = newTokens.accessToken; | ||
accessToken = _a.sent(); | ||
return [3 /*break*/, 4]; | ||
@@ -164,0 +170,0 @@ case 3: |
{ | ||
"name": "axios-jwt", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "Axios interceptor to store, use, and refresh tokens for authentication.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -24,3 +24,2 @@ # axios-jwt | ||
```typescript | ||
// | ||
import { | ||
@@ -38,3 +37,3 @@ IAuthTokens, | ||
// type of response from refresh token endpoint | ||
// type of response from login endpoint | ||
export interface IAuthResponse { | ||
@@ -49,3 +48,3 @@ access_token: string; | ||
// transform response into IAuthTokens | ||
// this assumes your auth/refresh endpoints return `{"access_token": ..., "refresh_token": ...}` | ||
// this assumes your auth endpoint returns `{"access_token": ..., "refresh_token": ...}` | ||
export const authResponseToAuthTokens = (res: IAuthResponse): IAuthTokens => ({ | ||
@@ -59,8 +58,6 @@ accessToken: res.access_token, | ||
refreshToken: string | ||
): Promise<IAuthTokens> => { | ||
): Promise<string> => { | ||
// perform refresh | ||
const res: IAuthResponse = (await axios.post(refreshEndpoint, { | ||
token: refreshToken | ||
})).data; | ||
return authResponseToAuthTokens(res); | ||
return (await axios.post(refreshEndpoint, { token: refreshToken })).data | ||
.access_token; | ||
}; | ||
@@ -97,3 +94,3 @@ | ||
{ | ||
requestRefresh, // async function that takes refresh token and returns {accessToken, refreshToken} | ||
requestRefresh, // async function that takes refreshToken and returns a promise for a fresh accessToken | ||
header = "Authorization", // header name | ||
@@ -100,0 +97,0 @@ headerPrefix = "Bearer ", // header value prefix |
import * as jwt from "jsonwebtoken"; | ||
import { AxiosInstance, default as axiosRaw, AxiosRequestConfig } from "axios"; | ||
import { AxiosInstance, AxiosRequestConfig } from "axios"; | ||
@@ -13,5 +13,3 @@ // a little time before expiration to try refresh (seconds) | ||
export const getTokenStorageKey = (): string => | ||
`auth-tokens-${process.env.NODE_ENV}`; | ||
// EXPORTS | ||
export const isLoggedIn = (): boolean => { | ||
@@ -25,5 +23,21 @@ const token = getRefreshToken(); | ||
export const setAccessToken = (token: Token) => { | ||
const tokens = getAuthTokens(); | ||
if (!tokens) { | ||
console.warn( | ||
"Trying to set new access token but no auth tokens found in storage. This should not happen." | ||
); | ||
return; | ||
} | ||
tokens.accessToken = token; | ||
setAuthTokens(tokens); | ||
}; | ||
export const clearAuthTokens = () => | ||
localStorage.removeItem(getTokenStorageKey()); | ||
// PRIVATE | ||
const getTokenStorageKey = (): string => `auth-tokens-${process.env.NODE_ENV}`; | ||
const getAuthTokens = (): IAuthTokens | undefined => { | ||
@@ -71,3 +85,3 @@ const tokensRaw = localStorage.getItem(getTokenStorageKey()); | ||
requestRefresh: TokenRefreshRequest | ||
): Promise<IAuthTokens> => { | ||
): Promise<Token> => { | ||
const refreshToken = getRefreshToken(); | ||
@@ -78,5 +92,5 @@ if (!refreshToken) return Promise.reject("No refresh token available"); | ||
// do refresh with default axios client (we don't want our interceptor applied for refresh) | ||
const res: IAuthTokens = await requestRefresh(refreshToken); | ||
const res = await requestRefresh(refreshToken); | ||
// save tokens | ||
setAuthTokens(res); | ||
setAccessToken(res); | ||
return res; | ||
@@ -98,5 +112,3 @@ } catch (err) { | ||
export type TokenRefreshRequest = ( | ||
refreshToken: string | ||
) => Promise<IAuthTokens>; | ||
export type TokenRefreshRequest = (refreshToken: string) => Promise<Token>; | ||
export interface IAuthTokenInterceptorConfig { | ||
@@ -125,5 +137,4 @@ header?: string; | ||
try { | ||
const newTokens = await refreshToken(requestRefresh); | ||
accessToken = await refreshToken(requestRefresh); | ||
// refresh ok. proceed | ||
if (newTokens) accessToken = newTokens.accessToken; | ||
} catch (err) { | ||
@@ -130,0 +141,0 @@ return Promise.reject( |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
26763
391
100