react-auth-kit
Advanced tools
Comparing version 1.6.4 to 1.6.5
@@ -1,2 +0,2 @@ | ||
/*! react-auth-kit v1.6.4 | Apache-2.0 */ | ||
/*! react-auth-kit v1.6.5 | Apache-2.0 */ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react"),require("js-cookie"),require("react-router-dom")):"function"==typeof define&&define.amd?define(["exports","react","js-cookie","react-router-dom"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).ReactAuthKit={},e.React,e.Cookies,e.ReactRouterDOM)}(this,(function(e,t,r,n){"use strict";function o(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}function i(e){if(e&&e.__esModule)return e;var t=Object.create(null);return e&&Object.keys(e).forEach((function(r){if("default"!==r){var n=Object.getOwnPropertyDescriptor(e,r);Object.defineProperty(t,r,n.get?n:{enumerable:!0,get:function(){return e[r]}})}})),t.default=e,Object.freeze(t)}var a,s=i(t),u=o(r),h=s.createContext(null),c=h.Consumer,f=function(){return(f=Object.assign||function(e){for(var t,r=1,n=arguments.length;r<n;r++)for(var o in t=arguments[r])Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o]);return e}).apply(this,arguments)},l=function(){function e(e,t,r,n,o){this.authStorageType=t,this.authStorageName=e,this.authTimeStorageName="".concat(e,"_storage"),this.stateStorageName="".concat(e,"_state"),this.refreshTokenName=r,this.cookieDomain=n,this.cookieSecure=o,this.authStorageTypeName="".concat(this.authStorageName,"_type"),this.isUsingRefreshToken=!!this.refreshTokenName,this.refreshTokenTimeName=this.refreshTokenName?"".concat(this.refreshTokenName,"_time"):null}return e.prototype.initialToken=function(){return"cookie"===this.authStorageType?this.initialCookieToken_():this.initialLSToken_()},e.prototype.initialCookieToken_=function(){var e=u.default.get(this.authStorageName),t=u.default.get(this.authStorageTypeName),r=u.default.get(this.authTimeStorageName),n=u.default.get(this.stateStorageName),o=this.isUsingRefreshToken&&null!=this.refreshTokenName?u.default.get(this.refreshTokenName):null,i=this.isUsingRefreshToken&&null!=this.refreshTokenTimeName?u.default.get(this.refreshTokenTimeName):null;return this.checkTokenExist(e,t,r,n,o,i)},e.prototype.initialLSToken_=function(){var e=localStorage.getItem(this.authStorageName),t=localStorage.getItem(this.authStorageTypeName),r=localStorage.getItem(this.authTimeStorageName),n=localStorage.getItem(this.stateStorageName),o=this.isUsingRefreshToken&&null!=this.refreshTokenName?localStorage.getItem(this.refreshTokenName):null,i=this.isUsingRefreshToken&&null!=this.refreshTokenTimeName?localStorage.getItem(this.refreshTokenTimeName):null;return this.checkTokenExist(e,t,r,n,o,i)},e.prototype.checkTokenExist=function(e,t,r,n,o,i){if(!(e&&t&&r&&n))return{auth:null,refresh:null,userState:null,isUsingRefreshToken:this.isUsingRefreshToken,isSignIn:!1};var a=new Date(r);try{var s={auth:{token:e,type:t,expiresAt:a},userState:JSON.parse(n),isSignIn:!0,isUsingRefreshToken:this.isUsingRefreshToken,refresh:void 0};if(this.isUsingRefreshToken&&o&&i){var u=new Date(i);return f(f({},s),{refresh:{token:o,expiresAt:u}})}return f(f({},s),{refresh:null})}catch(e){return{auth:null,refresh:null,userState:null,isUsingRefreshToken:this.isUsingRefreshToken,isSignIn:!1}}},e.prototype.syncTokens=function(e){e.auth?this.isUsingRefreshToken&&e.refresh?this.setToken(e.auth.token,e.auth.type,e.refresh.token,e.refresh.expiresAt,e.auth.expiresAt,e.userState):this.setToken(e.auth.token,e.auth.type,null,null,e.auth.expiresAt,e.userState):this.removeToken()},e.prototype.setToken=function(e,t,r,n,o,i){"cookie"===this.authStorageType?this.setCookieToken_(e,t,r,o,n,i):this.setLSToken_(e,t,r,o,n,i)},e.prototype.setCookieToken_=function(e,t,r,n,o,i){u.default.set(this.authStorageName,e,{expires:n,domain:this.cookieDomain,secure:this.cookieSecure}),u.default.set(this.authStorageTypeName,t,{expires:n,domain:this.cookieDomain,secure:this.cookieSecure}),u.default.set(this.authTimeStorageName,n.toISOString(),{expires:n,domain:this.cookieDomain,secure:this.cookieSecure}),i&&u.default.set(this.stateStorageName,JSON.stringify(i),{expires:n,domain:this.cookieDomain,secure:this.cookieSecure}),this.isUsingRefreshToken&&this.refreshTokenName&&r&&u.default.set(this.refreshTokenName,r,{expires:n,domain:this.cookieDomain,secure:this.cookieSecure}),this.isUsingRefreshToken&&this.refreshTokenTimeName&&o&&u.default.set(this.refreshTokenTimeName,o.toISOString(),{expires:n,domain:this.cookieDomain,secure:this.cookieSecure})},e.prototype.setLSToken_=function(e,t,r,n,o,i){localStorage.setItem(this.authStorageName,e),localStorage.setItem(this.authStorageTypeName,t),localStorage.setItem(this.authTimeStorageName,n.toISOString()),i&&localStorage.setItem(this.stateStorageName,JSON.stringify(i)),this.isUsingRefreshToken&&this.refreshTokenName&&r&&localStorage.setItem(this.refreshTokenName,r),this.isUsingRefreshToken&&this.refreshTokenTimeName&&o&&localStorage.setItem(this.refreshTokenTimeName,o.toISOString())},e.prototype.removeToken=function(){"cookie"===this.authStorageType?this.removeCookieToken_():this.removeLSToken_()},e.prototype.removeCookieToken_=function(){u.default.remove(this.authStorageName,{domain:this.cookieDomain,secure:this.cookieSecure}),u.default.remove(this.authTimeStorageName,{domain:this.cookieDomain,secure:this.cookieSecure}),u.default.remove(this.authStorageTypeName,{domain:this.cookieDomain,secure:this.cookieSecure}),u.default.remove(this.stateStorageName,{domain:this.cookieDomain,secure:this.cookieSecure}),this.isUsingRefreshToken&&this.refreshTokenName&&u.default.remove(this.refreshTokenName,{domain:this.cookieDomain,secure:this.cookieSecure}),this.isUsingRefreshToken&&this.refreshTokenTimeName&&u.default.remove(this.refreshTokenTimeName,{domain:this.cookieDomain,secure:this.cookieSecure})},e.prototype.removeLSToken_=function(){localStorage.removeItem(this.authStorageName),localStorage.removeItem(this.authTimeStorageName),localStorage.removeItem(this.authStorageTypeName),localStorage.removeItem(this.stateStorageName),this.isUsingRefreshToken&&this.refreshTokenName&&localStorage.removeItem(this.refreshTokenName),this.isUsingRefreshToken&&this.refreshTokenTimeName&&localStorage.removeItem(this.refreshTokenTimeName)},e}();function m(e,t){switch(t.type){case a.SignIn:return f(f({},e),{auth:t.payload.auth,refresh:t.payload.refresh,userState:t.payload.userState,isSignIn:!0});case a.SignOut:return f(f({},e),{auth:null,refresh:null,userState:null,isSignIn:!1});case a.RefreshToken:return e.isSignIn&&e.auth&&e.refresh?f(f({},e),{auth:{token:t.payload.newAuthToken?t.payload.newAuthToken:e.auth.token,type:e.auth.type,expiresAt:t.payload.newAuthTokenExpireIn?new Date((new Date).getTime()+60*t.payload.newAuthTokenExpireIn*1e3):e.auth.expiresAt},refresh:{token:t.payload.newRefreshToken?t.payload.newRefreshToken:e.refresh.token,expiresAt:t.payload.newRefreshTokenExpiresIn?new Date((new Date).getTime()+60*t.payload.newRefreshTokenExpiresIn*1e3):e.refresh.expiresAt},userState:t.payload.newAuthUserState?t.payload.newAuthUserState:e.userState}):e}}function k(e){return{type:a.SignIn,payload:e}}function p(){return{type:a.SignOut}}!function(e){e[e.SignIn=0]="SignIn",e[e.SignOut=1]="SignOut",e[e.RefreshToken=2]="RefreshToken"}(a||(a={}));var d=function(e){var t=e.children,r=e.authType,n=e.authName,o=e.cookieDomain,i=e.cookieSecure,u=e.refresh;if("cookie"===r&&!o)throw new Error("authType 'cookie' requires 'cookieDomain' and 'cookieSecure' props in AuthProvider");var c,f,k,p,d=u?"".concat(n,"_refresh"):null,S=new l(n,r,d,o,i),T=s.useReducer(m,S.initialToken()),g=T[0],v=T[1];return u&&(c=function(){var e,t,r,n;u.refreshApiCallback({authToken:null===(e=g.auth)||void 0===e?void 0:e.token,authTokenExpireAt:null===(t=g.auth)||void 0===t?void 0:t.expiresAt,authUserState:g.userState,refreshToken:null===(r=g.refresh)||void 0===r?void 0:r.token,refreshTokenExpiresAt:null===(n=g.refresh)||void 0===n?void 0:n.expiresAt}).then((function(e){var t;e.isSuccess&&v((t=e,{type:a.RefreshToken,payload:t}))}))},f=g.isSignIn?u.interval:null,k=s.useRef(c),p=s.useRef(null),s.useEffect((function(){k.current=c}),[c]),s.useEffect((function(){return"number"==typeof f&&(p.current=window.setInterval((function(){return k.current()}),60*f*1e3)),function(){p.current&&window.clearTimeout(p.current)}}),[f])),s.useEffect((function(){S.syncTokens(g)}),[g]),s.createElement(h.Provider,{value:{authState:g,dispatch:v}},t)};d.defaultProps={cookieDomain:window.location.hostname,cookieSecure:"https:"===window.location.protocol},e.AuthProvider=d,e.PrivateRoute=function(e){var t=s.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,u=e.exact,c=e.path,f=e.location,l=e.render;return s.createElement(n.Route,{location:f,path:c,exact:u,sensitive:a,strict:i,render:function(e){return t.authState.auth&&(new Date(t.authState.auth.expiresAt)>new Date||(t.dispatch(p()),0))?r?s.createElement(r,e):l?l(e):null:s.createElement(n.Redirect,{to:o})}})},e.createRefresh=function(e){return e},e.useAuthHeader=function(){var e=s.useContext(h);if(null===e)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return function(){return e.authState.auth?"".concat(e.authState.auth.type," ").concat(e.authState.auth.token):""}},e.useAuthUser=function(){var e=s.useContext(h);if(null===e)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return function(){return e.authState.auth?e.authState.userState:null}},e.useIsAuthenticated=function(){var e=s.useContext(h);if(null===e)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return function(){return!!e.authState.auth&&(new Date(e.authState.auth.expiresAt)>new Date||(e.dispatch(p()),!1))}},e.useSignIn= | ||
@@ -3,0 +3,0 @@ /** |
{ | ||
"name": "react-auth-kit", | ||
"version": "1.6.4", | ||
"version": "1.6.5", | ||
"description": "Authentication Library for React, which makes Token based auth very easy", | ||
@@ -51,3 +51,3 @@ "source": "src/index.tsx", | ||
"@commitlint/config-conventional": "^15.0.0", | ||
"@rollup/plugin-node-resolve": "^13.0.6", | ||
"@rollup/plugin-node-resolve": "^13.1.1", | ||
"@semantic-release/changelog": "^6.0.1", | ||
@@ -59,3 +59,3 @@ "@semantic-release/git": "^10.0.1", | ||
"@types/js-cookie": "^3.0.1", | ||
"@types/node": "^16.11.11", | ||
"@types/node": "^16.11.12", | ||
"@types/react": "^17.0.37", | ||
@@ -65,3 +65,3 @@ "@types/react-dom": "^17.0.11", | ||
"@typescript-eslint/eslint-plugin": "^5.6.0", | ||
"@typescript-eslint/parser": "^5.5.0", | ||
"@typescript-eslint/parser": "^5.7.0", | ||
"codecov": "^3.8.3", | ||
@@ -73,3 +73,3 @@ "eslint": "^8.4.1", | ||
"husky": "^7.0.4", | ||
"jest": "^27.4.3", | ||
"jest": "^27.4.5", | ||
"lint-staged": "^12.1.2", | ||
@@ -80,3 +80,3 @@ "mkdirp": "^1.0.4", | ||
"rimraf": "^3.0.2", | ||
"rollup": "^2.60.2", | ||
"rollup": "^2.61.1", | ||
"rollup-plugin-filesize": "^9.0.2", | ||
@@ -87,6 +87,6 @@ "rollup-plugin-license": "^2.6.0", | ||
"rollup-plugin-typescript2": "^0.31.1", | ||
"ts-jest": "^27.1.0", | ||
"ts-jest": "^27.1.1", | ||
"tslib": "^2.3.1", | ||
"typedoc": "^0.22.10", | ||
"typescript": "^4.5.2", | ||
"typescript": "^4.5.4", | ||
"utility-types": "^3.10.0" | ||
@@ -93,0 +93,0 @@ }, |