use-jwt-manager
Advanced tools
Comparing version 0.1.62 to 0.1.63
@@ -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 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 new l("An error occurred trying to refresh the token",e)})))}),[c.authenticated]);(0,a.useEffect)(p,[]);const O=(0,a.useCallback)((async({data:{access_token:e,refresh_token:r}})=>(s().set(f,e),r&&s().set(y,r),i().defaults.headers.Authorization="Bearer "+e,await t().then((t=>(h({type:"LOGIN",payload:{user:t,access_token:e,refresh_token:r}}),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,refrshToken: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)})(); | ||
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 new l("An error occurred trying to refresh the token",e)})))}),[c.authenticated]);(0,a.useEffect)(p,[]);const O=(0,a.useCallback)((async({data:{access_token:e,refresh_token:r}})=>(s().set(f,e),r&&s().set(y,r),i().defaults.headers.Authorization="Bearer "+e,await t().then((({data:t})=>(h({type:"LOGIN",payload:{user:t,access_token:e,refresh_token:r}}),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,refrshToken: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.62", | ||
"version": "0.1.63", | ||
"description": "hook for jwt auth", | ||
@@ -10,0 +10,0 @@ "repository": { |
import Lockr from 'lockr'; | ||
import useJWT from './src'; | ||
import { renderHook, act } from '@testing-library/react-hooks'; | ||
import { TokenParams, UserDetails, JwtError } from './src/constants/types'; | ||
import { TokenParams, UserDetails, JwtError, ApiResponse } from './src/constants/types'; | ||
@@ -11,6 +11,6 @@ const USER = { id: 1, username: 'finto', password: 'finta', role: 'call center' }; | ||
function refresh(forceFail = false) { | ||
return new Promise<TokenParams>((resolve, reject) => { | ||
return new Promise<ApiResponse & { data: TokenParams }>((resolve, reject) => { | ||
forceFail | ||
? reject({ code: 'B4N4N41', description: 'refresh stuff' }) | ||
: resolve({ access_token: 'new_at1', refresh_token: 'new_rt1' }); | ||
: resolve({ message: '', status: true, data: { access_token: 'new_at1', refresh_token: 'new_rt1' } }); | ||
}); | ||
@@ -20,4 +20,4 @@ } | ||
function me(forceFail = false) { | ||
return new Promise((resolve, reject) => { | ||
forceFail ? reject({ code: 'B4N4N42', description: 'me stuff' }) : resolve(USER); | ||
return new Promise<ApiResponse & { data: UserDetails }>((resolve, reject) => { | ||
forceFail ? reject({ code: 'B4N4N42', description: 'me stuff' }) : resolve({ message: '', status: true, data: { ...USER } }); | ||
}); | ||
@@ -27,5 +27,5 @@ } | ||
function login(user: UserDetails) { | ||
return new Promise<TokenParams>((resolve, reject) => { | ||
return new Promise<ApiResponse & { data: TokenParams }>((resolve, reject) => { | ||
if (user.username == 'finto') { | ||
resolve({ access_token: 'new_at2', refresh_token: 'new_rt2' }); | ||
resolve({ message: '', status: true, data: { access_token: 'new_at2', refresh_token: 'new_rt2' } }); | ||
} | ||
@@ -55,2 +55,3 @@ reject({ code: 'B4N4N43', description: 'login stuff' }); | ||
await waitForNextUpdate(); | ||
// assert initial state | ||
@@ -104,3 +105,3 @@ expect(result.current.initialized).toBe(true); | ||
test('login with wrong user will fail', async () => { | ||
test('login with wrong user will fail', () => { | ||
Lockr.rm(TOKEN_KEY); | ||
@@ -114,3 +115,3 @@ Lockr.rm(REFRESH_TOKEN_KEY); | ||
await act(async () => { | ||
act(() => { | ||
//log in with existing user | ||
@@ -120,3 +121,2 @@ result.current.login({ username: 'vero', password: 'vera' }).catch((error: JwtError) => { | ||
}); | ||
await waitForNextUpdate(); | ||
}); | ||
@@ -123,0 +123,0 @@ |
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
16120
384