react-auth-kit
Advanced tools
Comparing version 2.8.1 to 2.8.2
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
var React = require('react'); | ||
@@ -9,6 +7,3 @@ var Cookies = require('js-cookie'); | ||
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } | ||
function _interopNamespace(e) { | ||
if (e && e.__esModule) return e; | ||
function _interopNamespaceDefault(e) { | ||
var n = Object.create(null); | ||
@@ -26,8 +21,7 @@ if (e) { | ||
} | ||
n["default"] = e; | ||
n.default = e; | ||
return Object.freeze(n); | ||
} | ||
var React__namespace = /*#__PURE__*/_interopNamespace(React); | ||
var Cookies__default = /*#__PURE__*/_interopDefaultLegacy(Cookies); | ||
var React__namespace = /*#__PURE__*/_interopNamespaceDefault(React); | ||
@@ -153,11 +147,11 @@ /** | ||
TokenObject.prototype.initialCookieToken_ = function () { | ||
var authToken = Cookies__default["default"].get(this.authStorageName); | ||
var authTokenType = Cookies__default["default"].get(this.authStorageTypeName); | ||
var authTokenTime = Cookies__default["default"].get(this.authTimeStorageName); | ||
var stateCookie = Cookies__default["default"].get(this.stateStorageName); | ||
var authToken = Cookies.get(this.authStorageName); | ||
var authTokenType = Cookies.get(this.authStorageTypeName); | ||
var authTokenTime = Cookies.get(this.authTimeStorageName); | ||
var stateCookie = Cookies.get(this.stateStorageName); | ||
var refreshToken = this.isUsingRefreshToken && | ||
this.refreshTokenName != null ? Cookies__default["default"].get(this.refreshTokenName) : null; | ||
this.refreshTokenName != null ? Cookies.get(this.refreshTokenName) : null; | ||
var refreshTokenTime = this.isUsingRefreshToken && | ||
this.refreshTokenTimeName != null ? | ||
Cookies__default["default"].get(this.refreshTokenTimeName) : null; | ||
Cookies.get(this.refreshTokenTimeName) : null; | ||
return this.checkTokenExist(authToken, authTokenType, authTokenTime, stateCookie, refreshToken, refreshTokenTime); | ||
@@ -304,3 +298,3 @@ }; | ||
TokenObject.prototype.setCookieToken_ = function (authToken, authTokenType, refreshToken, expiresAt, refreshTokenExpiresAt, authState) { | ||
Cookies__default["default"].set(this.authStorageName, authToken, { | ||
Cookies.set(this.authStorageName, authToken, { | ||
expires: expiresAt, | ||
@@ -310,3 +304,3 @@ domain: this.cookieDomain, | ||
}); | ||
Cookies__default["default"].set(this.authStorageTypeName, authTokenType, { | ||
Cookies.set(this.authStorageTypeName, authTokenType, { | ||
expires: expiresAt, | ||
@@ -316,3 +310,3 @@ domain: this.cookieDomain, | ||
}); | ||
Cookies__default["default"].set(this.authTimeStorageName, expiresAt.toISOString(), { | ||
Cookies.set(this.authTimeStorageName, expiresAt.toISOString(), { | ||
expires: expiresAt, | ||
@@ -323,3 +317,3 @@ domain: this.cookieDomain, | ||
if (authState) { | ||
Cookies__default["default"].set(this.stateStorageName, JSON.stringify(authState), { | ||
Cookies.set(this.stateStorageName, JSON.stringify(authState), { | ||
expires: expiresAt, | ||
@@ -332,3 +326,3 @@ domain: this.cookieDomain, | ||
!!refreshToken) { | ||
Cookies__default["default"].set(this.refreshTokenName, refreshToken, { | ||
Cookies.set(this.refreshTokenName, refreshToken, { | ||
expires: expiresAt, | ||
@@ -341,3 +335,3 @@ domain: this.cookieDomain, | ||
!!refreshTokenExpiresAt) { | ||
Cookies__default["default"].set(this.refreshTokenTimeName, refreshTokenExpiresAt.toISOString(), { | ||
Cookies.set(this.refreshTokenTimeName, refreshTokenExpiresAt.toISOString(), { | ||
expires: expiresAt, | ||
@@ -390,15 +384,15 @@ domain: this.cookieDomain, | ||
TokenObject.prototype.removeCookieToken_ = function () { | ||
Cookies__default["default"].remove(this.authStorageName, { | ||
Cookies.remove(this.authStorageName, { | ||
domain: this.cookieDomain, | ||
secure: this.cookieSecure, | ||
}); | ||
Cookies__default["default"].remove(this.authTimeStorageName, { | ||
Cookies.remove(this.authTimeStorageName, { | ||
domain: this.cookieDomain, | ||
secure: this.cookieSecure, | ||
}); | ||
Cookies__default["default"].remove(this.authStorageTypeName, { | ||
Cookies.remove(this.authStorageTypeName, { | ||
domain: this.cookieDomain, | ||
secure: this.cookieSecure, | ||
}); | ||
Cookies__default["default"].remove(this.stateStorageName, { | ||
Cookies.remove(this.stateStorageName, { | ||
domain: this.cookieDomain, | ||
@@ -408,3 +402,3 @@ secure: this.cookieSecure, | ||
if (this.isUsingRefreshToken && !!this.refreshTokenName) { | ||
Cookies__default["default"].remove(this.refreshTokenName, { | ||
Cookies.remove(this.refreshTokenName, { | ||
domain: this.cookieDomain, | ||
@@ -415,3 +409,3 @@ secure: this.cookieSecure, | ||
if (this.isUsingRefreshToken && !!this.refreshTokenTimeName) { | ||
Cookies__default["default"].remove(this.refreshTokenTimeName, { | ||
Cookies.remove(this.refreshTokenTimeName, { | ||
domain: this.cookieDomain, | ||
@@ -418,0 +412,0 @@ secure: this.cookieSecure, |
@@ -1,5 +0,24 @@ | ||
/*! react-auth-kit v2.8.1 | 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,r,n){"use strict";function o(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}function i(e){if(e&&e.__esModule)return e;var t=Object.create(null);return e&&Object.keys(e).forEach((function(r){if("default"!==r){var n=Object.getOwnPropertyDescriptor(e,r);Object.defineProperty(t,r,n.get?n:{enumerable:!0,get:function(){return e[r]}})}})),t.default=e,Object.freeze(t)}var a,s=i(t),u=o(r),h=s.createContext(null),c=h.Consumer,f=function(){return f=Object.assign||function(e){for(var t,r=1,n=arguments.length;r<n;r++)for(var o in t=arguments[r])Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o]);return e},f.apply(this,arguments)},l=function(){function e(e,t,r,n,o){this.authStorageType=t,this.authStorageName=e,this.authTimeStorageName="".concat(e,"_storage"),this.stateStorageName="".concat(e,"_state"),this.refreshTokenName=r,this.cookieDomain=n,this.cookieSecure=o,this.authStorageTypeName="".concat(this.authStorageName,"_type"),this.isUsingRefreshToken=!!this.refreshTokenName,this.refreshTokenTimeName=this.refreshTokenName?"".concat(this.refreshTokenName,"_time"):null}return e.prototype.initialToken=function(){return"cookie"===this.authStorageType?this.initialCookieToken_():this.initialLSToken_()},e.prototype.initialCookieToken_=function(){var e=u.default.get(this.authStorageName),t=u.default.get(this.authStorageTypeName),r=u.default.get(this.authTimeStorageName),n=u.default.get(this.stateStorageName),o=this.isUsingRefreshToken&&null!=this.refreshTokenName?u.default.get(this.refreshTokenName):null,i=this.isUsingRefreshToken&&null!=this.refreshTokenTimeName?u.default.get(this.refreshTokenTimeName):null;return this.checkTokenExist(e,t,r,n,o,i)},e.prototype.initialLSToken_=function(){var e=localStorage.getItem(this.authStorageName),t=localStorage.getItem(this.authStorageTypeName),r=localStorage.getItem(this.authTimeStorageName),n=localStorage.getItem(this.stateStorageName),o=this.isUsingRefreshToken&&null!=this.refreshTokenName?localStorage.getItem(this.refreshTokenName):null,i=this.isUsingRefreshToken&&null!=this.refreshTokenTimeName?localStorage.getItem(this.refreshTokenTimeName):null;return this.checkTokenExist(e,t,r,n,o,i)},e.prototype.checkTokenExist=function(e,t,r,n,o,i){if(!(e&&t&&r&&n))return{auth:null,refresh:null,userState:null,isUsingRefreshToken:this.isUsingRefreshToken,isSignIn:!1};var a=new Date(r);try{var s={auth:{token:e,type:t,expiresAt:a},userState:JSON.parse(n),isSignIn:!0,isUsingRefreshToken:this.isUsingRefreshToken,refresh:void 0};if(this.isUsingRefreshToken&&o&&i){var u=new Date(i);return f(f({},s),{refresh:{token:o,expiresAt:u}})}return f(f({},s),{refresh:null})}catch(e){return{auth:null,refresh:null,userState:null,isUsingRefreshToken:this.isUsingRefreshToken,isSignIn:!1}}},e.prototype.syncTokens=function(e){e.auth?this.isUsingRefreshToken&&e.refresh?this.setToken(e.auth.token,e.auth.type,e.refresh.token,e.refresh.expiresAt,e.auth.expiresAt,e.userState):this.setToken(e.auth.token,e.auth.type,null,null,e.auth.expiresAt,e.userState):this.removeToken()},e.prototype.setToken=function(e,t,r,n,o,i){"cookie"===this.authStorageType?this.setCookieToken_(e,t,r,o,n,i):this.setLSToken_(e,t,r,o,n,i)},e.prototype.setCookieToken_=function(e,t,r,n,o,i){u.default.set(this.authStorageName,e,{expires:n,domain:this.cookieDomain,secure:this.cookieSecure}),u.default.set(this.authStorageTypeName,t,{expires:n,domain:this.cookieDomain,secure:this.cookieSecure}),u.default.set(this.authTimeStorageName,n.toISOString(),{expires:n,domain:this.cookieDomain,secure:this.cookieSecure}),i&&u.default.set(this.stateStorageName,JSON.stringify(i),{expires:n,domain:this.cookieDomain,secure:this.cookieSecure}),this.isUsingRefreshToken&&this.refreshTokenName&&r&&u.default.set(this.refreshTokenName,r,{expires:n,domain:this.cookieDomain,secure:this.cookieSecure}),this.isUsingRefreshToken&&this.refreshTokenTimeName&&o&&u.default.set(this.refreshTokenTimeName,o.toISOString(),{expires:n,domain:this.cookieDomain,secure:this.cookieSecure})},e.prototype.setLSToken_=function(e,t,r,n,o,i){localStorage.setItem(this.authStorageName,e),localStorage.setItem(this.authStorageTypeName,t),localStorage.setItem(this.authTimeStorageName,n.toISOString()),i&&localStorage.setItem(this.stateStorageName,JSON.stringify(i)),this.isUsingRefreshToken&&this.refreshTokenName&&r&&localStorage.setItem(this.refreshTokenName,r),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(){u.default.remove(this.authStorageName,{domain:this.cookieDomain,secure:this.cookieSecure}),u.default.remove(this.authTimeStorageName,{domain:this.cookieDomain,secure:this.cookieSecure}),u.default.remove(this.authStorageTypeName,{domain:this.cookieDomain,secure:this.cookieSecure}),u.default.remove(this.stateStorageName,{domain:this.cookieDomain,secure:this.cookieSecure}),this.isUsingRefreshToken&&this.refreshTokenName&&u.default.remove(this.refreshTokenName,{domain:this.cookieDomain,secure:this.cookieSecure}),this.isUsingRefreshToken&&this.refreshTokenTimeName&&u.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.authStorageTypeName),localStorage.removeItem(this.stateStorageName),this.isUsingRefreshToken&&this.refreshTokenName&&localStorage.removeItem(this.refreshTokenName),this.isUsingRefreshToken&&this.refreshTokenTimeName&&localStorage.removeItem(this.refreshTokenTimeName)},e}();function m(e,t){switch(t.type){case a.SignIn:return f(f({},e),{auth:t.payload.auth,refresh:t.payload.refresh,userState:t.payload.userState,isSignIn:!0});case a.SignOut:return f(f({},e),{auth:null,refresh:null,userState:null,isSignIn:!1});case a.RefreshToken:return e.isSignIn&&e.auth&&e.refresh?f(f({},e),{auth:{token:t.payload.newAuthToken?t.payload.newAuthToken:e.auth.token,type:e.auth.type,expiresAt:t.payload.newAuthTokenExpireIn?new Date((new Date).getTime()+60*t.payload.newAuthTokenExpireIn*1e3):e.auth.expiresAt},refresh:{token:t.payload.newRefreshToken?t.payload.newRefreshToken:e.refresh.token,expiresAt:t.payload.newRefreshTokenExpiresIn?new Date((new Date).getTime()+60*t.payload.newRefreshTokenExpiresIn*1e3):e.refresh.expiresAt},userState:t.payload.newAuthUserState?t.payload.newAuthUserState:e.userState}):e}}function k(e){return{type:a.SignIn,payload:e}}function p(){return{type:a.SignOut}}!function(e){e[e.SignIn=0]="SignIn",e[e.SignOut=1]="SignOut",e[e.RefreshToken=2]="RefreshToken"}(a||(a={}));var d=function(e){var t=e.children,r=e.authType,n=e.authName,o=e.cookieDomain,i=e.cookieSecure,u=e.refresh;if("cookie"===r&&!o)throw new Error("authType 'cookie' requires 'cookieDomain' and 'cookieSecure' props in AuthProvider");var c,f,k,p,d=u?"".concat(n,"_refresh"):null,S=new l(n,r,d,o,i),T=s.useReducer(m,S.initialToken()),g=T[0],v=T[1];return u&&(c=function(){var e,t,r,n;u.refreshApiCallback({authToken:null===(e=g.auth)||void 0===e?void 0:e.token,authTokenExpireAt:null===(t=g.auth)||void 0===t?void 0:t.expiresAt,authUserState:g.userState,refreshToken:null===(r=g.refresh)||void 0===r?void 0:r.token,refreshTokenExpiresAt:null===(n=g.refresh)||void 0===n?void 0:n.expiresAt}).then((function(e){var t;e.isSuccess&&v((t=e,{type:a.RefreshToken,payload:t}))}))},f=g.isSignIn?u.interval:null,k=s.useRef(c),p=s.useRef(null),s.useEffect((function(){k.current=c}),[c]),s.useEffect((function(){return"number"==typeof f&&(p.current=window.setInterval((function(){return k.current()}),60*f*1e3)),function(){p.current&&window.clearTimeout(p.current)}}),[f])),s.useEffect((function(){S.syncTokens(g)}),[g]),s.createElement(h.Provider,{value:{authState:g,dispatch:v}},t)};d.defaultProps={cookieDomain:window.location.hostname,cookieSecure:"https:"===window.location.protocol},e.AuthProvider=d,e.RequireAuth=function(e){var t=e.children,r=e.loginPath,o=s.useContext(h);if(null===o)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");var i=n.useLocation();return o.authState.auth&&new Date(o.authState.auth.expiresAt)>new Date||(o.dispatch(p()),0)?t:s.createElement(n.Navigate,{to:r,state:{from:i},replace:!0})},e.createRefresh=function(e){return e} | ||
/*! react-auth-kit v2.8.2 | 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,r,n){"use strict";function o(e){var t=Object.create(null);return e&&Object.keys(e).forEach((function(r){if("default"!==r){var n=Object.getOwnPropertyDescriptor(e,r);Object.defineProperty(t,r,n.get?n:{enumerable:!0,get:function(){return e[r]}})}})),t.default=e,Object.freeze(t)}var i,a=o(t),s=a.createContext(null),u=s.Consumer,h=function(){return h=Object.assign||function(e){for(var t,r=1,n=arguments.length;r<n;r++)for(var o in t=arguments[r])Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o]);return e},h.apply(this,arguments)},c=function(){function e(e,t,r,n,o){this.authStorageType=t,this.authStorageName=e,this.authTimeStorageName="".concat(e,"_storage"),this.stateStorageName="".concat(e,"_state"),this.refreshTokenName=r,this.cookieDomain=n,this.cookieSecure=o,this.authStorageTypeName="".concat(this.authStorageName,"_type"),this.isUsingRefreshToken=!!this.refreshTokenName,this.refreshTokenTimeName=this.refreshTokenName?"".concat(this.refreshTokenName,"_time"):null}return e.prototype.initialToken=function(){return"cookie"===this.authStorageType?this.initialCookieToken_():this.initialLSToken_()},e.prototype.initialCookieToken_=function(){var e=r.get(this.authStorageName),t=r.get(this.authStorageTypeName),n=r.get(this.authTimeStorageName),o=r.get(this.stateStorageName),i=this.isUsingRefreshToken&&null!=this.refreshTokenName?r.get(this.refreshTokenName):null,a=this.isUsingRefreshToken&&null!=this.refreshTokenTimeName?r.get(this.refreshTokenTimeName):null;return this.checkTokenExist(e,t,n,o,i,a)},e.prototype.initialLSToken_=function(){var e=localStorage.getItem(this.authStorageName),t=localStorage.getItem(this.authStorageTypeName),r=localStorage.getItem(this.authTimeStorageName),n=localStorage.getItem(this.stateStorageName),o=this.isUsingRefreshToken&&null!=this.refreshTokenName?localStorage.getItem(this.refreshTokenName):null,i=this.isUsingRefreshToken&&null!=this.refreshTokenTimeName?localStorage.getItem(this.refreshTokenTimeName):null;return this.checkTokenExist(e,t,r,n,o,i)},e.prototype.checkTokenExist=function(e,t,r,n,o,i){if(!(e&&t&&r&&n))return{auth:null,refresh:null,userState:null,isUsingRefreshToken:this.isUsingRefreshToken,isSignIn:!1};var a=new Date(r);try{var s={auth:{token:e,type:t,expiresAt:a},userState:JSON.parse(n),isSignIn:!0,isUsingRefreshToken:this.isUsingRefreshToken,refresh:void 0};if(this.isUsingRefreshToken&&o&&i){var u=new Date(i);return h(h({},s),{refresh:{token:o,expiresAt:u}})}return h(h({},s),{refresh:null})}catch(e){return{auth:null,refresh:null,userState:null,isUsingRefreshToken:this.isUsingRefreshToken,isSignIn:!1}}},e.prototype.syncTokens=function(e){e.auth?this.isUsingRefreshToken&&e.refresh?this.setToken(e.auth.token,e.auth.type,e.refresh.token,e.refresh.expiresAt,e.auth.expiresAt,e.userState):this.setToken(e.auth.token,e.auth.type,null,null,e.auth.expiresAt,e.userState):this.removeToken()},e.prototype.setToken=function(e,t,r,n,o,i){"cookie"===this.authStorageType?this.setCookieToken_(e,t,r,o,n,i):this.setLSToken_(e,t,r,o,n,i)},e.prototype.setCookieToken_=function(e,t,n,o,i,a){r.set(this.authStorageName,e,{expires:o,domain:this.cookieDomain,secure:this.cookieSecure}),r.set(this.authStorageTypeName,t,{expires:o,domain:this.cookieDomain,secure:this.cookieSecure}),r.set(this.authTimeStorageName,o.toISOString(),{expires:o,domain:this.cookieDomain,secure:this.cookieSecure}),a&&r.set(this.stateStorageName,JSON.stringify(a),{expires:o,domain:this.cookieDomain,secure:this.cookieSecure}),this.isUsingRefreshToken&&this.refreshTokenName&&n&&r.set(this.refreshTokenName,n,{expires:o,domain:this.cookieDomain,secure:this.cookieSecure}),this.isUsingRefreshToken&&this.refreshTokenTimeName&&i&&r.set(this.refreshTokenTimeName,i.toISOString(),{expires:o,domain:this.cookieDomain,secure:this.cookieSecure})},e.prototype.setLSToken_=function(e,t,r,n,o,i){localStorage.setItem(this.authStorageName,e),localStorage.setItem(this.authStorageTypeName,t),localStorage.setItem(this.authTimeStorageName,n.toISOString()),i&&localStorage.setItem(this.stateStorageName,JSON.stringify(i)),this.isUsingRefreshToken&&this.refreshTokenName&&r&&localStorage.setItem(this.refreshTokenName,r),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(){r.remove(this.authStorageName,{domain:this.cookieDomain,secure:this.cookieSecure}),r.remove(this.authTimeStorageName,{domain:this.cookieDomain,secure:this.cookieSecure}),r.remove(this.authStorageTypeName,{domain:this.cookieDomain,secure:this.cookieSecure}),r.remove(this.stateStorageName,{domain:this.cookieDomain,secure:this.cookieSecure}),this.isUsingRefreshToken&&this.refreshTokenName&&r.remove(this.refreshTokenName,{domain:this.cookieDomain,secure:this.cookieSecure}),this.isUsingRefreshToken&&this.refreshTokenTimeName&&r.remove(this.refreshTokenTimeName,{domain:this.cookieDomain,secure:this.cookieSecure})},e.prototype.removeLSToken_=function(){localStorage.removeItem(this.authStorageName),localStorage.removeItem(this.authTimeStorageName),localStorage.removeItem(this.authStorageTypeName),localStorage.removeItem(this.stateStorageName),this.isUsingRefreshToken&&this.refreshTokenName&&localStorage.removeItem(this.refreshTokenName),this.isUsingRefreshToken&&this.refreshTokenTimeName&&localStorage.removeItem(this.refreshTokenTimeName)},e}(); | ||
/** | ||
* @author Arkadip Bhattacharya <in2arkadipb13@gmail.com> | ||
* @fileoverview Auth Context | ||
* @copyright Arkadip Bhattacharya 2020 | ||
* | ||
* Copyright 2020 Arkadip Bhattacharya | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/function f(e,t){switch(t.type){case i.SignIn:return h(h({},e),{auth:t.payload.auth,refresh:t.payload.refresh,userState:t.payload.userState,isSignIn:!0});case i.SignOut:return h(h({},e),{auth:null,refresh:null,userState:null,isSignIn:!1});case i.RefreshToken:return e.isSignIn&&e.auth&&e.refresh?h(h({},e),{auth:{token:t.payload.newAuthToken?t.payload.newAuthToken:e.auth.token,type:e.auth.type,expiresAt:t.payload.newAuthTokenExpireIn?new Date((new Date).getTime()+60*t.payload.newAuthTokenExpireIn*1e3):e.auth.expiresAt},refresh:{token:t.payload.newRefreshToken?t.payload.newRefreshToken:e.refresh.token,expiresAt:t.payload.newRefreshTokenExpiresIn?new Date((new Date).getTime()+60*t.payload.newRefreshTokenExpiresIn*1e3):e.refresh.expiresAt},userState:t.payload.newAuthUserState?t.payload.newAuthUserState:e.userState}):e}}function l(e){return{type:i.SignIn,payload:e}}function m(){return{type:i.SignOut}}!function(e){e[e.SignIn=0]="SignIn",e[e.SignOut=1]="SignOut",e[e.RefreshToken=2]="RefreshToken"}(i||(i={}));var k=function(e){var t=e.children,r=e.authType,n=e.authName,o=e.cookieDomain,u=e.cookieSecure,h=e.refresh;if("cookie"===r&&!o)throw new Error("authType 'cookie' requires 'cookieDomain' and 'cookieSecure' props in AuthProvider");var l,m,k,p,S=h?"".concat(n,"_refresh"):null,T=new c(n,r,S,o,u),g=a.useReducer(f,T.initialToken()),d=g[0],v=g[1];return h&&(l=function(){var e,t,r,n;h.refreshApiCallback({authToken:null===(e=d.auth)||void 0===e?void 0:e.token,authTokenExpireAt:null===(t=d.auth)||void 0===t?void 0:t.expiresAt,authUserState:d.userState,refreshToken:null===(r=d.refresh)||void 0===r?void 0:r.token,refreshTokenExpiresAt:null===(n=d.refresh)||void 0===n?void 0:n.expiresAt}).then((function(e){var t;e.isSuccess&&v((t=e,{type:i.RefreshToken,payload:t}))}))},m=d.isSignIn?h.interval:null,k=a.useRef(l),p=a.useRef(null),a.useEffect((function(){k.current=l}),[l]),a.useEffect((function(){return"number"==typeof m&&(p.current=window.setInterval((function(){return k.current()}),60*m*1e3)),function(){p.current&&window.clearTimeout(p.current)}}),[m])),a.useEffect((function(){T.syncTokens(d)}),[d]),a.createElement(s.Provider,{value:{authState:d,dispatch:v}},t)};k.defaultProps={cookieDomain:window.location.hostname,cookieSecure:"https:"===window.location.protocol},e.AuthProvider=k,e.RequireAuth=function(e){var t=e.children,r=e.loginPath,o=a.useContext(s);if(null===o)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");var i=n.useLocation();return o.authState.auth&&new Date(o.authState.auth.expiresAt)>new Date||(o.dispatch(m()),0)?t:a.createElement(n.Navigate,{to:r,state:{from:i},replace:!0})},e.createRefresh=function(e){return e} | ||
/** | ||
* @author Arkadip Bhattacharya <in2arkadipb13@gmail.com> | ||
* @fileoverview PrivateRoute component | ||
@@ -41,3 +60,3 @@ * @copyright Arkadip Bhattacharya 2020 | ||
*/ | ||
function(){var e=s.useContext(h);if(null===e)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return function(){return e.authState.auth?"".concat(e.authState.auth.type," ").concat(e.authState.auth.token):""}} | ||
function(){var e=a.useContext(s);if(null===e)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return function(){return e.authState.auth?"".concat(e.authState.auth.type," ").concat(e.authState.auth.token):""}} | ||
/** | ||
@@ -81,3 +100,3 @@ * @author Arkadip Bhattacharya <in2arkadipb13@gmail.com> | ||
*/ | ||
function(){var e=s.useContext(h);if(null===e)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return function(){return e.authState.auth?e.authState.userState:null}},e.useIsAuthenticated=function(){var e=s.useContext(h);if(null===e)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return function(){return!!e.authState.auth&&(new Date(e.authState.auth.expiresAt)>new Date||(e.dispatch(p()),!1))}},e.useSignIn= | ||
function(){var e=a.useContext(s);if(null===e)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return function(){return e.authState.auth?e.authState.userState:null}},e.useIsAuthenticated=function(){var e=a.useContext(s);if(null===e)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return function(){return!!e.authState.auth&&(new Date(e.authState.auth.expiresAt)>new Date||(e.dispatch(m()),!1))}},e.useSignIn= | ||
/** | ||
@@ -103,3 +122,3 @@ *@author Arkadip Bhattacharya <in2arkadipb13@gmail.com> | ||
*/ | ||
function(){var e=s.useContext(h);if(null===e)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return function(t){var r=t.token,n=t.tokenType,o=t.authState,i=t.expiresIn,a=t.refreshToken,s=t.refreshTokenExpireIn,u=new Date((new Date).getTime()+60*i*1e3);if(e.authState.isUsingRefreshToken){if(a&&s){var h=new Date((new Date).getTime()+60*s*1e3);return e.dispatch(k({auth:{token:r,type:n,expiresAt:u},userState:o||null,refresh:{token:a,expiresAt:h}})),!0}throw new Error('Make sure you given "refreshToken" and "refreshTokenExpireIn" parameter')}if(a&&s)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.dispatch(k({auth:{token:r,type:n,expiresAt:u},userState:o||null,refresh:null})),!0}} | ||
function(){var e=a.useContext(s);if(null===e)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return function(t){var r=t.token,n=t.tokenType,o=t.authState,i=t.expiresIn,a=t.refreshToken,s=t.refreshTokenExpireIn,u=new Date((new Date).getTime()+60*i*1e3);if(e.authState.isUsingRefreshToken){if(a&&s){var h=new Date((new Date).getTime()+60*s*1e3);return e.dispatch(l({auth:{token:r,type:n,expiresAt:u},userState:o||null,refresh:{token:a,expiresAt:h}})),!0}throw new Error('Make sure you given "refreshToken" and "refreshTokenExpireIn" parameter')}if(a&&s)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.dispatch(l({auth:{token:r,type:n,expiresAt:u},userState:o||null,refresh:null})),!0}} | ||
/** | ||
@@ -123,3 +142,3 @@ * @author Arkadip Bhattacharya <in2arkadipb13@gmail.com> | ||
* limitations under the License. | ||
*/,e.useSignOut=function(){var e=s.useContext(h);if(null===e)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return function(){try{return!!e&&(e.dispatch(p()),!0)}catch(e){return!1}}},e.withAuthHeader= | ||
*/,e.useSignOut=function(){var e=a.useContext(s);if(null===e)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return function(){try{return!!e&&(e.dispatch(m()),!0)}catch(e){return!1}}},e.withAuthHeader= | ||
/** | ||
@@ -144,3 +163,3 @@ * @author Arkadip Bhattacharya <in2arkadipb13@gmail.com> | ||
*/ | ||
function(e){return function(t){return s.createElement(c,null,(function(r){if(null===r)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return r.authState.auth?s.createElement(e,f({},t,{authHeader:"".concat(r.authState.auth.type," ").concat(r.authState.auth.token)})):s.createElement(e,f({},t,{authHeader:""}))}))}} | ||
function(e){return function(t){return a.createElement(u,null,(function(r){if(null===r)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return r.authState.auth?a.createElement(e,h({},t,{authHeader:"".concat(r.authState.auth.type," ").concat(r.authState.auth.token)})):a.createElement(e,h({},t,{authHeader:""}))}))}} | ||
/** | ||
@@ -164,3 +183,3 @@ * @author Arkadip Bhattacharya <in2arkadipb13@gmail.com> | ||
* limitations under the License. | ||
*/,e.withAuthUser=function(e){return function(t){return s.createElement(c,null,(function(r){if(null===r)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return r.authState.auth?s.createElement(e,f({},t,{authState:r.authState.userState})):s.createElement(e,f({},t,{authState:null}))}))}},e.withIsAuthenticated=function(e){return function(t){return s.createElement(c,null,(function(r){if(null===r)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return r.authState.auth?new Date(r.authState.auth.expiresAt)>new Date?s.createElement(e,f({},t,{isAuth:!0})):(r.dispatch(p()),s.createElement(e,f({},t,{isAuth:!1}))):s.createElement(e,f({},t,{isAuth:!1}))}))}},e.withSignIn=function(e){return function(t){return s.createElement(c,null,(function(r){if(null===r)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return s.createElement(e,f({},t,{signIn:function(e){var t=e.token,n=e.tokenType,o=e.authState,i=e.expiresIn,a=e.refreshToken,s=e.refreshTokenExpireIn,u=new Date((new Date).getTime()+60*i*1e3);if(r.authState.isUsingRefreshToken){if(a&&s){var h=new Date((new Date).getTime()+60*s*1e3);return r.dispatch(k({auth:{token:t,type:n,expiresAt:u},userState:o||null,refresh:{token:a,expiresAt:h}})),!0}throw new Error('Make sure you given "refreshToken" and "refreshTokenExpireIn" parameter')}if(a&&s)throw new Error("The app doesn't implement 'refreshToken' feature.\n So you have to implement refresh token feature from 'AuthProvider' before using it.");return r.dispatch(k({auth:{token:t,type:n,expiresAt:u},userState:o||null,refresh:null})),!0}}))}))}},e.withSignOut=function(e){return function(t){return s.createElement(c,null,(function(r){if(null===r)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return s.createElement(e,f({},t,{signOut:function(){try{return!!r&&(r.dispatch(p()),!0)}catch(e){return!1}}}))}))}},Object.defineProperty(e,"__esModule",{value:!0})})); | ||
*/,e.withAuthUser=function(e){return function(t){return a.createElement(u,null,(function(r){if(null===r)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return r.authState.auth?a.createElement(e,h({},t,{authState:r.authState.userState})):a.createElement(e,h({},t,{authState:null}))}))}},e.withIsAuthenticated=function(e){return function(t){return a.createElement(u,null,(function(r){if(null===r)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return r.authState.auth?new Date(r.authState.auth.expiresAt)>new Date?a.createElement(e,h({},t,{isAuth:!0})):(r.dispatch(m()),a.createElement(e,h({},t,{isAuth:!1}))):a.createElement(e,h({},t,{isAuth:!1}))}))}},e.withSignIn=function(e){return function(t){return a.createElement(u,null,(function(r){if(null===r)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return a.createElement(e,h({},t,{signIn:function(e){var t=e.token,n=e.tokenType,o=e.authState,i=e.expiresIn,a=e.refreshToken,s=e.refreshTokenExpireIn,u=new Date((new Date).getTime()+60*i*1e3);if(r.authState.isUsingRefreshToken){if(a&&s){var h=new Date((new Date).getTime()+60*s*1e3);return r.dispatch(l({auth:{token:t,type:n,expiresAt:u},userState:o||null,refresh:{token:a,expiresAt:h}})),!0}throw new Error('Make sure you given "refreshToken" and "refreshTokenExpireIn" parameter')}if(a&&s)throw new Error("The app doesn't implement 'refreshToken' feature.\n So you have to implement refresh token feature from 'AuthProvider' before using it.");return r.dispatch(l({auth:{token:t,type:n,expiresAt:u},userState:o||null,refresh:null})),!0}}))}))}},e.withSignOut=function(e){return function(t){return a.createElement(u,null,(function(r){if(null===r)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return a.createElement(e,h({},t,{signOut:function(){try{return!!r&&(r.dispatch(m()),!0)}catch(e){return!1}}}))}))}}})); | ||
//# sourceMappingURL=index.umd.js.map |
{ | ||
"name": "react-auth-kit", | ||
"version": "2.8.1", | ||
"version": "2.8.2", | ||
"description": "Authentication Library for React, which makes Token based auth very easy", | ||
@@ -65,3 +65,3 @@ "source": "src/index.tsx", | ||
"@testing-library/react": "14.0.0", | ||
"@types/jest": "27.5.2", | ||
"@types/jest": "29.4.0", | ||
"@types/js-cookie": "3.0.3", | ||
@@ -79,3 +79,4 @@ "@types/node": "18.14.1", | ||
"husky": "8.0.3", | ||
"jest": "27.5.1", | ||
"jest": "29.4.3", | ||
"jest-environment-jsdom": "^29.4.3", | ||
"lint-staged": "13.1.2", | ||
@@ -87,3 +88,3 @@ "markdownlint-cli": "0.33.0", | ||
"rimraf": "4.1.2", | ||
"rollup": "2.79.1", | ||
"rollup": "3.17.3", | ||
"rollup-plugin-filesize": "9.1.2", | ||
@@ -93,3 +94,3 @@ "rollup-plugin-license": "3.0.1", | ||
"rollup-plugin-typescript2": "0.34.1", | ||
"ts-jest": "27.1.5", | ||
"ts-jest": "29.0.5", | ||
"tslib": "2.5.0", | ||
@@ -96,0 +97,0 @@ "typedoc": "0.23.25", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
414690
3319
38