react-auth-kit
Advanced tools
Comparing version 1.4.9 to 1.4.10-0
@@ -1,62 +0,3 @@ | ||
/*! react-auth-kit v1.4.9 | Apache-2.0 */ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react"),require("js-cookie"),require("react-router-dom")):"function"==typeof define&&define.amd?define(["exports","react","js-cookie","react-router-dom"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).ReactAuthKit={},e.React,e.Cookies,e.ReactRouterDOM)}(this,(function(e,t,n,r){"use strict";function o(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}function a(e){if(e&&e.__esModule)return e;var t=Object.create(null);return e&&Object.keys(e).forEach((function(n){if("default"!==n){var r=Object.getOwnPropertyDescriptor(e,n);Object.defineProperty(t,n,r.get?r:{enumerable:!0,get:function(){return e[n]}})}})),t.default=e,Object.freeze(t)}var u=a(t),i=o(n),h=function(){function e(e){var t=e.authStorageName,n=e.authStorageType,r=e.authTimeStorageName,o=e.stateStorageName,a=e.refreshTokenName,u=e.cookieDomain,i=e.cookieSecure;this.authStorageType=n,this.authStorageName=t,this.authTimeStorageName=r,this.stateStorageName=o,this.refreshTokenName=a,this.cookieDomain=u,this.cookieSecure=i,this.authStorageTypeName=this.authStorageName+"_type",this.isUsingRefreshToken=!!this.refreshTokenName,this.refreshTokenTimeName=this.refreshTokenName?this.refreshTokenName+"_time":null}return e.prototype.initialToken=function(){return"cookie"===this.authStorageType?this.initialCookieToken_():this.initialLSToken_()},e.prototype.initialCookieToken_=function(){var e=i.default.get(this.authStorageName),t=i.default.get(this.authStorageTypeName),n=i.default.get(this.authTimeStorageName),r=i.default.get(this.stateStorageName),o=this.isUsingRefreshToken&&null!=this.refreshTokenName?i.default.get(this.refreshTokenName):null,a=this.isUsingRefreshToken&&null!=this.refreshTokenTimeName?i.default.get(this.refreshTokenTimeName):null;return this.checkTokenExist(e,t,n,r,o,a)},e.prototype.initialLSToken_=function(){var e=localStorage.getItem(this.authStorageName),t=localStorage.getItem(this.authStorageTypeName),n=localStorage.getItem(this.authTimeStorageName),r=localStorage.getItem(this.stateStorageName),o=this.isUsingRefreshToken&&null!=this.refreshTokenName?localStorage.getItem(this.refreshTokenName):null,a=this.isUsingRefreshToken&&null!=this.refreshTokenTimeName?localStorage.getItem(this.refreshTokenTimeName):null;return this.checkTokenExist(e,t,n,r,o,a)},e.prototype.checkTokenExist=function(e,t,n,r,o,a){if(!(e&&t&&n&&r))return{authToken:null,authTokenType:null,isUsingRefreshToken:this.isUsingRefreshToken,refreshToken:null,expireAt:null,authState:null,refreshTokenExpireAt:null};var u=new Date(n);try{var i=JSON.parse(r);return{authToken:e,authTokenType:t,isUsingRefreshToken:this.isUsingRefreshToken,refreshToken:this.isUsingRefreshToken&&o?o:null,refreshTokenExpireAt:this.isUsingRefreshToken&&a?new Date(a):null,expireAt:u,authState:i}}catch(e){return{authToken:null,authTokenType:null,isUsingRefreshToken:this.isUsingRefreshToken,refreshToken:null,expireAt:null,authState:null,refreshTokenExpireAt:null}}},e.prototype.syncTokens=function(e){void 0===e.authToken||null===e.authTokenType||null===e.authToken||null===e.expireAt||null===e.authState?this.removeToken():this.setToken(e.authToken,e.authTokenType,e.refreshToken,e.refreshTokenExpireAt,e.expireAt,e.authState)},e.prototype.setToken=function(e,t,n,r,o,a){"cookie"===this.authStorageType?this.setCookieToken_(e,t,n,o,r,a):this.setLSToken_(e,t,n,o,r,a)},e.prototype.setCookieToken_=function(e,t,n,r,o,a){i.default.set(this.authStorageName,e,{expires:r,domain:this.cookieDomain,secure:this.cookieSecure}),i.default.set(this.authStorageTypeName,t,{expires:r,domain:this.cookieDomain,secure:this.cookieSecure}),i.default.set(this.authTimeStorageName,r.toISOString(),{expires:r,domain:this.cookieDomain,secure:this.cookieSecure}),i.default.set(this.stateStorageName,a,{expires:r,domain:this.cookieDomain,secure:this.cookieSecure}),this.isUsingRefreshToken&&this.refreshTokenName&&n&&i.default.set(this.refreshTokenName,n,{expires:r,domain:this.cookieDomain,secure:this.cookieSecure}),this.isUsingRefreshToken&&this.refreshTokenTimeName&&o&&i.default.set(this.refreshTokenTimeName,o.toISOString(),{expires:r,domain:this.cookieDomain,secure:this.cookieSecure})},e.prototype.setLSToken_=function(e,t,n,r,o,a){localStorage.setItem(this.authStorageName,e),localStorage.setItem(this.authStorageTypeName,t),localStorage.setItem(this.authTimeStorageName,r.toISOString()),localStorage.setItem(this.stateStorageName,JSON.stringify(a)),this.isUsingRefreshToken&&this.refreshTokenName&&n&&localStorage.setItem(this.refreshTokenName,n),this.isUsingRefreshToken&&this.refreshTokenTimeName&&o&&localStorage.setItem(this.refreshTokenTimeName,o.toISOString())},e.prototype.removeToken=function(){"cookie"===this.authStorageType?this.removeCookieToken_():this.removeLSToken_()},e.prototype.removeCookieToken_=function(){i.default.remove(this.authStorageName,{domain:this.cookieDomain,secure:this.cookieSecure}),i.default.remove(this.authTimeStorageName,{domain:this.cookieDomain,secure:this.cookieSecure}),i.default.remove(this.stateStorageName,{domain:this.cookieDomain,secure:this.cookieSecure}),this.isUsingRefreshToken&&this.refreshTokenName&&i.default.remove(this.refreshTokenName,{domain:this.cookieDomain,secure:this.cookieSecure}),this.isUsingRefreshToken&&this.refreshTokenTimeName&&i.default.remove(this.refreshTokenTimeName,{domain:this.cookieDomain,secure:this.cookieSecure})},e.prototype.removeLSToken_=function(){localStorage.removeItem(this.authStorageName),localStorage.removeItem(this.authTimeStorageName),localStorage.removeItem(this.stateStorageName),this.isUsingRefreshToken&&this.refreshTokenName&&localStorage.removeItem(this.refreshTokenName),this.isUsingRefreshToken&&this.refreshTokenTimeName&&localStorage.removeItem(this.refreshTokenTimeName)},e}(),s=u.createContext({authState:{authTokenType:null,authState:null,authToken:null,isUsingRefreshToken:!1,refreshToken:null,refreshTokenExpireAt:null,expireAt:null},setAuthState:function(){}}),l=function(e){var t=e.children,n=e.authType,r=e.authName,o=e.refreshToken,a=e.cookieDomain,i=e.cookieSecure;if("cookie"===n&&!a)throw new Error("authStorageType 'cookie' requires 'cookieDomain' and 'cookieSecure' in AuthProvider");var l=new h({authTimeStorageName:r+"_time",authStorageType:n,authStorageName:r,refreshTokenName:o?r+"_refresh":void 0,cookieDomain:a,cookieSecure:i,stateStorageName:r+"_state"}),c=u.useState(l.initialToken()),f=c[0],T=c[1];return u.useEffect((function(){l.syncTokens(f)}),[f]),u.createElement(s.Provider,{value:{authState:f,setAuthState:T}},t)};l.defaultProps={authType:"cookie",authName:"_auth",cookieDomain:window.location.hostname,cookieSecure:"https:"===window.location.protocol};var c=s.Consumer,f=function(){return(f=Object.assign||function(e){for(var t,n=1,r=arguments.length;n<r;n++)for(var o in t=arguments[n])Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o]);return e}).apply(this,arguments)}; | ||
/*! ***************************************************************************** | ||
Copyright (c) Microsoft Corporation. | ||
Permission to use, copy, modify, and/or distribute this software for any | ||
purpose with or without fee is hereby granted. | ||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH | ||
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY | ||
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, | ||
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM | ||
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR | ||
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR | ||
PERFORMANCE OF THIS SOFTWARE. | ||
***************************************************************************** */var T=function(){function e(e){if(this.context=e,!this.context.authState.isUsingRefreshToken)throw new Error("The app doesn't implement 'refreshToken' feature.\nSo you have to implement refresh token feature from 'AuthProvider' before using it.")}return e.prototype.getCurrentRefreshToken=function(){return{refreshToken:this.context.authState.refreshToken,refreshTokenExpireAt:this.context.authState.refreshTokenExpireAt}},e.prototype.getCurrentAuthState=function(){return{authToken:this.context.authState.authToken,authTokenType:this.context.authState.authTokenType,expireAt:this.context.authState.expireAt}},e.prototype.getCurrentUserState=function(){return this.context.authState.authState},e.prototype.updateRefreshToken=function(e,t){var n=new Date((new Date).getTime()+60*t*1e3);this.context.setAuthState((function(t){return f(f({},t),{refreshToken:e,refreshTokenExpireAt:n})}))},e.prototype.updateAuthState=function(e,t,n){var r={authToken:e};if(void 0!==t&&Object.assign(r,{authTokenType:t}),void 0!==n){var o=new Date((new Date).getTime()+60*n*1e3);Object.assign(r,{expireAt:o})}this.context.setAuthState((function(e){return f(f({},e),r)}))},e.prototype.updateUserState=function(e){this.context.setAuthState((function(t){return f(f({},t),{authState:e})}))},e}();e.AuthProvider=l,e.PrivateRoute=function(e){var t=u.useContext(s),n=e.component,o=e.loginPath,a=e.strict,i=e.sensitive,h=e.exact,l=e.path,c=e.location,T=e.render;return u.createElement(r.Route,{location:c,path:l,exact:h,sensitive:i,strict:a,render:function(e){return(null==t?void 0:t.authState.authToken)&&(null==t?void 0:t.authState.expireAt)&&(new Date(t.authState.expireAt)>new Date||(t.setAuthState((function(e){return f(f({},e),{authToken:null,authTokenType:null,expireAt:null,authState:null,refreshToken:null,refreshTokenExpireAt:null})})),0))?n?u.createElement(n,e):T?T(e):null:u.createElement(r.Redirect,{to:o})}})},e.useAuth= | ||
/** | ||
*@author Arkadip Bhattacharya <in2arkadipb13@gmail.com> | ||
*@fileoverview Authentication | ||
*@copyright Arkadip Bhattacharya 2020 | ||
*@license Apache-2.0 | ||
*/ | ||
function(){var e=u.useContext(s);return{authHeader:function(){return(null==e?void 0:e.authState)?e.authState.authTokenType+" "+e.authState.authToken:"Bearer "},isAuthenticated:function(){return!(!(null==e?void 0:e.authState.authToken)||!(null==e?void 0:e.authState.expireAt))&&(new Date(e.authState.expireAt)>new Date||(e.setAuthState((function(e){return f(f({},e),{authToken:null,authTokenType:null,expireAt:null,authState:null,refreshToken:null,refreshTokenExpireAt:null})})),!1))},authUser:function(){return e.authState.authState},signOut:function(){try{return!!(null==e?void 0:e.authState.authToken)&&(e.setAuthState((function(e){return f(f({},e),{authToken:null,authTokenType:null,refreshToken:null,refreshTokenExpireAt:null,expireAt:null,authState:null})})),console.log("RAJ :: Signing Out"),!0)}catch(e){return!1}},signIn:function(t){var n=t.token,r=t.tokenType,o=t.authState,a=t.expiresIn,u=t.refreshToken,i=t.refreshTokenExpireIn;if((u||i)&&!e.authState.isUsingRefreshToken)throw new Error("The app doesn't implement 'refreshToken' feature.\nSo you have to implement refresh token feature from 'AuthProvider' before using it.");var h=new Date((new Date).getTime()+60*a*1e3),s=i?new Date((new Date).getTime()+60*i*1e3):null;try{return!!e&&(e.setAuthState((function(e){return f(f({},e),{authToken:n,authTokenType:r,expireAt:h,authState:o,refreshToken:u||null,refreshTokenExpireAt:s})})),!0)}catch(e){return console.error(e),!1}}}} | ||
/** | ||
*@author Arkadip Bhattacharya <in2arkadipb13@gmail.com> | ||
*@fileoverview Sign In functionality | ||
*@copyright Arkadip Bhattacharya 2020 | ||
*@license Apache-2.0 | ||
*/,e.useAuthHeader=function(){var e=u.useContext(s);return function(){return(null==e?void 0:e.authState)?e.authState.authTokenType+" "+e.authState.authToken:"Bearer "}},e.useAuthUser=function(){var e=u.useContext(s);return function(){return e.authState.authState}},e.useIsAuthenticated= | ||
/** | ||
*@author Arkadip Bhattacharya <in2arkadipb13@gmail.com> | ||
*@fileoverview Check User is authenticated or not | ||
*@copyright Arkadip Bhattacharya 2020 | ||
*@license Apache-2.0 | ||
*/ | ||
function(){var e=u.useContext(s);return function(){return!(!(null==e?void 0:e.authState.authToken)||!(null==e?void 0:e.authState.expireAt))&&(new Date(e.authState.expireAt)>new Date||(e.setAuthState((function(e){return f(f({},e),{authToken:null,authTokenType:null,expireAt:null,authState:null,refreshToken:null,refreshTokenExpireAt:null})})),!1))}},e.useRefreshToken=function(){var e=u.useContext(s);return new T(e)},e.useSignIn=function(){var e=u.useContext(s);return function(t){var n=t.token,r=t.tokenType,o=t.authState,a=t.expiresIn,u=t.refreshToken,i=t.refreshTokenExpireIn;if((u||i)&&!e.authState.isUsingRefreshToken)throw new Error("The app doesn't implement 'refreshToken' feature.\nSo you have to implement refresh token feature from 'AuthProvider' before using it.");var h=new Date((new Date).getTime()+60*a*1e3),s=i?new Date((new Date).getTime()+60*i*1e3):null;try{return!!e&&(e.setAuthState((function(e){return f(f({},e),{authToken:n,authTokenType:r,expireAt:h,authState:o,refreshToken:u||null,refreshTokenExpireAt:s})})),!0)}catch(e){return console.error(e),!1}}} | ||
/** | ||
*@author Arkadip Bhattacharya <in2arkadipb13@gmail.com> | ||
*@fileoverview Sign Out functionality | ||
*@copyright Arkadip Bhattacharya 2020 | ||
*@license Apache-2.0 | ||
*/,e.useSignOut=function(){var e=u.useContext(s);return function(){try{return!!(null==e?void 0:e.authState.authToken)&&(e.setAuthState((function(e){return f(f({},e),{authToken:null,authTokenType:null,expireAt:null,authState:null,refreshToken:null,refreshTokenExpireAt:null})})),console.log("RAJ :: Signing Out"),!0)}catch(e){return!1}}},e.withAuthHeader= | ||
/** | ||
*@author Arkadip Bhattacharya <in2arkadipb13@gmail.com> | ||
*@fileoverview Authentication header | ||
*@copyright Arkadip Bhattacharya 2020 | ||
*@license Apache-2.0 | ||
*/ | ||
function(e){return function(t){return u.createElement(c,null,(function(n){return(null==n?void 0:n.authState)?u.createElement(e,f({},t,{authHeader:n.authState.authTokenType+" "+n.authState.authToken})):u.createElement(e,f({},t,{authHeader:null}))}))}} | ||
/** | ||
*@author Arkadip Bhattacharya <in2arkadipb13@gmail.com> | ||
*@fileoverview Authentication header | ||
*@copyright Arkadip Bhattacharya 2020 | ||
*@license Apache-2.0 | ||
*/,e.withAuthUser=function(e){return function(t){return u.createElement(c,null,(function(n){return u.createElement(e,f({},t,{authState:null==n?void 0:n.authState.authState}))}))}},e.withIsAuthenticated= | ||
/** | ||
*@author Arkadip Bhattacharya <in2arkadipb13@gmail.com> | ||
*@fileoverview Authentication status | ||
*@copyright Arkadip Bhattacharya 2020 | ||
*@license Apache-2.0 | ||
*/ | ||
function(e){return function(t){return u.createElement(c,null,(function(n){return(null==n?void 0:n.authState.authToken)&&(null==n?void 0:n.authState.expireAt)?new Date(n.authState.expireAt)>new Date?u.createElement(e,f({},t,{isAuth:!0})):(n.setAuthState((function(e){return f(f({},e),{authToken:null,authTokenType:null,expireAt:null,authState:null,refreshToken:null,refreshTokenExpireAt:null})})),u.createElement(e,f({},t,{isAuth:!1}))):u.createElement(e,f({},t,{isAuth:!1}))}))}},e.withRefreshToken=function(e){return function(t){return u.createElement(c,null,(function(n){return u.createElement(e,f({},t,{refreshToken:new T(n)}))}))}},e.withSignIn=function(e){return function(t){return u.createElement(c,null,(function(n){return u.createElement(e,f({},t,{signIn:function(e){var t=e.token,r=e.tokenType,o=e.authState,a=e.expiresIn,u=e.refreshToken,i=e.refreshTokenExpireIn;if((u||i)&&!n.authState.isUsingRefreshToken)throw new Error("The app doesn't implement 'refreshToken' feature.\nSo you have to implement refresh token feature from 'AuthProvider' before using it.");var h=new Date((new Date).getTime()+60*a*1e3),s=i?new Date((new Date).getTime()+60*i*1e3):null;try{return!!n&&(n.setAuthState((function(e){return f(f({},e),{authToken:t,authTokenType:r,expireAt:h,authState:o,refreshToken:u||null,refreshTokenExpireAt:s})})),!0)}catch(e){return console.error(e),!1}}}))}))}},e.withSignOut=function(e){return function(t){return u.createElement(c,null,(function(n){return u.createElement(e,f({},t,{signOut:function(){try{return!!(null==n?void 0:n.authState.authToken)&&(n.setAuthState((function(e){return f(f({},e),{authToken:null,authTokenType:null,expireAt:null,authState:null,refreshToken:null,refreshTokenExpireAt:null})})),!0)}catch(e){return!1}}}))}))}},Object.defineProperty(e,"__esModule",{value:!0})})); | ||
/*! react-auth-kit v1.4.10-0 | Apache-2.0 */ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react"),require("js-cookie"),require("react-router-dom")):"function"==typeof define&&define.amd?define(["exports","react","js-cookie","react-router-dom"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).ReactAuthKit={},e.React,e.Cookies,e.ReactRouterDOM)}(this,(function(e,t,n,r){"use strict";function o(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}function a(e){if(e&&e.__esModule)return e;var t=Object.create(null);return e&&Object.keys(e).forEach((function(n){if("default"!==n){var r=Object.getOwnPropertyDescriptor(e,n);Object.defineProperty(t,n,r.get?r:{enumerable:!0,get:function(){return e[n]}})}})),t.default=e,Object.freeze(t)}var u=a(t),i=o(n),h=u.createContext({authState:{authTokenType:null,authState:null,authToken:null,isUsingRefreshToken:!1,refreshToken:null,refreshTokenExpireAt:null,expireAt:null},setAuthState:function(){}}),s=h.Consumer,l=function(){function e(e){var t=e.authStorageName,n=e.authStorageType,r=e.authTimeStorageName,o=e.stateStorageName,a=e.refreshTokenName,u=e.cookieDomain,i=e.cookieSecure;this.authStorageType=n,this.authStorageName=t,this.authTimeStorageName=r,this.stateStorageName=o,this.refreshTokenName=a,this.cookieDomain=u,this.cookieSecure=i,this.authStorageTypeName=this.authStorageName+"_type",this.isUsingRefreshToken=!!this.refreshTokenName,this.refreshTokenTimeName=this.refreshTokenName?this.refreshTokenName+"_time":null}return e.prototype.initialToken=function(){return"cookie"===this.authStorageType?this.initialCookieToken_():this.initialLSToken_()},e.prototype.initialCookieToken_=function(){var e=i.default.get(this.authStorageName),t=i.default.get(this.authStorageTypeName),n=i.default.get(this.authTimeStorageName),r=i.default.get(this.stateStorageName),o=this.isUsingRefreshToken&&null!=this.refreshTokenName?i.default.get(this.refreshTokenName):null,a=this.isUsingRefreshToken&&null!=this.refreshTokenTimeName?i.default.get(this.refreshTokenTimeName):null;return this.checkTokenExist(e,t,n,r,o,a)},e.prototype.initialLSToken_=function(){var e=localStorage.getItem(this.authStorageName),t=localStorage.getItem(this.authStorageTypeName),n=localStorage.getItem(this.authTimeStorageName),r=localStorage.getItem(this.stateStorageName),o=this.isUsingRefreshToken&&null!=this.refreshTokenName?localStorage.getItem(this.refreshTokenName):null,a=this.isUsingRefreshToken&&null!=this.refreshTokenTimeName?localStorage.getItem(this.refreshTokenTimeName):null;return this.checkTokenExist(e,t,n,r,o,a)},e.prototype.checkTokenExist=function(e,t,n,r,o,a){if(!(e&&t&&n&&r))return{authToken:null,authTokenType:null,isUsingRefreshToken:this.isUsingRefreshToken,refreshToken:null,expireAt:null,authState:null,refreshTokenExpireAt:null};var u=new Date(n);try{var i=JSON.parse(r);return{authToken:e,authTokenType:t,isUsingRefreshToken:this.isUsingRefreshToken,refreshToken:this.isUsingRefreshToken&&o?o:null,refreshTokenExpireAt:this.isUsingRefreshToken&&a?new Date(a):null,expireAt:u,authState:i}}catch(e){return{authToken:null,authTokenType:null,isUsingRefreshToken:this.isUsingRefreshToken,refreshToken:null,expireAt:null,authState:null,refreshTokenExpireAt:null}}},e.prototype.syncTokens=function(e){void 0===e.authToken||null===e.authTokenType||null===e.authToken||null===e.expireAt||null===e.authState?this.removeToken():this.setToken(e.authToken,e.authTokenType,e.refreshToken,e.refreshTokenExpireAt,e.expireAt,e.authState)},e.prototype.setToken=function(e,t,n,r,o,a){"cookie"===this.authStorageType?this.setCookieToken_(e,t,n,o,r,a):this.setLSToken_(e,t,n,o,r,a)},e.prototype.setCookieToken_=function(e,t,n,r,o,a){i.default.set(this.authStorageName,e,{expires:r,domain:this.cookieDomain,secure:this.cookieSecure}),i.default.set(this.authStorageTypeName,t,{expires:r,domain:this.cookieDomain,secure:this.cookieSecure}),i.default.set(this.authTimeStorageName,r.toISOString(),{expires:r,domain:this.cookieDomain,secure:this.cookieSecure}),i.default.set(this.stateStorageName,a,{expires:r,domain:this.cookieDomain,secure:this.cookieSecure}),this.isUsingRefreshToken&&this.refreshTokenName&&n&&i.default.set(this.refreshTokenName,n,{expires:r,domain:this.cookieDomain,secure:this.cookieSecure}),this.isUsingRefreshToken&&this.refreshTokenTimeName&&o&&i.default.set(this.refreshTokenTimeName,o.toISOString(),{expires:r,domain:this.cookieDomain,secure:this.cookieSecure})},e.prototype.setLSToken_=function(e,t,n,r,o,a){localStorage.setItem(this.authStorageName,e),localStorage.setItem(this.authStorageTypeName,t),localStorage.setItem(this.authTimeStorageName,r.toISOString()),localStorage.setItem(this.stateStorageName,JSON.stringify(a)),this.isUsingRefreshToken&&this.refreshTokenName&&n&&localStorage.setItem(this.refreshTokenName,n),this.isUsingRefreshToken&&this.refreshTokenTimeName&&o&&localStorage.setItem(this.refreshTokenTimeName,o.toISOString())},e.prototype.removeToken=function(){"cookie"===this.authStorageType?this.removeCookieToken_():this.removeLSToken_()},e.prototype.removeCookieToken_=function(){i.default.remove(this.authStorageName,{domain:this.cookieDomain,secure:this.cookieSecure}),i.default.remove(this.authTimeStorageName,{domain:this.cookieDomain,secure:this.cookieSecure}),i.default.remove(this.stateStorageName,{domain:this.cookieDomain,secure:this.cookieSecure}),this.isUsingRefreshToken&&this.refreshTokenName&&i.default.remove(this.refreshTokenName,{domain:this.cookieDomain,secure:this.cookieSecure}),this.isUsingRefreshToken&&this.refreshTokenTimeName&&i.default.remove(this.refreshTokenTimeName,{domain:this.cookieDomain,secure:this.cookieSecure})},e.prototype.removeLSToken_=function(){localStorage.removeItem(this.authStorageName),localStorage.removeItem(this.authTimeStorageName),localStorage.removeItem(this.stateStorageName),this.isUsingRefreshToken&&this.refreshTokenName&&localStorage.removeItem(this.refreshTokenName),this.isUsingRefreshToken&&this.refreshTokenTimeName&&localStorage.removeItem(this.refreshTokenTimeName)},e}(),c=function(e){var t=e.children,n=e.authType,r=e.authName,o=e.refreshToken,a=e.cookieDomain,i=e.cookieSecure;if("cookie"===n&&!a)throw new Error("authStorageType 'cookie' requires 'cookieDomain' and 'cookieSecure' in AuthProvider");var s=new l({authTimeStorageName:r+"_time",authStorageType:n,authStorageName:r,refreshTokenName:o?r+"_refresh":void 0,cookieDomain:a,cookieSecure:i,stateStorageName:r+"_state"}),c=u.useState(s.initialToken()),f=c[0],T=c[1];return u.useEffect((function(){s.syncTokens(f)}),[f]),u.createElement(h.Provider,{value:{authState:f,setAuthState:T}},t)};c.defaultProps={authType:"cookie",authName:"_auth",cookieDomain:window.location.hostname,cookieSecure:"https:"===window.location.protocol};var f=function(){return(f=Object.assign||function(e){for(var t,n=1,r=arguments.length;n<r;n++)for(var o in t=arguments[n])Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o]);return e}).apply(this,arguments)};var T=function(){function e(e){if(this.context=e,!this.context.authState.isUsingRefreshToken)throw new Error("The app doesn't implement 'refreshToken' feature.\nSo you have to implement refresh token feature from 'AuthProvider' before using it.")}return e.prototype.getCurrentRefreshToken=function(){return{refreshToken:this.context.authState.refreshToken,refreshTokenExpireAt:this.context.authState.refreshTokenExpireAt}},e.prototype.getCurrentAuthState=function(){return{authToken:this.context.authState.authToken,authTokenType:this.context.authState.authTokenType,expireAt:this.context.authState.expireAt}},e.prototype.getCurrentUserState=function(){return this.context.authState.authState},e.prototype.updateRefreshToken=function(e,t){var n=new Date((new Date).getTime()+60*t*1e3);this.context.setAuthState((function(t){return f(f({},t),{refreshToken:e,refreshTokenExpireAt:n})}))},e.prototype.updateAuthState=function(e,t,n){var r={authToken:e};if(void 0!==t&&Object.assign(r,{authTokenType:t}),void 0!==n){var o=new Date((new Date).getTime()+60*n*1e3);Object.assign(r,{expireAt:o})}this.context.setAuthState((function(e){return f(f({},e),r)}))},e.prototype.updateUserState=function(e){this.context.setAuthState((function(t){return f(f({},t),{authState:e})}))},e}();e.AuthProvider=c,e.PrivateRoute=function(e){var t=u.useContext(h),n=e.component,o=e.loginPath,a=e.strict,i=e.sensitive,s=e.exact,l=e.path,c=e.location,T=e.render;return u.createElement(r.Route,{location:c,path:l,exact:s,sensitive:i,strict:a,render:function(e){return(null==t?void 0:t.authState.authToken)&&(null==t?void 0:t.authState.expireAt)&&(new Date(t.authState.expireAt)>new Date||(t.setAuthState((function(e){return f(f({},e),{authToken:null,authTokenType:null,expireAt:null,authState:null,refreshToken:null,refreshTokenExpireAt:null})})),0))?n?u.createElement(n,e):T?T(e):null:u.createElement(r.Redirect,{to:o})}})},e.useAuth=function(){var e=u.useContext(h);return{authHeader:function(){return(null==e?void 0:e.authState)?e.authState.authTokenType+" "+e.authState.authToken:"Bearer "},isAuthenticated:function(){return!(!(null==e?void 0:e.authState.authToken)||!(null==e?void 0:e.authState.expireAt))&&(new Date(e.authState.expireAt)>new Date||(e.setAuthState((function(e){return f(f({},e),{authToken:null,authTokenType:null,expireAt:null,authState:null,refreshToken:null,refreshTokenExpireAt:null})})),!1))},authUser:function(){return e.authState.authState},signOut:function(){try{return!!(null==e?void 0:e.authState.authToken)&&(e.setAuthState((function(e){return f(f({},e),{authToken:null,authTokenType:null,refreshToken:null,refreshTokenExpireAt:null,expireAt:null,authState:null})})),console.log("RAJ :: Signing Out"),!0)}catch(e){return!1}},signIn:function(t){var n=t.token,r=t.tokenType,o=t.authState,a=t.expiresIn,u=t.refreshToken,i=t.refreshTokenExpireIn;if((u||i)&&!e.authState.isUsingRefreshToken)throw new Error("The app doesn't implement 'refreshToken' feature.\nSo you have to implement refresh token feature from 'AuthProvider' before using it.");var h=new Date((new Date).getTime()+60*a*1e3),s=i?new Date((new Date).getTime()+60*i*1e3):null;try{return!!e&&(e.setAuthState((function(e){return f(f({},e),{authToken:n,authTokenType:r,expireAt:h,authState:o,refreshToken:u||null,refreshTokenExpireAt:s})})),!0)}catch(e){return console.error(e),!1}}}},e.useAuthHeader=function(){var e=u.useContext(h);return function(){return(null==e?void 0:e.authState)?e.authState.authTokenType+" "+e.authState.authToken:"Bearer "}},e.useAuthUser=function(){var e=u.useContext(h);return function(){return e.authState.authState}},e.useIsAuthenticated=function(){var e=u.useContext(h);return function(){return!(!(null==e?void 0:e.authState.authToken)||!(null==e?void 0:e.authState.expireAt))&&(new Date(e.authState.expireAt)>new Date||(e.setAuthState((function(e){return f(f({},e),{authToken:null,authTokenType:null,expireAt:null,authState:null,refreshToken:null,refreshTokenExpireAt:null})})),!1))}},e.useRefreshToken=function(){var e=u.useContext(h);return new T(e)},e.useSignIn=function(){var e=u.useContext(h);return function(t){var n=t.token,r=t.tokenType,o=t.authState,a=t.expiresIn,u=t.refreshToken,i=t.refreshTokenExpireIn;if((u||i)&&!e.authState.isUsingRefreshToken)throw new Error("The app doesn't implement 'refreshToken' feature.\nSo you have to implement refresh token feature from 'AuthProvider' before using it.");var h=new Date((new Date).getTime()+60*a*1e3),s=i?new Date((new Date).getTime()+60*i*1e3):null;try{return!!e&&(e.setAuthState((function(e){return f(f({},e),{authToken:n,authTokenType:r,expireAt:h,authState:o,refreshToken:u||null,refreshTokenExpireAt:s})})),!0)}catch(e){return console.error(e),!1}}},e.useSignOut=function(){var e=u.useContext(h);return function(){try{return!!(null==e?void 0:e.authState.authToken)&&(e.setAuthState((function(e){return f(f({},e),{authToken:null,authTokenType:null,expireAt:null,authState:null,refreshToken:null,refreshTokenExpireAt:null})})),console.log("RAJ :: Signing Out"),!0)}catch(e){return!1}}},e.withAuthHeader=function(e){return function(t){return u.createElement(s,null,(function(n){return(null==n?void 0:n.authState)?u.createElement(e,f({},t,{authHeader:n.authState.authTokenType+" "+n.authState.authToken})):u.createElement(e,f({},t,{authHeader:null}))}))}},e.withAuthUser=function(e){return function(t){return u.createElement(s,null,(function(n){return u.createElement(e,f({},t,{authState:null==n?void 0:n.authState.authState}))}))}},e.withIsAuthenticated=function(e){return function(t){return u.createElement(s,null,(function(n){return(null==n?void 0:n.authState.authToken)&&(null==n?void 0:n.authState.expireAt)?new Date(n.authState.expireAt)>new Date?u.createElement(e,f({},t,{isAuth:!0})):(n.setAuthState((function(e){return f(f({},e),{authToken:null,authTokenType:null,expireAt:null,authState:null,refreshToken:null,refreshTokenExpireAt:null})})),u.createElement(e,f({},t,{isAuth:!1}))):u.createElement(e,f({},t,{isAuth:!1}))}))}},e.withRefreshToken=function(e){return function(t){return u.createElement(s,null,(function(n){return u.createElement(e,f({},t,{refreshToken:new T(n)}))}))}},e.withSignIn=function(e){return function(t){return u.createElement(s,null,(function(n){return u.createElement(e,f({},t,{signIn:function(e){var t=e.token,r=e.tokenType,o=e.authState,a=e.expiresIn,u=e.refreshToken,i=e.refreshTokenExpireIn;if((u||i)&&!n.authState.isUsingRefreshToken)throw new Error("The app doesn't implement 'refreshToken' feature.\nSo you have to implement refresh token feature from 'AuthProvider' before using it.");var h=new Date((new Date).getTime()+60*a*1e3),s=i?new Date((new Date).getTime()+60*i*1e3):null;try{return!!n&&(n.setAuthState((function(e){return f(f({},e),{authToken:t,authTokenType:r,expireAt:h,authState:o,refreshToken:u||null,refreshTokenExpireAt:s})})),!0)}catch(e){return console.error(e),!1}}}))}))}},e.withSignOut=function(e){return function(t){return u.createElement(s,null,(function(n){return u.createElement(e,f({},t,{signOut:function(){try{return!!(null==n?void 0:n.authState.authToken)&&(n.setAuthState((function(e){return f(f({},e),{authToken:null,authTokenType:null,expireAt:null,authState:null,refreshToken:null,refreshTokenExpireAt:null})})),!0)}catch(e){return!1}}}))}))}},Object.defineProperty(e,"__esModule",{value:!0})})); | ||
//# sourceMappingURL=index.umd.js.map |
{ | ||
"name": "react-auth-kit", | ||
"version": "1.4.9", | ||
"version": "1.4.10-0", | ||
"description": "Authentication Library for React, which makes Token based auth very easy", | ||
"source": "src/index.tsx", | ||
"main": "dist/index.js", | ||
"module": "dist/index.modern.js", | ||
"main": "dist/cjs/index.js", | ||
"module": "dist/esm/index.modern.js", | ||
"unpkg": "dist/index.umd.js", | ||
"types": "dist/types/index.d.ts", | ||
"files": [ | ||
@@ -45,5 +46,3 @@ "dist" | ||
"devDependencies": { | ||
"@rollup/plugin-commonjs": "^19.0.0", | ||
"@rollup/plugin-node-resolve": "^13.0.0", | ||
"@rollup/plugin-typescript": "^8.2.1", | ||
"@types/jest": "^26.0.10", | ||
@@ -69,5 +68,7 @@ "@types/js-cookie": "^2.2.6", | ||
"rollup-plugin-terser": "^7.0.0", | ||
"rollup-plugin-typescript2": "^0.30.0", | ||
"ts-jest": "^26.3.0", | ||
"tslib": "^1.14.1", | ||
"typedoc": "^0.20.16", | ||
"typescript": "^4.0.2", | ||
"typescript": "^4.2.4", | ||
"utility-types": "^3.10.0" | ||
@@ -74,0 +75,0 @@ }, |
@@ -19,2 +19,5 @@ <p align="center"> | ||
</a> | ||
<a href="https://bundlephobia.com/result?p=react-auth-kit"> | ||
<img src="https://img.shields.io/bundlephobia/minzip/react-auth-kit?style=flat-square" alt="Test Suites"> | ||
</a> | ||
<a href="https://www.codacy.com/gh/react-auth-kit/react-auth-kit?utm_source=github.com&utm_medium=referral&utm_content=react-auth-kit/react-auth-kit&utm_campaign=Badge_Grade"> | ||
@@ -34,3 +37,3 @@ <img src="https://app.codacy.com/project/badge/Grade/a65202426152483d8e63d6623721080c" alt="Codacy Badge"> | ||
* 📦 3.33 KB Gzipped | ||
* 📦 Very lightweight | ||
* ⚛️Build for React Js | ||
@@ -37,0 +40,0 @@ * 🚀 Fast and easy to Implement |
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
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 4 instances in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
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
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
126916
98
916
49
17