use-jwt-manager
Advanced tools
Comparing version 0.1.75 to 0.1.80
@@ -1,1 +0,1 @@ | ||
module.exports=(()=>{"use strict";var e={613:(e,t,r)=>{r.r(t),r.d(t,{default:()=>h});const a=require("react");var n=r.n(a);const o=require("lockr");var i=r.n(o);const c=require("axios");var s=r.n(c);const u=(e,t)=>{switch(t.type){case"LOGIN":return{...e,...t.payload,authenticated:!0,initialized:!0};case"LOGOUT":return{authenticated:!1,initialized:!0}}};class l extends Error{constructor(e,t){super(e),this.cause=t,this.name="JwtError"}}const d={initialized:!1,authenticated:!1},h=({refresh:e,me:t,login:r,config:o})=>{const[c,h]=n().useReducer(u,d),{TOKEN_KEY:y}=o,f=(0,a.useCallback)((()=>{const t=i().get(y);c.authenticated||!t?h({type:"LOGOUT"}):(s().defaults.headers.Authorization="Bearer "+t,e().then(p).catch((e=>{throw h({type:"LOGOUT"}),new l("An error occurred trying to refresh the token",e)})))}),[c.authenticated]);(0,a.useEffect)(f,[]);const p=(0,a.useCallback)((async({token:e})=>(i().set(y,e),s().defaults.headers.Authorization="Bearer "+e,await t().then((t=>(h({type:"LOGIN",payload:{user:t,token:e}}),t))).catch((e=>{throw new l("An error occurred retrieving the user information",e)})))),[]),O=(0,a.useCallback)((async e=>await r(e).then(p).then((e=>e)).catch((e=>{throw new l("An error occurred trying to log in",e)}))),[]),g=(0,a.useCallback)((async()=>(i().rm(y),s().defaults.headers.Authorization=null,h({type:"LOGOUT"}),await!0)),[]);return{...c,login:O,logout:g,refresh:f}}}},t={};function r(a){if(t[a])return t[a].exports;var n=t[a]={exports:{}};return e[a](n,n.exports,r),n.exports}return r.n=e=>{var t=e&&e.__esModule?()=>e.default:()=>e;return r.d(t,{a:t}),t},r.d=(e,t)=>{for(var a in t)r.o(t,a)&&!r.o(e,a)&&Object.defineProperty(e,a,{enumerable:!0,get:t[a]})},r.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},r(613)})(); | ||
module.exports=(()=>{"use strict";var e={613:(e,t,r)=>{r.r(t),r.d(t,{default:()=>h});const a=require("react");var n=r.n(a);const o=require("lockr");var s=r.n(o);const c=require("axios");var i=r.n(c);const u=(e,t)=>{switch(t.type){case"LOGIN":return{...e,...t.payload,authenticated:!0,initialized:!0};case"LOGOUT":return{authenticated:!1,initialized:!0}}};class l extends Error{constructor(e,t){super(e),this.cause=t,this.name="JwtError"}}const d={initialized:!1,authenticated:!1},h=({refresh:e,me:t,login:r,config:o})=>{const[c,h]=n().useReducer(u,d),{TOKEN_KEY:f,REFRESH_TOKEN_KEY:y}=o,p=(0,a.useCallback)((()=>{const t=s().get(y);c.authenticated||!t?h({type:"LOGOUT"}):(i().defaults.headers.Authorization="Bearer "+t,e().then(O).catch((e=>{throw h({type:"LOGOUT"}),new l("An error occurred trying to refresh the token",e)})))}),[c.authenticated]);(0,a.useEffect)(p,[]);const O=(0,a.useCallback)((async({access_token:e,refresh_token:r})=>{let a=r;return s().set(f,e),a?s().set(y,a):a=s().get(y),i().defaults.headers.Authorization="Bearer "+e,await t().then((t=>(h({type:"LOGIN",payload:{user:t,access_token:e,refresh_token:a}}),t))).catch((e=>{throw new l("An error occurred retrieving the user information",e)}))}),[]),g=(0,a.useCallback)((async e=>await r(e).then(O).then((e=>e)).catch((e=>{throw new l("An error occurred trying to log in",e)}))),[]),b=(0,a.useCallback)((async()=>(s().rm(f),s().rm(y),i().defaults.headers.Authorization=null,h({type:"LOGOUT"}),await!0)),[]);return{...c,login:g,logout:b,refresh:p}}}},t={};function r(a){if(t[a])return t[a].exports;var n=t[a]={exports:{}};return e[a](n,n.exports,r),n.exports}return r.n=e=>{var t=e&&e.__esModule?()=>e.default:()=>e;return r.d(t,{a:t}),t},r.d=(e,t)=>{for(var a in t)r.o(t,a)&&!r.o(e,a)&&Object.defineProperty(e,a,{enumerable:!0,get:t[a]})},r.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},r(613)})(); |
@@ -7,3 +7,3 @@ { | ||
}, | ||
"version": "0.1.75", | ||
"version": "0.1.81", | ||
"description": "hook for jwt auth", | ||
@@ -10,0 +10,0 @@ "repository": { |
@@ -10,3 +10,4 @@ interface UserDetails { | ||
user?: UserDetails; | ||
token?: string; | ||
access_token?: string; | ||
refresh_token?: string; | ||
initialized?: boolean; | ||
@@ -18,3 +19,4 @@ authenticated?: boolean; | ||
interface TokenParams { | ||
token: string; | ||
access_token: string; | ||
refresh_token?: string; | ||
[x: string]: any; | ||
@@ -27,3 +29,3 @@ } | ||
login: (user: UserDetails) => Promise<TokenParams>; | ||
config: { TOKEN_KEY: string }; | ||
config: { TOKEN_KEY: string; REFRESH_TOKEN_KEY: string }; | ||
} | ||
@@ -34,3 +36,3 @@ | ||
logout: () => Promise<boolean>; | ||
refresh: () => void; | ||
refresh: () => void | ||
} | ||
@@ -37,0 +39,0 @@ |
import { UserDetails, UserContextReducerParams } from './constants/types'; | ||
type Actions = { type: 'LOGIN'; payload: { user: UserDetails; token: string } | {} } | { type: 'LOGOUT' }; | ||
type Actions = | ||
| { type: 'LOGIN'; payload: { user: UserDetails; access_token: string; refresh_token: string } | {} } | ||
| { type: 'LOGOUT' }; | ||
@@ -5,0 +7,0 @@ const userContextReducer = (state: UserContextReducerParams, action: Actions): UserContextReducerParams => { |
@@ -8,6 +8,9 @@ import Lockr from 'lockr'; | ||
const TOKEN_KEY = 'BANANA'; | ||
const REFRESH_TOKEN_KEY = 'BANANA_ROTATE'; | ||
function refresh(forceFail = false) { | ||
return new Promise<TokenParams>((resolve, reject) => { | ||
forceFail ? reject({ code: 'B4N4N41', description: 'refresh stuff' }) : resolve({ token: 'new_at1' }); | ||
forceFail | ||
? reject({ code: 'B4N4N41', description: 'refresh stuff' }) | ||
: resolve({ access_token: 'new_at1', refresh_token: 'new_rt1' }); | ||
}); | ||
@@ -25,9 +28,9 @@ } | ||
if (user.username == 'finto') { | ||
resolve({ token: 'new_at2' }); | ||
resolve({ access_token: 'new_at2', refresh_token: 'new_rt2' }); | ||
} | ||
reject({ code: 'B4N4N43' }); | ||
reject({ code: 'B4N4N43', description: 'login stuff' }); | ||
}); | ||
} | ||
const config = { TOKEN_KEY }; | ||
const config = { TOKEN_KEY, REFRESH_TOKEN_KEY }; | ||
@@ -42,8 +45,10 @@ test('not authenticated if no jwt present in ls at start', () => { | ||
expect(Lockr.get(TOKEN_KEY)).toBe(undefined); | ||
expect(Lockr.get(REFRESH_TOKEN_KEY)).toBe(undefined); | ||
}); | ||
test('authenticated if jw present at start', async () => { | ||
Lockr.set(TOKEN_KEY, 'old_token'); | ||
Lockr.rm(TOKEN_KEY); | ||
Lockr.rm(REFRESH_TOKEN_KEY); | ||
Lockr.set(REFRESH_TOKEN_KEY, 'old_rt'); | ||
const { result, waitForNextUpdate } = renderHook(() => useJWT({ login, me, refresh, config })); | ||
console.log(result.current); | ||
await waitForNextUpdate(); | ||
@@ -54,5 +59,7 @@ // assert initial state | ||
expect(result.current.user).toEqual(USER); | ||
expect(result.current.token).toEqual('new_at1'); | ||
expect(result.current.access_token).toEqual('new_at1'); | ||
expect(result.current.refresh_token).toEqual('new_rt1'); | ||
expect(Lockr.get(TOKEN_KEY)).toEqual('new_at1'); | ||
expect(Lockr.get(REFRESH_TOKEN_KEY)).toEqual('new_rt1'); | ||
}); | ||
@@ -62,2 +69,3 @@ | ||
Lockr.rm(TOKEN_KEY); | ||
Lockr.rm(REFRESH_TOKEN_KEY); | ||
const { result, waitForNextUpdate } = renderHook(() => useJWT({ login, me, refresh, config })); | ||
@@ -78,4 +86,6 @@ | ||
expect(result.current.user).toEqual(USER); | ||
expect(result.current.token).toEqual('new_at2'); | ||
expect(result.current.access_token).toEqual('new_at2'); | ||
expect(result.current.refresh_token).toEqual('new_rt2'); | ||
expect(Lockr.get(TOKEN_KEY)).toEqual('new_at2'); | ||
expect(Lockr.get(REFRESH_TOKEN_KEY)).toEqual('new_rt2'); | ||
@@ -90,4 +100,6 @@ await act(async () => { | ||
expect(result.current.user).toBe(undefined); | ||
expect(result.current.token).toBe(undefined); | ||
expect(result.current.access_token).toBe(undefined); | ||
expect(result.current.refresh_token).toBe(undefined); | ||
expect(Lockr.get(TOKEN_KEY)).toBe(undefined); | ||
expect(Lockr.get(REFRESH_TOKEN_KEY)).toBe(undefined); | ||
}); | ||
@@ -97,2 +109,3 @@ | ||
Lockr.rm(TOKEN_KEY); | ||
Lockr.rm(REFRESH_TOKEN_KEY); | ||
const { result, waitForNextUpdate } = renderHook(() => useJWT({ login, me, refresh, config })); | ||
@@ -115,5 +128,7 @@ | ||
expect(result.current.user).toBe(undefined); | ||
expect(result.current.token).toBe(undefined); | ||
expect(result.current.access_token).toBe(undefined); | ||
expect(result.current.refresh_token).toBe(undefined); | ||
expect(Lockr.get(TOKEN_KEY)).toBe(undefined); | ||
expect(Lockr.get(REFRESH_TOKEN_KEY)).toBe(undefined); | ||
}); |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
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
407371
10204
12
2