react-auth-kit
Advanced tools
Comparing version 1.4.12 to 1.4.13-0
@@ -7,2 +7,6 @@ # Changelog | ||
## [Unreleased] | ||
### Changed: | ||
- Auth state now uses `useReducer` instead of `useState` | ||
## [1.4.12] - 2021-07-25 | ||
@@ -9,0 +13,0 @@ ### Changed: |
@@ -7,3 +7,3 @@ /** | ||
*/ | ||
import { signInFunctionParams } from '../types'; | ||
import { AuthStateUserObject, signInFunctionParams } from '../types'; | ||
/** | ||
@@ -19,3 +19,3 @@ *@public | ||
isAuthenticated: () => (boolean); | ||
authUser: () => (object | null); | ||
authUser: () => (AuthStateUserObject | null); | ||
}; | ||
@@ -22,0 +22,0 @@ /** |
@@ -5,3 +5,2 @@ /** | ||
*@copyright Arkadip Bhattacharya 2020 | ||
*@license Apache-2.0 | ||
*/ | ||
@@ -12,3 +11,3 @@ /** | ||
*@name useSignOut | ||
*@description Signout Hook | ||
*@description Sign out Hook | ||
*/ | ||
@@ -15,0 +14,0 @@ declare function useSignOut(): () => (boolean); |
@@ -1,2 +0,2 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react"),t=require("js-cookie"),r=require("react-router-dom");function n(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}function o(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 i=o(e),a=n(t),u=i.createContext(null),h=u.Consumer,s=function(){function e(e){var t=e.authStorageName,r=e.authStorageType,n=e.authTimeStorageName,o=e.stateStorageName,i=e.refreshTokenName,a=e.cookieDomain,u=e.cookieSecure;this.authStorageType=r,this.authStorageName=t,this.authTimeStorageName=n,this.stateStorageName=o,this.refreshTokenName=i,this.cookieDomain=a,this.cookieSecure=u,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=a.default.get(this.authStorageName),t=a.default.get(this.authStorageTypeName),r=a.default.get(this.authTimeStorageName),n=a.default.get(this.stateStorageName),o=this.isUsingRefreshToken&&null!=this.refreshTokenName?a.default.get(this.refreshTokenName):null,i=this.isUsingRefreshToken&&null!=this.refreshTokenTimeName?a.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{authToken:null,authTokenType:null,isUsingRefreshToken:this.isUsingRefreshToken,refreshToken:null,expireAt:null,authState:null,refreshTokenExpireAt:null};var a=new Date(r);try{var u=JSON.parse(n);return{authToken:e,authTokenType:t,isUsingRefreshToken:this.isUsingRefreshToken,refreshToken:this.isUsingRefreshToken&&o?o:null,refreshTokenExpireAt:this.isUsingRefreshToken&&i?new Date(i):null,expireAt:a,authState:u}}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,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){a.default.set(this.authStorageName,e,{expires:n,domain:this.cookieDomain,secure:this.cookieSecure}),a.default.set(this.authStorageTypeName,t,{expires:n,domain:this.cookieDomain,secure:this.cookieSecure}),a.default.set(this.authTimeStorageName,n.toISOString(),{expires:n,domain:this.cookieDomain,secure:this.cookieSecure}),a.default.set(this.stateStorageName,i,{expires:n,domain:this.cookieDomain,secure:this.cookieSecure}),this.isUsingRefreshToken&&this.refreshTokenName&&r&&a.default.set(this.refreshTokenName,r,{expires:n,domain:this.cookieDomain,secure:this.cookieSecure}),this.isUsingRefreshToken&&this.refreshTokenTimeName&&o&&a.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()),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(){a.default.remove(this.authStorageName,{domain:this.cookieDomain,secure:this.cookieSecure}),a.default.remove(this.authTimeStorageName,{domain:this.cookieDomain,secure:this.cookieSecure}),a.default.remove(this.stateStorageName,{domain:this.cookieDomain,secure:this.cookieSecure}),this.isUsingRefreshToken&&this.refreshTokenName&&a.default.remove(this.refreshTokenName,{domain:this.cookieDomain,secure:this.cookieSecure}),this.isUsingRefreshToken&&this.refreshTokenTimeName&&a.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}(),l=function(e){var t=e.children,r=e.authType,n=e.authName,o=e.refreshToken,a=e.cookieDomain,h=e.cookieSecure;if("cookie"===r&&!a)throw new Error("authType 'cookie' requires 'cookieDomain' and 'cookieSecure' props in AuthProvider");var l=new s({authTimeStorageName:n+"_time",authStorageType:r,authStorageName:n,refreshTokenName:o?n+"_refresh":void 0,cookieDomain:a,cookieSecure:h,stateStorageName:n+"_state"}),f=i.useState(l.initialToken()),c=f[0],T=f[1];return i.useEffect((function(){l.syncTokens(c)}),[c]),i.createElement(u.Provider,{value:{authState:c,setAuthState:T}},t)},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}).apply(this,arguments)};var c=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 r=new Date((new Date).getTime()+60*t*1e3);this.context.setAuthState((function(t){return f(f({},t),{refreshToken:e,refreshTokenExpireAt:r})}))},e.prototype.updateAuthState=function(e,t,r){var n={authToken:e};if(void 0!==t&&Object.assign(n,{authTokenType:t}),void 0!==r){var o=new Date((new Date).getTime()+60*r*1e3);Object.assign(n,{expireAt:o})}this.context.setAuthState((function(e){return f(f({},e),n)}))},e.prototype.updateUserState=function(e){this.context.setAuthState((function(t){return f(f({},t),{authState:e})}))},e}();l.defaultProps={cookieDomain:window.location.hostname,cookieSecure:"https:"===window.location.protocol},exports.AuthProvider=l,exports.PrivateRoute=function(e){var t=i.useContext(u);if(null===t)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");var n=e.component,o=e.loginPath,a=e.strict,h=e.sensitive,s=e.exact,l=e.path,c=e.location,T=e.render;return i.createElement(r.Route,{location:c,path:l,exact:s,sensitive:h,strict:a,render:function(e){return t.authState.authToken&&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?i.createElement(n,e):T?T(e):null:i.createElement(r.Redirect,{to:o})}})},exports.useAuth= | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react"),t=require("js-cookie"),r=require("react-router-dom");function o(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}function n(e){if(e&&e.__esModule)return e;var t=Object.create(null);return e&&Object.keys(e).forEach((function(r){if("default"!==r){var o=Object.getOwnPropertyDescriptor(e,r);Object.defineProperty(t,r,o.get?o:{enumerable:!0,get:function(){return e[r]}})}})),t.default=e,Object.freeze(t)}var i,a=n(e),u=o(t),s=a.createContext(null),h=s.Consumer,l=function(){function e(e){var t=e.authStorageName,r=e.authStorageType,o=e.authTimeStorageName,n=e.stateStorageName,i=e.refreshTokenName,a=e.cookieDomain,u=e.cookieSecure;this.authStorageType=r,this.authStorageName=t,this.authTimeStorageName=o,this.stateStorageName=n,this.refreshTokenName=i,this.cookieDomain=a,this.cookieSecure=u,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=u.default.get(this.authStorageName),t=u.default.get(this.authStorageTypeName),r=u.default.get(this.authTimeStorageName),o=u.default.get(this.stateStorageName),n=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,o,n,i)},e.prototype.initialLSToken_=function(){var e=localStorage.getItem(this.authStorageName),t=localStorage.getItem(this.authStorageTypeName),r=localStorage.getItem(this.authTimeStorageName),o=localStorage.getItem(this.stateStorageName),n=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,o,n,i)},e.prototype.checkTokenExist=function(e,t,r,o,n,i){if(!(e&&t&&r&&o))return{authToken:null,authTokenType:null,isUsingRefreshToken:this.isUsingRefreshToken,refreshToken:null,expireAt:null,authState:null,refreshTokenExpireAt:null};var a=new Date(r);try{var u=JSON.parse(o);return{authToken:e,authTokenType:t,isUsingRefreshToken:this.isUsingRefreshToken,refreshToken:this.isUsingRefreshToken&&n?n:null,refreshTokenExpireAt:this.isUsingRefreshToken&&i?new Date(i):null,expireAt:a,authState:u}}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,r,o,n,i){"cookie"===this.authStorageType?this.setCookieToken_(e,t,r,n,o,i):this.setLSToken_(e,t,r,n,o,i)},e.prototype.setCookieToken_=function(e,t,r,o,n,i){u.default.set(this.authStorageName,e,{expires:o,domain:this.cookieDomain,secure:this.cookieSecure}),u.default.set(this.authStorageTypeName,t,{expires:o,domain:this.cookieDomain,secure:this.cookieSecure}),u.default.set(this.authTimeStorageName,o.toISOString(),{expires:o,domain:this.cookieDomain,secure:this.cookieSecure}),u.default.set(this.stateStorageName,i,{expires:o,domain:this.cookieDomain,secure:this.cookieSecure}),this.isUsingRefreshToken&&this.refreshTokenName&&r&&u.default.set(this.refreshTokenName,r,{expires:o,domain:this.cookieDomain,secure:this.cookieSecure}),this.isUsingRefreshToken&&this.refreshTokenTimeName&&n&&u.default.set(this.refreshTokenTimeName,n.toISOString(),{expires:o,domain:this.cookieDomain,secure:this.cookieSecure})},e.prototype.setLSToken_=function(e,t,r,o,n,i){localStorage.setItem(this.authStorageName,e),localStorage.setItem(this.authStorageTypeName,t),localStorage.setItem(this.authTimeStorageName,o.toISOString()),localStorage.setItem(this.stateStorageName,JSON.stringify(i)),this.isUsingRefreshToken&&this.refreshTokenName&&r&&localStorage.setItem(this.refreshTokenName,r),this.isUsingRefreshToken&&this.refreshTokenTimeName&&n&&localStorage.setItem(this.refreshTokenTimeName,n.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.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.stateStorageName),this.isUsingRefreshToken&&this.refreshTokenName&&localStorage.removeItem(this.refreshTokenName),this.isUsingRefreshToken&&this.refreshTokenTimeName&&localStorage.removeItem(this.refreshTokenTimeName)},e}(),f=function(){return(f=Object.assign||function(e){for(var t,r=1,o=arguments.length;r<o;r++)for(var n in t=arguments[r])Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n]);return e}).apply(this,arguments)};function c(e,t){switch(t.type){case i.SignIn:return f(f({},e),{authToken:t.payload.authToken,authTokenType:t.payload.authTokenType,expireAt:t.payload.expireAt,authState:t.payload.authState,refreshToken:t.payload.refreshToken?t.payload.refreshToken:null,refreshTokenExpireAt:t.payload.refreshTokenExpireAt});case i.SignOut:return f(f({},e),{authToken:null,authTokenType:null,expireAt:null,authState:null,refreshToken:null,refreshTokenExpireAt:null});default:return e}}function m(e){return{type:i.SignIn,payload:e}}function T(){return{type:i.SignOut}}!function(e){e[e.SignIn=0]="SignIn",e[e.SignOut=1]="SignOut"}(i||(i={}));var k=function(e){var t=e.children,r=e.authType,o=e.authName,n=e.refreshToken,i=e.cookieDomain,u=e.cookieSecure;if("cookie"===r&&!i)throw new Error("authType 'cookie' requires 'cookieDomain' and 'cookieSecure' props in AuthProvider");var h=new l({authTimeStorageName:o+"_time",authStorageType:r,authStorageName:o,refreshTokenName:n?o+"_refresh":void 0,cookieDomain:i,cookieSecure:u,stateStorageName:o+"_state"}),f=a.useReducer(c,h.initialToken()),m=f[0],T=f[1];return a.useEffect((function(){h.syncTokens(m)}),[m]),a.createElement(s.Provider,{value:{authState:m,dispatch:T}},t)};var p=function(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.")};k.defaultProps={cookieDomain:window.location.hostname,cookieSecure:"https:"===window.location.protocol},exports.AuthProvider=k,exports.PrivateRoute=function(e){var t=a.useContext(s);if(null===t)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");var o=e.component,n=e.loginPath,i=e.strict,u=e.sensitive,h=e.exact,l=e.path,f=e.location,c=e.render;return a.createElement(r.Route,{location:f,path:l,exact:h,sensitive:u,strict:i,render:function(e){return t.authState.authToken&&t.authState.expireAt&&(new Date(t.authState.expireAt)>new Date||(t.dispatch(T()),0))?o?a.createElement(o,e):c?c(e):null:a.createElement(r.Redirect,{to:n})}})},exports.useAuth= | ||
/** | ||
@@ -8,3 +8,3 @@ *@author Arkadip Bhattacharya <in2arkadipb13@gmail.com> | ||
*/ | ||
function(){var e=i.useContext(u);if(null===e)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");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 r=t.token,n=t.tokenType,o=t.authState,i=t.expiresIn,a=t.refreshToken,u=t.refreshTokenExpireIn;if((a||u)&&!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*i*1e3),s=u?new Date((new Date).getTime()+60*u*1e3):null;try{return!!e&&(e.setAuthState((function(e){return f(f({},e),{authToken:r,authTokenType:n,expireAt:h,authState:o,refreshToken:a||null,refreshTokenExpireAt:s})})),!0)}catch(e){return console.error(e),!1}}}} | ||
function(){var e=a.useContext(s);if(null===e)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");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.dispatch(T()),!1))},authUser:function(){return e.authState.authState},signOut:function(){try{return!!(null==e?void 0:e.authState.authToken)&&(e.dispatch(T()),console.log("RAJ :: Signing Out"),!0)}catch(e){return!1}},signIn:function(t){var r=t.token,o=t.tokenType,n=t.authState,i=t.expiresIn,a=t.refreshToken,u=t.refreshTokenExpireIn;if((a||u)&&!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 s=new Date((new Date).getTime()+60*i*1e3),h=u?new Date((new Date).getTime()+60*u*1e3):null;try{return!!e&&(e.dispatch(m({authToken:r,authTokenType:o,expireAt:s,authState:n,refreshToken:a||null,refreshTokenExpireAt:h})),!0)}catch(e){return console.error(e),!1}}}} | ||
/** | ||
@@ -15,3 +15,3 @@ *@author Arkadip Bhattacharya <in2arkadipb13@gmail.com> | ||
*@license Apache-2.0 | ||
*/,exports.useAuthHeader=function(){var e=i.useContext(u);if(null===e)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return function(){return(null==e?void 0:e.authState)?e.authState.authTokenType+" "+e.authState.authToken:"Bearer "}},exports.useAuthUser=function(){var e=i.useContext(u);if(null===e)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return function(){return e.authState.authState}},exports.useIsAuthenticated= | ||
*/,exports.useAuthHeader=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(null==e?void 0:e.authState)?e.authState.authTokenType+" "+e.authState.authToken:"Bearer "}},exports.useAuthUser=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.authState}},exports.useIsAuthenticated= | ||
/** | ||
@@ -23,11 +23,5 @@ *@author Arkadip Bhattacharya <in2arkadipb13@gmail.com> | ||
*/ | ||
function(){var e=i.useContext(u);if(null===e)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");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))}},exports.useRefreshToken=function(){var e=i.useContext(u);if(null===e)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return new c(e)},exports.useSignIn=function(){var e=i.useContext(u);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,u=t.refreshTokenExpireIn;if((a||u)&&!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*i*1e3),s=u?new Date((new Date).getTime()+60*u*1e3):null;try{return!!e&&(e.setAuthState((function(e){return f(f({},e),{authToken:r,authTokenType:n,expireAt:h,authState:o,refreshToken:a||null,refreshTokenExpireAt:s})})),!0)}catch(e){return console.error(e),!1}}} | ||
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!(!(null==e?void 0:e.authState.authToken)||!(null==e?void 0:e.authState.expireAt))&&(new Date(e.authState.expireAt)>new Date||(e.dispatch(T()),!1))}},exports.useRefreshToken=function(){var e=a.useContext(s);if(null===e)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return new p(e)},exports.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(t){var r=t.token,o=t.tokenType,n=t.authState,i=t.expiresIn,a=t.refreshToken,u=t.refreshTokenExpireIn;if((a||u)&&!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 s=new Date((new Date).getTime()+60*i*1e3),h=u?new Date((new Date).getTime()+60*u*1e3):null;try{return!!e&&(e.dispatch(m({authToken:r,authTokenType:o,expireAt:s,authState:n,refreshToken:a||null,refreshTokenExpireAt:h})),!0)}catch(e){return console.error(e),!1}}},exports.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(T()),!0)}catch(e){return!1}}},exports.withAuthHeader= | ||
/** | ||
*@author Arkadip Bhattacharya <in2arkadipb13@gmail.com> | ||
*@fileoverview Sign Out functionality | ||
*@copyright Arkadip Bhattacharya 2020 | ||
*@license Apache-2.0 | ||
*/,exports.useSignOut=function(){var e=i.useContext(u);if(null===e)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");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}}},exports.withAuthHeader= | ||
/** | ||
*@author Arkadip Bhattacharya <in2arkadipb13@gmail.com> | ||
*@fileoverview Authentication header | ||
@@ -37,3 +31,3 @@ *@copyright Arkadip Bhattacharya 2020 | ||
*/ | ||
function(e){return function(t){return i.createElement(h,null,(function(r){if(null===r)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return(null==r?void 0:r.authState)?i.createElement(e,f({},t,{authHeader:r.authState.authTokenType+" "+r.authState.authToken})):i.createElement(e,f({},t,{authHeader:null}))}))}} | ||
function(e){return function(t){return a.createElement(h,null,(function(r){if(null===r)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return(null==r?void 0:r.authState)?a.createElement(e,f({},t,{authHeader:r.authState.authTokenType+" "+r.authState.authToken})):a.createElement(e,f({},t,{authHeader:null}))}))}} | ||
/** | ||
@@ -44,3 +38,3 @@ *@author Arkadip Bhattacharya <in2arkadipb13@gmail.com> | ||
*@license Apache-2.0 | ||
*/,exports.withAuthUser=function(e){return function(t){return i.createElement(h,null,(function(r){if(null===r)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return i.createElement(e,f({},t,{authState:null==r?void 0:r.authState.authState}))}))}},exports.withIsAuthenticated= | ||
*/,exports.withAuthUser=function(e){return function(t){return a.createElement(h,null,(function(r){if(null===r)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return a.createElement(e,f({},t,{authState:null==r?void 0:r.authState.authState}))}))}},exports.withIsAuthenticated= | ||
/** | ||
@@ -52,3 +46,3 @@ *@author Arkadip Bhattacharya <in2arkadipb13@gmail.com> | ||
*/ | ||
function(e){return function(t){return i.createElement(h,null,(function(r){if(null===r)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return(null==r?void 0:r.authState.authToken)&&(null==r?void 0:r.authState.expireAt)?new Date(r.authState.expireAt)>new Date?i.createElement(e,f({},t,{isAuth:!0})):(r.setAuthState((function(e){return f(f({},e),{authToken:null,authTokenType:null,expireAt:null,authState:null,refreshToken:null,refreshTokenExpireAt:null})})),i.createElement(e,f({},t,{isAuth:!1}))):i.createElement(e,f({},t,{isAuth:!1}))}))}},exports.withRefreshToken=function(e){return function(t){return i.createElement(h,null,(function(r){if(null===r)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return i.createElement(e,f({},t,{refreshToken:new c(r)}))}))}},exports.withSignIn=function(e){return function(t){return i.createElement(h,null,(function(r){if(null===r)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return i.createElement(e,f({},t,{signIn:function(e){var t=e.token,n=e.tokenType,o=e.authState,i=e.expiresIn,a=e.refreshToken,u=e.refreshTokenExpireIn;if((a||u)&&!r.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*i*1e3),s=u?new Date((new Date).getTime()+60*u*1e3):null;try{return!!r&&(r.setAuthState((function(e){return f(f({},e),{authToken:t,authTokenType:n,expireAt:h,authState:o,refreshToken:a||null,refreshTokenExpireAt:s})})),!0)}catch(e){return console.error(e),!1}}}))}))}},exports.withSignOut=function(e){return function(t){return i.createElement(h,null,(function(r){if(null===r)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return i.createElement(e,f({},t,{signOut:function(){try{return!!(null==r?void 0:r.authState.authToken)&&(r.setAuthState((function(e){return f(f({},e),{authToken:null,authTokenType:null,expireAt:null,authState:null,refreshToken:null,refreshTokenExpireAt:null})})),!0)}catch(e){return!1}}}))}))}}; | ||
function(e){return function(t){return a.createElement(h,null,(function(r){if(null===r)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return(null==r?void 0:r.authState.authToken)&&(null==r?void 0:r.authState.expireAt)?new Date(r.authState.expireAt)>new Date?a.createElement(e,f({},t,{isAuth:!0})):(r.dispatch(T()),a.createElement(e,f({},t,{isAuth:!1}))):a.createElement(e,f({},t,{isAuth:!1}))}))}},exports.withRefreshToken=function(e){return function(t){return a.createElement(h,null,(function(r){if(null===r)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return a.createElement(e,f({},t,{refreshToken:new p(r)}))}))}},exports.withSignIn=function(e){return function(t){return a.createElement(h,null,(function(r){if(null===r)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return a.createElement(e,f({},t,{signIn:function(e){var t=e.token,o=e.tokenType,n=e.authState,i=e.expiresIn,a=e.refreshToken,u=e.refreshTokenExpireIn;if((a||u)&&!r.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 s=new Date((new Date).getTime()+60*i*1e3),h=u?new Date((new Date).getTime()+60*u*1e3):null;try{return!!r&&(r.dispatch(m({authToken:t,authTokenType:o,expireAt:s,authState:n,refreshToken:a||null,refreshTokenExpireAt:h})),!0)}catch(e){return console.error(e),!1}}}))}))}},exports.withSignOut=function(e){return function(t){return a.createElement(h,null,(function(r){if(null===r)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return a.createElement(e,f({},t,{signOut:function(){try{return!!r&&(r.dispatch(T()),!0)}catch(e){return!1}}}))}))}}; | ||
//# sourceMappingURL=index.js.map |
@@ -1,2 +0,2 @@ | ||
import*as e from"react";import t from"js-cookie";import{Route as r,Redirect as n}from"react-router-dom";var o=e.createContext(null),i=o.Consumer,a=function(){function e(e){var t=e.authStorageName,r=e.authStorageType,n=e.authTimeStorageName,o=e.stateStorageName,i=e.refreshTokenName,a=e.cookieDomain,u=e.cookieSecure;this.authStorageType=r,this.authStorageName=t,this.authTimeStorageName=n,this.stateStorageName=o,this.refreshTokenName=i,this.cookieDomain=a,this.cookieSecure=u,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=t.get(this.authStorageName),r=t.get(this.authStorageTypeName),n=t.get(this.authTimeStorageName),o=t.get(this.stateStorageName),i=this.isUsingRefreshToken&&null!=this.refreshTokenName?t.get(this.refreshTokenName):null,a=this.isUsingRefreshToken&&null!=this.refreshTokenTimeName?t.get(this.refreshTokenTimeName):null;return this.checkTokenExist(e,r,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{authToken:null,authTokenType:null,isUsingRefreshToken:this.isUsingRefreshToken,refreshToken:null,expireAt:null,authState:null,refreshTokenExpireAt:null};var a=new Date(r);try{var u=JSON.parse(n);return{authToken:e,authTokenType:t,isUsingRefreshToken:this.isUsingRefreshToken,refreshToken:this.isUsingRefreshToken&&o?o:null,refreshTokenExpireAt:this.isUsingRefreshToken&&i?new Date(i):null,expireAt:a,authState:u}}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,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,r,n,o,i,a){t.set(this.authStorageName,e,{expires:o,domain:this.cookieDomain,secure:this.cookieSecure}),t.set(this.authStorageTypeName,r,{expires:o,domain:this.cookieDomain,secure:this.cookieSecure}),t.set(this.authTimeStorageName,o.toISOString(),{expires:o,domain:this.cookieDomain,secure:this.cookieSecure}),t.set(this.stateStorageName,a,{expires:o,domain:this.cookieDomain,secure:this.cookieSecure}),this.isUsingRefreshToken&&this.refreshTokenName&&n&&t.set(this.refreshTokenName,n,{expires:o,domain:this.cookieDomain,secure:this.cookieSecure}),this.isUsingRefreshToken&&this.refreshTokenTimeName&&i&&t.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()),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(){t.remove(this.authStorageName,{domain:this.cookieDomain,secure:this.cookieSecure}),t.remove(this.authTimeStorageName,{domain:this.cookieDomain,secure:this.cookieSecure}),t.remove(this.stateStorageName,{domain:this.cookieDomain,secure:this.cookieSecure}),this.isUsingRefreshToken&&this.refreshTokenName&&t.remove(this.refreshTokenName,{domain:this.cookieDomain,secure:this.cookieSecure}),this.isUsingRefreshToken&&this.refreshTokenTimeName&&t.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}(),u=function(t){var r=t.children,n=t.authType,i=t.authName,u=t.refreshToken,h=t.cookieDomain,s=t.cookieSecure;if("cookie"===n&&!h)throw new Error("authType 'cookie' requires 'cookieDomain' and 'cookieSecure' props in AuthProvider");var l=new a({authTimeStorageName:i+"_time",authStorageType:n,authStorageName:i,refreshTokenName:u?i+"_refresh":void 0,cookieDomain:h,cookieSecure:s,stateStorageName:i+"_state"}),T=e.useState(l.initialToken()),f=T[0],c=T[1];return e.useEffect((function(){l.syncTokens(f)}),[f]),e.createElement(o.Provider,{value:{authState:f,setAuthState:c}},r)},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}).apply(this,arguments)},s=function(t){var i=e.useContext(o);if(null===i)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");var a=t.component,u=t.loginPath,s=t.strict,l=t.sensitive,T=t.exact,f=t.path,c=t.location,k=t.render;return e.createElement(r,{location:c,path:f,exact:T,sensitive:l,strict:s,render:function(t){return i.authState.authToken&&i.authState.expireAt&&(new Date(i.authState.expireAt)>new Date||(i.setAuthState((function(e){return h(h({},e),{authToken:null,authTokenType:null,expireAt:null,authState:null,refreshToken:null,refreshTokenExpireAt:null})})),0))?a?e.createElement(a,t):k?k(t):null:e.createElement(n,{to:u})}})}; | ||
import*as e from"react";import t from"js-cookie";import{Route as r,Redirect as o}from"react-router-dom";var n,i=e.createContext(null),a=i.Consumer,h=function(){function e(e){var t=e.authStorageName,r=e.authStorageType,o=e.authTimeStorageName,n=e.stateStorageName,i=e.refreshTokenName,a=e.cookieDomain,h=e.cookieSecure;this.authStorageType=r,this.authStorageName=t,this.authTimeStorageName=o,this.stateStorageName=n,this.refreshTokenName=i,this.cookieDomain=a,this.cookieSecure=h,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=t.get(this.authStorageName),r=t.get(this.authStorageTypeName),o=t.get(this.authTimeStorageName),n=t.get(this.stateStorageName),i=this.isUsingRefreshToken&&null!=this.refreshTokenName?t.get(this.refreshTokenName):null,a=this.isUsingRefreshToken&&null!=this.refreshTokenTimeName?t.get(this.refreshTokenTimeName):null;return this.checkTokenExist(e,r,o,n,i,a)},e.prototype.initialLSToken_=function(){var e=localStorage.getItem(this.authStorageName),t=localStorage.getItem(this.authStorageTypeName),r=localStorage.getItem(this.authTimeStorageName),o=localStorage.getItem(this.stateStorageName),n=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,o,n,i)},e.prototype.checkTokenExist=function(e,t,r,o,n,i){if(!(e&&t&&r&&o))return{authToken:null,authTokenType:null,isUsingRefreshToken:this.isUsingRefreshToken,refreshToken:null,expireAt:null,authState:null,refreshTokenExpireAt:null};var a=new Date(r);try{var h=JSON.parse(o);return{authToken:e,authTokenType:t,isUsingRefreshToken:this.isUsingRefreshToken,refreshToken:this.isUsingRefreshToken&&n?n:null,refreshTokenExpireAt:this.isUsingRefreshToken&&i?new Date(i):null,expireAt:a,authState:h}}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,r,o,n,i){"cookie"===this.authStorageType?this.setCookieToken_(e,t,r,n,o,i):this.setLSToken_(e,t,r,n,o,i)},e.prototype.setCookieToken_=function(e,r,o,n,i,a){t.set(this.authStorageName,e,{expires:n,domain:this.cookieDomain,secure:this.cookieSecure}),t.set(this.authStorageTypeName,r,{expires:n,domain:this.cookieDomain,secure:this.cookieSecure}),t.set(this.authTimeStorageName,n.toISOString(),{expires:n,domain:this.cookieDomain,secure:this.cookieSecure}),t.set(this.stateStorageName,a,{expires:n,domain:this.cookieDomain,secure:this.cookieSecure}),this.isUsingRefreshToken&&this.refreshTokenName&&o&&t.set(this.refreshTokenName,o,{expires:n,domain:this.cookieDomain,secure:this.cookieSecure}),this.isUsingRefreshToken&&this.refreshTokenTimeName&&i&&t.set(this.refreshTokenTimeName,i.toISOString(),{expires:n,domain:this.cookieDomain,secure:this.cookieSecure})},e.prototype.setLSToken_=function(e,t,r,o,n,i){localStorage.setItem(this.authStorageName,e),localStorage.setItem(this.authStorageTypeName,t),localStorage.setItem(this.authTimeStorageName,o.toISOString()),localStorage.setItem(this.stateStorageName,JSON.stringify(i)),this.isUsingRefreshToken&&this.refreshTokenName&&r&&localStorage.setItem(this.refreshTokenName,r),this.isUsingRefreshToken&&this.refreshTokenTimeName&&n&&localStorage.setItem(this.refreshTokenTimeName,n.toISOString())},e.prototype.removeToken=function(){"cookie"===this.authStorageType?this.removeCookieToken_():this.removeLSToken_()},e.prototype.removeCookieToken_=function(){t.remove(this.authStorageName,{domain:this.cookieDomain,secure:this.cookieSecure}),t.remove(this.authTimeStorageName,{domain:this.cookieDomain,secure:this.cookieSecure}),t.remove(this.stateStorageName,{domain:this.cookieDomain,secure:this.cookieSecure}),this.isUsingRefreshToken&&this.refreshTokenName&&t.remove(this.refreshTokenName,{domain:this.cookieDomain,secure:this.cookieSecure}),this.isUsingRefreshToken&&this.refreshTokenTimeName&&t.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=function(){return(s=Object.assign||function(e){for(var t,r=1,o=arguments.length;r<o;r++)for(var n in t=arguments[r])Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n]);return e}).apply(this,arguments)};function u(e,t){switch(t.type){case n.SignIn:return s(s({},e),{authToken:t.payload.authToken,authTokenType:t.payload.authTokenType,expireAt:t.payload.expireAt,authState:t.payload.authState,refreshToken:t.payload.refreshToken?t.payload.refreshToken:null,refreshTokenExpireAt:t.payload.refreshTokenExpireAt});case n.SignOut:return s(s({},e),{authToken:null,authTokenType:null,expireAt:null,authState:null,refreshToken:null,refreshTokenExpireAt:null});default:return e}}function l(e){return{type:n.SignIn,payload:e}}function c(){return{type:n.SignOut}}!function(e){e[e.SignIn=0]="SignIn",e[e.SignOut=1]="SignOut"}(n||(n={}));var m=function(t){var r=t.children,o=t.authType,n=t.authName,a=t.refreshToken,s=t.cookieDomain,l=t.cookieSecure;if("cookie"===o&&!s)throw new Error("authType 'cookie' requires 'cookieDomain' and 'cookieSecure' props in AuthProvider");var c=new h({authTimeStorageName:n+"_time",authStorageType:o,authStorageName:n,refreshTokenName:a?n+"_refresh":void 0,cookieDomain:s,cookieSecure:l,stateStorageName:n+"_state"}),m=e.useReducer(u,c.initialToken()),f=m[0],T=m[1];return e.useEffect((function(){c.syncTokens(f)}),[f]),e.createElement(i.Provider,{value:{authState:f,dispatch:T}},r)},f=function(t){var n=e.useContext(i);if(null===n)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");var a=t.component,h=t.loginPath,s=t.strict,u=t.sensitive,l=t.exact,m=t.path,f=t.location,T=t.render;return e.createElement(r,{location:f,path:m,exact:l,sensitive:u,strict:s,render:function(t){return n.authState.authToken&&n.authState.expireAt&&(new Date(n.authState.expireAt)>new Date||(n.dispatch(c()),0))?a?e.createElement(a,t):T?T(t):null:e.createElement(o,{to:h})}})}; | ||
/** | ||
@@ -8,3 +8,3 @@ *@author Arkadip Bhattacharya <in2arkadipb13@gmail.com> | ||
*/ | ||
function l(){var t=e.useContext(o);if(null===t)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return{authHeader:function(){return(null==t?void 0:t.authState)?t.authState.authTokenType+" "+t.authState.authToken:"Bearer "},isAuthenticated:function(){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 h(h({},e),{authToken:null,authTokenType:null,expireAt:null,authState:null,refreshToken:null,refreshTokenExpireAt:null})})),!1))},authUser:function(){return t.authState.authState},signOut:function(){try{return!!(null==t?void 0:t.authState.authToken)&&(t.setAuthState((function(e){return h(h({},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(e){var r=e.token,n=e.tokenType,o=e.authState,i=e.expiresIn,a=e.refreshToken,u=e.refreshTokenExpireIn;if((a||u)&&!t.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 s=new Date((new Date).getTime()+60*i*1e3),l=u?new Date((new Date).getTime()+60*u*1e3):null;try{return!!t&&(t.setAuthState((function(e){return h(h({},e),{authToken:r,authTokenType:n,expireAt:s,authState:o,refreshToken:a||null,refreshTokenExpireAt:l})})),!0)}catch(e){return console.error(e),!1}}}} | ||
function T(){var t=e.useContext(i);if(null===t)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return{authHeader:function(){return(null==t?void 0:t.authState)?t.authState.authTokenType+" "+t.authState.authToken:"Bearer "},isAuthenticated:function(){return!(!(null==t?void 0:t.authState.authToken)||!(null==t?void 0:t.authState.expireAt))&&(new Date(t.authState.expireAt)>new Date||(t.dispatch(c()),!1))},authUser:function(){return t.authState.authState},signOut:function(){try{return!!(null==t?void 0:t.authState.authToken)&&(t.dispatch(c()),console.log("RAJ :: Signing Out"),!0)}catch(e){return!1}},signIn:function(e){var r=e.token,o=e.tokenType,n=e.authState,i=e.expiresIn,a=e.refreshToken,h=e.refreshTokenExpireIn;if((a||h)&&!t.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 s=new Date((new Date).getTime()+60*i*1e3),u=h?new Date((new Date).getTime()+60*h*1e3):null;try{return!!t&&(t.dispatch(l({authToken:r,authTokenType:o,expireAt:s,authState:n,refreshToken:a||null,refreshTokenExpireAt:u})),!0)}catch(e){return console.error(e),!1}}}} | ||
/** | ||
@@ -15,15 +15,9 @@ *@author Arkadip Bhattacharya <in2arkadipb13@gmail.com> | ||
*@license Apache-2.0 | ||
*/function T(){var t=e.useContext(o);if(null===t)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return function(e){var r=e.token,n=e.tokenType,o=e.authState,i=e.expiresIn,a=e.refreshToken,u=e.refreshTokenExpireIn;if((a||u)&&!t.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 s=new Date((new Date).getTime()+60*i*1e3),l=u?new Date((new Date).getTime()+60*u*1e3):null;try{return!!t&&(t.setAuthState((function(e){return h(h({},e),{authToken:r,authTokenType:n,expireAt:s,authState:o,refreshToken:a||null,refreshTokenExpireAt:l})})),!0)}catch(e){return console.error(e),!1}}} | ||
*/function k(){var t=e.useContext(i);if(null===t)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return function(e){var r=e.token,o=e.tokenType,n=e.authState,i=e.expiresIn,a=e.refreshToken,h=e.refreshTokenExpireIn;if((a||h)&&!t.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 s=new Date((new Date).getTime()+60*i*1e3),u=h?new Date((new Date).getTime()+60*h*1e3):null;try{return!!t&&(t.dispatch(l({authToken:r,authTokenType:o,expireAt:s,authState:n,refreshToken:a||null,refreshTokenExpireAt:u})),!0)}catch(e){return console.error(e),!1}}}function p(){var t=e.useContext(i);if(null===t)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return function(){try{return!!t&&(t.dispatch(c()),!0)}catch(e){return!1}}}function S(){var t=e.useContext(i);if(null===t)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return function(){return t.authState.authState}}function g(){var t=e.useContext(i);if(null===t)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return function(){return(null==t?void 0:t.authState)?t.authState.authTokenType+" "+t.authState.authToken:"Bearer "}}var d=function(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.")};function v(){var t=e.useContext(i);if(null===t)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return new d(t)} | ||
/** | ||
*@author Arkadip Bhattacharya <in2arkadipb13@gmail.com> | ||
*@fileoverview Sign Out functionality | ||
*@copyright Arkadip Bhattacharya 2020 | ||
*@license Apache-2.0 | ||
*/function f(){var t=e.useContext(o);if(null===t)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return function(){try{return!!(null==t?void 0:t.authState.authToken)&&(t.setAuthState((function(e){return h(h({},e),{authToken:null,authTokenType:null,expireAt:null,authState:null,refreshToken:null,refreshTokenExpireAt:null})})),console.log("RAJ :: Signing Out"),!0)}catch(e){return!1}}}function c(){var t=e.useContext(o);if(null===t)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return function(){return t.authState.authState}}function k(){var t=e.useContext(o);if(null===t)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return function(){return(null==t?void 0:t.authState)?t.authState.authTokenType+" "+t.authState.authToken:"Bearer "}}var m=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 r=new Date((new Date).getTime()+60*t*1e3);this.context.setAuthState((function(t){return h(h({},t),{refreshToken:e,refreshTokenExpireAt:r})}))},e.prototype.updateAuthState=function(e,t,r){var n={authToken:e};if(void 0!==t&&Object.assign(n,{authTokenType:t}),void 0!==r){var o=new Date((new Date).getTime()+60*r*1e3);Object.assign(n,{expireAt:o})}this.context.setAuthState((function(e){return h(h({},e),n)}))},e.prototype.updateUserState=function(e){this.context.setAuthState((function(t){return h(h({},t),{authState:e})}))},e}();function S(){var t=e.useContext(o);if(null===t)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return new m(t)} | ||
/** | ||
*@author Arkadip Bhattacharya <in2arkadipb13@gmail.com> | ||
*@fileoverview Check User is authenticated or not | ||
*@copyright Arkadip Bhattacharya 2020 | ||
*@license Apache-2.0 | ||
*/function p(){var t=e.useContext(o);if(null===t)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return function(){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 h(h({},e),{authToken:null,authTokenType:null,expireAt:null,authState:null,refreshToken:null,refreshTokenExpireAt:null})})),!1))}}function g(t){return function(r){return e.createElement(i,null,(function(n){if(null===n)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return e.createElement(t,h({},r,{signIn:function(e){var t=e.token,r=e.tokenType,o=e.authState,i=e.expiresIn,a=e.refreshToken,u=e.refreshTokenExpireIn;if((a||u)&&!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 s=new Date((new Date).getTime()+60*i*1e3),l=u?new Date((new Date).getTime()+60*u*1e3):null;try{return!!n&&(n.setAuthState((function(e){return h(h({},e),{authToken:t,authTokenType:r,expireAt:s,authState:o,refreshToken:a||null,refreshTokenExpireAt:l})})),!0)}catch(e){return console.error(e),!1}}}))}))}}function d(t){return function(r){return e.createElement(i,null,(function(n){if(null===n)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return e.createElement(t,h({},r,{signOut:function(){try{return!!(null==n?void 0:n.authState.authToken)&&(n.setAuthState((function(e){return h(h({},e),{authToken:null,authTokenType:null,expireAt:null,authState:null,refreshToken:null,refreshTokenExpireAt:null})})),!0)}catch(e){return!1}}}))}))}}function v(t){return function(r){return e.createElement(i,null,(function(n){if(null===n)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return e.createElement(t,h({},r,{authState:null==n?void 0:n.authState.authState}))}))}} | ||
*/function y(){var t=e.useContext(i);if(null===t)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return function(){return!(!(null==t?void 0:t.authState.authToken)||!(null==t?void 0:t.authState.expireAt))&&(new Date(t.authState.expireAt)>new Date||(t.dispatch(c()),!1))}}function w(t){return function(r){return e.createElement(a,null,(function(o){if(null===o)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return e.createElement(t,s({},r,{signIn:function(e){var t=e.token,r=e.tokenType,n=e.authState,i=e.expiresIn,a=e.refreshToken,h=e.refreshTokenExpireIn;if((a||h)&&!o.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 s=new Date((new Date).getTime()+60*i*1e3),u=h?new Date((new Date).getTime()+60*h*1e3):null;try{return!!o&&(o.dispatch(l({authToken:t,authTokenType:r,expireAt:s,authState:n,refreshToken:a||null,refreshTokenExpireAt:u})),!0)}catch(e){return console.error(e),!1}}}))}))}}function A(t){return function(r){return e.createElement(a,null,(function(o){if(null===o)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return e.createElement(t,s({},r,{signOut:function(){try{return!!o&&(o.dispatch(c()),!0)}catch(e){return!1}}}))}))}}function N(t){return function(r){return e.createElement(a,null,(function(o){if(null===o)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return e.createElement(t,s({},r,{authState:null==o?void 0:o.authState.authState}))}))}} | ||
/** | ||
@@ -34,3 +28,3 @@ *@author Arkadip Bhattacharya <in2arkadipb13@gmail.com> | ||
*@license Apache-2.0 | ||
*/function A(t){return function(r){return e.createElement(i,null,(function(n){if(null===n)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return(null==n?void 0:n.authState)?e.createElement(t,h({},r,{authHeader:n.authState.authTokenType+" "+n.authState.authToken})):e.createElement(t,h({},r,{authHeader:null}))}))}} | ||
*/function x(t){return function(r){return e.createElement(a,null,(function(o){if(null===o)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return(null==o?void 0:o.authState)?e.createElement(t,s({},r,{authHeader:o.authState.authTokenType+" "+o.authState.authToken})):e.createElement(t,s({},r,{authHeader:null}))}))}} | ||
/** | ||
@@ -41,3 +35,3 @@ *@author Arkadip Bhattacharya <in2arkadipb13@gmail.com> | ||
*@license Apache-2.0 | ||
*/function x(t){return function(r){return e.createElement(i,null,(function(n){if(null===n)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return e.createElement(t,h({},r,{refreshToken:new m(n)}))}))}} | ||
*/function E(t){return function(r){return e.createElement(a,null,(function(o){if(null===o)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return e.createElement(t,s({},r,{refreshToken:new d(o)}))}))}} | ||
/** | ||
@@ -48,3 +42,3 @@ *@author Arkadip Bhattacharya <in2arkadipb13@gmail.com> | ||
*@license Apache-2.0 | ||
*/function w(t){return function(r){return e.createElement(i,null,(function(n){if(null===n)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return(null==n?void 0:n.authState.authToken)&&(null==n?void 0:n.authState.expireAt)?new Date(n.authState.expireAt)>new Date?e.createElement(t,h({},r,{isAuth:!0})):(n.setAuthState((function(e){return h(h({},e),{authToken:null,authTokenType:null,expireAt:null,authState:null,refreshToken:null,refreshTokenExpireAt:null})})),e.createElement(t,h({},r,{isAuth:!1}))):e.createElement(t,h({},r,{isAuth:!1}))}))}}u.defaultProps={cookieDomain:window.location.hostname,cookieSecure:"https:"===window.location.protocol};export{u as AuthProvider,s as PrivateRoute,l as useAuth,k as useAuthHeader,c as useAuthUser,p as useIsAuthenticated,S as useRefreshToken,T as useSignIn,f as useSignOut,A as withAuthHeader,v as withAuthUser,w as withIsAuthenticated,x as withRefreshToken,g as withSignIn,d as withSignOut}; | ||
*/function P(t){return function(r){return e.createElement(a,null,(function(o){if(null===o)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return(null==o?void 0:o.authState.authToken)&&(null==o?void 0:o.authState.expireAt)?new Date(o.authState.expireAt)>new Date?e.createElement(t,s({},r,{isAuth:!0})):(o.dispatch(c()),e.createElement(t,s({},r,{isAuth:!1}))):e.createElement(t,s({},r,{isAuth:!1}))}))}}m.defaultProps={cookieDomain:window.location.hostname,cookieSecure:"https:"===window.location.protocol};export{m as AuthProvider,f as PrivateRoute,T as useAuth,g as useAuthHeader,S as useAuthUser,y as useIsAuthenticated,v as useRefreshToken,k as useSignIn,p as useSignOut,x as withAuthHeader,N as withAuthUser,P as withIsAuthenticated,E as withRefreshToken,w as withSignIn,A as withSignOut}; | ||
//# sourceMappingURL=index.modern.js.map |
@@ -1,3 +0,3 @@ | ||
/*! react-auth-kit v1.4.12 | 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 u=i(t),a=o(r),h=u.createContext(null),s=h.Consumer,l=function(){function e(e){var t=e.authStorageName,r=e.authStorageType,n=e.authTimeStorageName,o=e.stateStorageName,i=e.refreshTokenName,u=e.cookieDomain,a=e.cookieSecure;this.authStorageType=r,this.authStorageName=t,this.authTimeStorageName=n,this.stateStorageName=o,this.refreshTokenName=i,this.cookieDomain=u,this.cookieSecure=a,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=a.default.get(this.authStorageName),t=a.default.get(this.authStorageTypeName),r=a.default.get(this.authTimeStorageName),n=a.default.get(this.stateStorageName),o=this.isUsingRefreshToken&&null!=this.refreshTokenName?a.default.get(this.refreshTokenName):null,i=this.isUsingRefreshToken&&null!=this.refreshTokenTimeName?a.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{authToken:null,authTokenType:null,isUsingRefreshToken:this.isUsingRefreshToken,refreshToken:null,expireAt:null,authState:null,refreshTokenExpireAt:null};var u=new Date(r);try{var a=JSON.parse(n);return{authToken:e,authTokenType:t,isUsingRefreshToken:this.isUsingRefreshToken,refreshToken:this.isUsingRefreshToken&&o?o:null,refreshTokenExpireAt:this.isUsingRefreshToken&&i?new Date(i):null,expireAt:u,authState:a}}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,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){a.default.set(this.authStorageName,e,{expires:n,domain:this.cookieDomain,secure:this.cookieSecure}),a.default.set(this.authStorageTypeName,t,{expires:n,domain:this.cookieDomain,secure:this.cookieSecure}),a.default.set(this.authTimeStorageName,n.toISOString(),{expires:n,domain:this.cookieDomain,secure:this.cookieSecure}),a.default.set(this.stateStorageName,i,{expires:n,domain:this.cookieDomain,secure:this.cookieSecure}),this.isUsingRefreshToken&&this.refreshTokenName&&r&&a.default.set(this.refreshTokenName,r,{expires:n,domain:this.cookieDomain,secure:this.cookieSecure}),this.isUsingRefreshToken&&this.refreshTokenTimeName&&o&&a.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()),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(){a.default.remove(this.authStorageName,{domain:this.cookieDomain,secure:this.cookieSecure}),a.default.remove(this.authTimeStorageName,{domain:this.cookieDomain,secure:this.cookieSecure}),a.default.remove(this.stateStorageName,{domain:this.cookieDomain,secure:this.cookieSecure}),this.isUsingRefreshToken&&this.refreshTokenName&&a.default.remove(this.refreshTokenName,{domain:this.cookieDomain,secure:this.cookieSecure}),this.isUsingRefreshToken&&this.refreshTokenTimeName&&a.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}(),f=function(e){var t=e.children,r=e.authType,n=e.authName,o=e.refreshToken,i=e.cookieDomain,a=e.cookieSecure;if("cookie"===r&&!i)throw new Error("authType 'cookie' requires 'cookieDomain' and 'cookieSecure' props in AuthProvider");var s=new l({authTimeStorageName:n+"_time",authStorageType:r,authStorageName:n,refreshTokenName:o?n+"_refresh":void 0,cookieDomain:i,cookieSecure:a,stateStorageName:n+"_state"}),f=u.useState(s.initialToken()),c=f[0],T=f[1];return u.useEffect((function(){s.syncTokens(c)}),[c]),u.createElement(h.Provider,{value:{authState:c,setAuthState:T}},t)},c=function(){return(c=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}).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 r=new Date((new Date).getTime()+60*t*1e3);this.context.setAuthState((function(t){return c(c({},t),{refreshToken:e,refreshTokenExpireAt:r})}))},e.prototype.updateAuthState=function(e,t,r){var n={authToken:e};if(void 0!==t&&Object.assign(n,{authTokenType:t}),void 0!==r){var o=new Date((new Date).getTime()+60*r*1e3);Object.assign(n,{expireAt:o})}this.context.setAuthState((function(e){return c(c({},e),n)}))},e.prototype.updateUserState=function(e){this.context.setAuthState((function(t){return c(c({},t),{authState:e})}))},e}();f.defaultProps={cookieDomain:window.location.hostname,cookieSecure:"https:"===window.location.protocol},e.AuthProvider=f,e.PrivateRoute=function(e){var t=u.useContext(h);if(null===t)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");var r=e.component,o=e.loginPath,i=e.strict,a=e.sensitive,s=e.exact,l=e.path,f=e.location,T=e.render;return u.createElement(n.Route,{location:f,path:l,exact:s,sensitive:a,strict:i,render:function(e){return t.authState.authToken&&t.authState.expireAt&&(new Date(t.authState.expireAt)>new Date||(t.setAuthState((function(e){return c(c({},e),{authToken:null,authTokenType:null,expireAt:null,authState:null,refreshToken:null,refreshTokenExpireAt:null})})),0))?r?u.createElement(r,e):T?T(e):null:u.createElement(n.Redirect,{to:o})}})},e.useAuth= | ||
/*! react-auth-kit v1.4.13-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,r,o){"use strict";function n(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 o=Object.getOwnPropertyDescriptor(e,r);Object.defineProperty(t,r,o.get?o:{enumerable:!0,get:function(){return e[r]}})}})),t.default=e,Object.freeze(t)}var a,u=i(t),s=n(r),h=u.createContext(null),l=h.Consumer,f=function(){function e(e){var t=e.authStorageName,r=e.authStorageType,o=e.authTimeStorageName,n=e.stateStorageName,i=e.refreshTokenName,a=e.cookieDomain,u=e.cookieSecure;this.authStorageType=r,this.authStorageName=t,this.authTimeStorageName=o,this.stateStorageName=n,this.refreshTokenName=i,this.cookieDomain=a,this.cookieSecure=u,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=s.default.get(this.authStorageName),t=s.default.get(this.authStorageTypeName),r=s.default.get(this.authTimeStorageName),o=s.default.get(this.stateStorageName),n=this.isUsingRefreshToken&&null!=this.refreshTokenName?s.default.get(this.refreshTokenName):null,i=this.isUsingRefreshToken&&null!=this.refreshTokenTimeName?s.default.get(this.refreshTokenTimeName):null;return this.checkTokenExist(e,t,r,o,n,i)},e.prototype.initialLSToken_=function(){var e=localStorage.getItem(this.authStorageName),t=localStorage.getItem(this.authStorageTypeName),r=localStorage.getItem(this.authTimeStorageName),o=localStorage.getItem(this.stateStorageName),n=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,o,n,i)},e.prototype.checkTokenExist=function(e,t,r,o,n,i){if(!(e&&t&&r&&o))return{authToken:null,authTokenType:null,isUsingRefreshToken:this.isUsingRefreshToken,refreshToken:null,expireAt:null,authState:null,refreshTokenExpireAt:null};var a=new Date(r);try{var u=JSON.parse(o);return{authToken:e,authTokenType:t,isUsingRefreshToken:this.isUsingRefreshToken,refreshToken:this.isUsingRefreshToken&&n?n:null,refreshTokenExpireAt:this.isUsingRefreshToken&&i?new Date(i):null,expireAt:a,authState:u}}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,r,o,n,i){"cookie"===this.authStorageType?this.setCookieToken_(e,t,r,n,o,i):this.setLSToken_(e,t,r,n,o,i)},e.prototype.setCookieToken_=function(e,t,r,o,n,i){s.default.set(this.authStorageName,e,{expires:o,domain:this.cookieDomain,secure:this.cookieSecure}),s.default.set(this.authStorageTypeName,t,{expires:o,domain:this.cookieDomain,secure:this.cookieSecure}),s.default.set(this.authTimeStorageName,o.toISOString(),{expires:o,domain:this.cookieDomain,secure:this.cookieSecure}),s.default.set(this.stateStorageName,i,{expires:o,domain:this.cookieDomain,secure:this.cookieSecure}),this.isUsingRefreshToken&&this.refreshTokenName&&r&&s.default.set(this.refreshTokenName,r,{expires:o,domain:this.cookieDomain,secure:this.cookieSecure}),this.isUsingRefreshToken&&this.refreshTokenTimeName&&n&&s.default.set(this.refreshTokenTimeName,n.toISOString(),{expires:o,domain:this.cookieDomain,secure:this.cookieSecure})},e.prototype.setLSToken_=function(e,t,r,o,n,i){localStorage.setItem(this.authStorageName,e),localStorage.setItem(this.authStorageTypeName,t),localStorage.setItem(this.authTimeStorageName,o.toISOString()),localStorage.setItem(this.stateStorageName,JSON.stringify(i)),this.isUsingRefreshToken&&this.refreshTokenName&&r&&localStorage.setItem(this.refreshTokenName,r),this.isUsingRefreshToken&&this.refreshTokenTimeName&&n&&localStorage.setItem(this.refreshTokenTimeName,n.toISOString())},e.prototype.removeToken=function(){"cookie"===this.authStorageType?this.removeCookieToken_():this.removeLSToken_()},e.prototype.removeCookieToken_=function(){s.default.remove(this.authStorageName,{domain:this.cookieDomain,secure:this.cookieSecure}),s.default.remove(this.authTimeStorageName,{domain:this.cookieDomain,secure:this.cookieSecure}),s.default.remove(this.stateStorageName,{domain:this.cookieDomain,secure:this.cookieSecure}),this.isUsingRefreshToken&&this.refreshTokenName&&s.default.remove(this.refreshTokenName,{domain:this.cookieDomain,secure:this.cookieSecure}),this.isUsingRefreshToken&&this.refreshTokenTimeName&&s.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(){return(c=Object.assign||function(e){for(var t,r=1,o=arguments.length;r<o;r++)for(var n in t=arguments[r])Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n]);return e}).apply(this,arguments)};function m(e,t){switch(t.type){case a.SignIn:return c(c({},e),{authToken:t.payload.authToken,authTokenType:t.payload.authTokenType,expireAt:t.payload.expireAt,authState:t.payload.authState,refreshToken:t.payload.refreshToken?t.payload.refreshToken:null,refreshTokenExpireAt:t.payload.refreshTokenExpireAt});case a.SignOut:return c(c({},e),{authToken:null,authTokenType:null,expireAt:null,authState:null,refreshToken:null,refreshTokenExpireAt:null});default:return e}}function T(e){return{type:a.SignIn,payload:e}}function k(){return{type:a.SignOut}}!function(e){e[e.SignIn=0]="SignIn",e[e.SignOut=1]="SignOut"}(a||(a={}));var d=function(e){var t=e.children,r=e.authType,o=e.authName,n=e.refreshToken,i=e.cookieDomain,a=e.cookieSecure;if("cookie"===r&&!i)throw new Error("authType 'cookie' requires 'cookieDomain' and 'cookieSecure' props in AuthProvider");var s=new f({authTimeStorageName:o+"_time",authStorageType:r,authStorageName:o,refreshTokenName:n?o+"_refresh":void 0,cookieDomain:i,cookieSecure:a,stateStorageName:o+"_state"}),l=u.useReducer(m,s.initialToken()),c=l[0],T=l[1];return u.useEffect((function(){s.syncTokens(c)}),[c]),u.createElement(h.Provider,{value:{authState:c,dispatch:T}},t)};var p=function(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.")};d.defaultProps={cookieDomain:window.location.hostname,cookieSecure:"https:"===window.location.protocol},e.AuthProvider=d,e.PrivateRoute=function(e){var t=u.useContext(h);if(null===t)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");var r=e.component,n=e.loginPath,i=e.strict,a=e.sensitive,s=e.exact,l=e.path,f=e.location,c=e.render;return u.createElement(o.Route,{location:f,path:l,exact:s,sensitive:a,strict:i,render:function(e){return t.authState.authToken&&t.authState.expireAt&&(new Date(t.authState.expireAt)>new Date||(t.dispatch(k()),0))?r?u.createElement(r,e):c?c(e):null:u.createElement(o.Redirect,{to:n})}})},e.useAuth= | ||
/** | ||
@@ -9,3 +9,3 @@ *@author Arkadip Bhattacharya <in2arkadipb13@gmail.com> | ||
*/ | ||
function(){var e=u.useContext(h);if(null===e)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");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 c(c({},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 c(c({},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 r=t.token,n=t.tokenType,o=t.authState,i=t.expiresIn,u=t.refreshToken,a=t.refreshTokenExpireIn;if((u||a)&&!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*i*1e3),s=a?new Date((new Date).getTime()+60*a*1e3):null;try{return!!e&&(e.setAuthState((function(e){return c(c({},e),{authToken:r,authTokenType:n,expireAt:h,authState:o,refreshToken:u||null,refreshTokenExpireAt:s})})),!0)}catch(e){return console.error(e),!1}}}} | ||
function(){var e=u.useContext(h);if(null===e)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");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.dispatch(k()),!1))},authUser:function(){return e.authState.authState},signOut:function(){try{return!!(null==e?void 0:e.authState.authToken)&&(e.dispatch(k()),console.log("RAJ :: Signing Out"),!0)}catch(e){return!1}},signIn:function(t){var r=t.token,o=t.tokenType,n=t.authState,i=t.expiresIn,a=t.refreshToken,u=t.refreshTokenExpireIn;if((a||u)&&!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 s=new Date((new Date).getTime()+60*i*1e3),h=u?new Date((new Date).getTime()+60*u*1e3):null;try{return!!e&&(e.dispatch(T({authToken:r,authTokenType:o,expireAt:s,authState:n,refreshToken:a||null,refreshTokenExpireAt:h})),!0)}catch(e){return console.error(e),!1}}}} | ||
/** | ||
@@ -23,11 +23,5 @@ *@author Arkadip Bhattacharya <in2arkadipb13@gmail.com> | ||
*/ | ||
function(){var e=u.useContext(h);if(null===e)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");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 c(c({},e),{authToken:null,authTokenType:null,expireAt:null,authState:null,refreshToken:null,refreshTokenExpireAt:null})})),!1))}},e.useRefreshToken=function(){var e=u.useContext(h);if(null===e)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return new T(e)},e.useSignIn=function(){var e=u.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,u=t.refreshToken,a=t.refreshTokenExpireIn;if((u||a)&&!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*i*1e3),s=a?new Date((new Date).getTime()+60*a*1e3):null;try{return!!e&&(e.setAuthState((function(e){return c(c({},e),{authToken:r,authTokenType:n,expireAt:h,authState:o,refreshToken:u||null,refreshTokenExpireAt:s})})),!0)}catch(e){return console.error(e),!1}}} | ||
function(){var e=u.useContext(h);if(null===e)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");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.dispatch(k()),!1))}},e.useRefreshToken=function(){var e=u.useContext(h);if(null===e)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return new p(e)},e.useSignIn=function(){var e=u.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,o=t.tokenType,n=t.authState,i=t.expiresIn,a=t.refreshToken,u=t.refreshTokenExpireIn;if((a||u)&&!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 s=new Date((new Date).getTime()+60*i*1e3),h=u?new Date((new Date).getTime()+60*u*1e3):null;try{return!!e&&(e.dispatch(T({authToken:r,authTokenType:o,expireAt:s,authState:n,refreshToken:a||null,refreshTokenExpireAt:h})),!0)}catch(e){return console.error(e),!1}}},e.useSignOut=function(){var e=u.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(k()),!0)}catch(e){return!1}}},e.withAuthHeader= | ||
/** | ||
*@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(h);if(null===e)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return function(){try{return!!(null==e?void 0:e.authState.authToken)&&(e.setAuthState((function(e){return c(c({},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 | ||
@@ -37,3 +31,3 @@ *@copyright Arkadip Bhattacharya 2020 | ||
*/ | ||
function(e){return function(t){return u.createElement(s,null,(function(r){if(null===r)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return(null==r?void 0:r.authState)?u.createElement(e,c({},t,{authHeader:r.authState.authTokenType+" "+r.authState.authToken})):u.createElement(e,c({},t,{authHeader:null}))}))}} | ||
function(e){return function(t){return u.createElement(l,null,(function(r){if(null===r)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return(null==r?void 0:r.authState)?u.createElement(e,c({},t,{authHeader:r.authState.authTokenType+" "+r.authState.authToken})):u.createElement(e,c({},t,{authHeader:null}))}))}} | ||
/** | ||
@@ -44,3 +38,3 @@ *@author Arkadip Bhattacharya <in2arkadipb13@gmail.com> | ||
*@license Apache-2.0 | ||
*/,e.withAuthUser=function(e){return function(t){return u.createElement(s,null,(function(r){if(null===r)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return u.createElement(e,c({},t,{authState:null==r?void 0:r.authState.authState}))}))}},e.withIsAuthenticated= | ||
*/,e.withAuthUser=function(e){return function(t){return u.createElement(l,null,(function(r){if(null===r)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return u.createElement(e,c({},t,{authState:null==r?void 0:r.authState.authState}))}))}},e.withIsAuthenticated= | ||
/** | ||
@@ -52,3 +46,3 @@ *@author Arkadip Bhattacharya <in2arkadipb13@gmail.com> | ||
*/ | ||
function(e){return function(t){return u.createElement(s,null,(function(r){if(null===r)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return(null==r?void 0:r.authState.authToken)&&(null==r?void 0:r.authState.expireAt)?new Date(r.authState.expireAt)>new Date?u.createElement(e,c({},t,{isAuth:!0})):(r.setAuthState((function(e){return c(c({},e),{authToken:null,authTokenType:null,expireAt:null,authState:null,refreshToken:null,refreshTokenExpireAt:null})})),u.createElement(e,c({},t,{isAuth:!1}))):u.createElement(e,c({},t,{isAuth:!1}))}))}},e.withRefreshToken=function(e){return function(t){return u.createElement(s,null,(function(r){if(null===r)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return u.createElement(e,c({},t,{refreshToken:new T(r)}))}))}},e.withSignIn=function(e){return function(t){return u.createElement(s,null,(function(r){if(null===r)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return u.createElement(e,c({},t,{signIn:function(e){var t=e.token,n=e.tokenType,o=e.authState,i=e.expiresIn,u=e.refreshToken,a=e.refreshTokenExpireIn;if((u||a)&&!r.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*i*1e3),s=a?new Date((new Date).getTime()+60*a*1e3):null;try{return!!r&&(r.setAuthState((function(e){return c(c({},e),{authToken:t,authTokenType:n,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(r){if(null===r)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return u.createElement(e,c({},t,{signOut:function(){try{return!!(null==r?void 0:r.authState.authToken)&&(r.setAuthState((function(e){return c(c({},e),{authToken:null,authTokenType:null,expireAt:null,authState:null,refreshToken:null,refreshTokenExpireAt:null})})),!0)}catch(e){return!1}}}))}))}},Object.defineProperty(e,"__esModule",{value:!0})})); | ||
function(e){return function(t){return u.createElement(l,null,(function(r){if(null===r)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return(null==r?void 0:r.authState.authToken)&&(null==r?void 0:r.authState.expireAt)?new Date(r.authState.expireAt)>new Date?u.createElement(e,c({},t,{isAuth:!0})):(r.dispatch(k()),u.createElement(e,c({},t,{isAuth:!1}))):u.createElement(e,c({},t,{isAuth:!1}))}))}},e.withRefreshToken=function(e){return function(t){return u.createElement(l,null,(function(r){if(null===r)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return u.createElement(e,c({},t,{refreshToken:new p(r)}))}))}},e.withSignIn=function(e){return function(t){return u.createElement(l,null,(function(r){if(null===r)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return u.createElement(e,c({},t,{signIn:function(e){var t=e.token,o=e.tokenType,n=e.authState,i=e.expiresIn,a=e.refreshToken,u=e.refreshTokenExpireIn;if((a||u)&&!r.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 s=new Date((new Date).getTime()+60*i*1e3),h=u?new Date((new Date).getTime()+60*u*1e3):null;try{return!!r&&(r.dispatch(T({authToken:t,authTokenType:o,expireAt:s,authState:n,refreshToken:a||null,refreshTokenExpireAt:h})),!0)}catch(e){return console.error(e),!1}}}))}))}},e.withSignOut=function(e){return function(t){return u.createElement(l,null,(function(r){if(null===r)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return u.createElement(e,c({},t,{signOut:function(){try{return!!r&&(r.dispatch(k()),!0)}catch(e){return!1}}}))}))}},Object.defineProperty(e,"__esModule",{value:!0})})); | ||
//# sourceMappingURL=index.umd.js.map |
@@ -14,50 +14,3 @@ import { AuthContextInterface } from './types'; | ||
constructor(context: AuthContextInterface); | ||
/** | ||
* Get the current Refresh Token | ||
* @returns refresh token | ||
*/ | ||
getCurrentRefreshToken(): { | ||
refreshToken: string | null; | ||
refreshTokenExpireAt: Date | null; | ||
}; | ||
/** | ||
* Get the Current Auth State | ||
* @returns a object with | ||
* authState: The Auth Token, | ||
* authTokenType: type of auth token, | ||
* expireAt: expire time | ||
*/ | ||
getCurrentAuthState(): { | ||
authToken: string | null; | ||
authTokenType: string | null; | ||
expireAt: Date | null; | ||
}; | ||
/** | ||
* Get the Current User State | ||
* @returns User State {object} | ||
*/ | ||
getCurrentUserState(): object | null; | ||
/** | ||
* Updates the Current Refresh Token | ||
* | ||
* @param refreshToken - new refresh Token | ||
* @param expiresIn | ||
*/ | ||
updateRefreshToken(refreshToken: string, expiresIn: number): void; | ||
/** | ||
* updates the AuthState | ||
* @param authToken - The Updated authToken | ||
* @param authTokenType - The updated authType (optional) | ||
* @param expiresIn - The updated expiresIn in minutes. (optional) | ||
* | ||
* If the new authToken has different expire time, | ||
* then you must have to update the expiresIn param | ||
*/ | ||
updateAuthState(authToken: string, authTokenType?: string, expiresIn?: number): void; | ||
/** | ||
* Updates the Auth User's state | ||
* @param userState | ||
*/ | ||
updateUserState(userState: object): void; | ||
} | ||
export default RefreshToken; |
@@ -1,2 +0,2 @@ | ||
import { TokenInterface, TokenObjectParamsInterface } from './types'; | ||
import { AuthStateUserObject, TokenInterface, TokenObjectParamsInterface } from './types'; | ||
/** | ||
@@ -118,3 +118,3 @@ * @class TokenObject | ||
*/ | ||
setToken(authToken: string, authTokenType: string, refreshToken: string | null, refreshTokenExpiresAt: Date | null, expiresAt: Date, authState: object): void; | ||
setToken(authToken: string, authTokenType: string, refreshToken: string | null, refreshTokenExpiresAt: Date | null, expiresAt: Date, authState: AuthStateUserObject): void; | ||
/** | ||
@@ -131,3 +131,3 @@ * | ||
*/ | ||
setCookieToken_(authToken: string, authTokenType: string, refreshToken: string | null, expiresAt: Date, refreshTokenExpiresAt: Date | null, authState: object): void; | ||
setCookieToken_(authToken: string, authTokenType: string, refreshToken: string | null, expiresAt: Date, refreshTokenExpiresAt: Date | null, authState: AuthStateUserObject): void; | ||
/** | ||
@@ -143,3 +143,3 @@ * Set LocalStorage on time of Login | ||
*/ | ||
setLSToken_(authToken: string, authTokenType: string, refreshToken: string | null, expiresAt: Date, refreshTokenExpiresAt: Date | null, authState: object): void; | ||
setLSToken_(authToken: string, authTokenType: string, refreshToken: string | null, expiresAt: Date, refreshTokenExpiresAt: Date | null, authState: AuthStateUserObject): void; | ||
/** | ||
@@ -146,0 +146,0 @@ * Remove Tokens on time of Logout |
import * as React from 'react'; | ||
import { AuthActions } from './utils/actions'; | ||
export interface TokenInterface { | ||
@@ -30,3 +31,3 @@ authToken: string | null; | ||
authState: TokenInterface; | ||
setAuthState: React.Dispatch<React.SetStateAction<TokenInterface>>; | ||
dispatch: React.Dispatch<AuthActions>; | ||
} | ||
@@ -33,0 +34,0 @@ export interface AuthProviderProps { |
{ | ||
"name": "react-auth-kit", | ||
"version": "1.4.12", | ||
"version": "1.4.13-0", | ||
"description": "Authentication Library for React, which makes Token based auth very easy", | ||
@@ -5,0 +5,0 @@ "source": "src/index.tsx", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
35
295187
831
4