var t=require("react"),e=require("@xstate/react"),n=require("date-fns"),r=require("xstate"),o=require("xstate/lib/actions");function a(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var i=a(t);function u(){return(u=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var n=arguments[e];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(t[r]=n[r])}return t}).apply(this,arguments)}var s=r.Machine({id:"useAuth",initial:"unauthenticated",context:{user:{},expiresAt:null,authResult:null,isAuthenticating:!1,error:void 0,errorType:void 0,config:{navigate:function(){return console.error("Please specify a navigation method that works with your router")},callbackDomain:"http://localhost:8000"}},states:{unauthenticated:{on:{LOGIN:"authenticating",CHECK_SESSION:"verifying",SET_CONFIG:{actions:["setConfig"]}}},authenticating:{on:{ERROR:"error",AUTHENTICATED:"authenticated",SET_CONFIG:{actions:["setConfig"]}},entry:["startAuthenticating"],exit:["stopAuthenticating"]},verifying:{invoke:{id:"checkSession",src:function(t,e){return t.config.authProvider.checkSession()},onDone:{target:"authenticated"},onError:{target:"unauthenticated",actions:["clearUserFromContext","clearLocalStorage"]}},entry:["startAuthenticating"],exit:["stopAuthenticating"]},authenticated:{on:{LOGOUT:"unauthenticated",SET_CONFIG:{actions:["setConfig"]},CHECK_SESSION:"verifying"},entry:["saveUserToContext","saveToLocalStorage"],exit:o.choose([{cond:function(t,e){return"CHECK_SESSION"!==e.type},actions:["clearUserFromContext","clearLocalStorage"]}])},error:{entry:["saveErrorToContext","clearUserFromContext","clearLocalStorage"]}}},{actions:{startAuthenticating:r.assign(function(t){return{isAuthenticating:!0}}),stopAuthenticating:r.assign(function(t){return{isAuthenticating:!1}}),saveUserToContext:r.assign(function(t,e){var r=e.data?e.data:e,o=r.authResult;return{user:r.user,authResult:o,expiresAt:n.addSeconds(new Date,o.expiresIn)}}),clearUserFromContext:r.assign(function(t){return{user:{},expiresAt:null,authResult:null}}),saveToLocalStorage:function(t,e){var n=t.expiresAt,r=t.user;"undefined"!=typeof localStorage&&(localStorage.setItem("useAuth:expires_at",n?n.toISOString():"0"),localStorage.setItem("useAuth:user",JSON.stringify(r)))},clearLocalStorage:function(){"undefined"!=typeof localStorage&&(localStorage.removeItem("useAuth:expires_at"),localStorage.removeItem("useAuth:user"))},saveErrorToContext:r.assign(function(t,e){return{errorType:e.errorType,error:e.error}}),setConfig:r.assign(function(t,e){return{config:u({},t.config,e)}})}}),c=r.interpret(s);c.start(),function(t){if("undefined"!=typeof localStorage){var e=new Date(localStorage.getItem("useAuth:expires_at")||"0"),r=new Date;if(n.isAfter(e,r)){var o=JSON.parse(localStorage.getItem("useAuth:user")||"{}");t("LOGIN"),t("AUTHENTICATED",{user:o,authResult:{expiresIn:n.differenceInSeconds(e,r)}})}}}(c.send);var l=function(){var r=e.useService(c),o=r[0],a=r[1],i=o.context.config,u=i.authProvider,s=i.navigate,l=i.callbackDomain,f=t.useCallback(function(t){var e=(void 0===t?{}:t).postLoginRoute,n=void 0===e?"/":e;try{if(!u||!s)return console.warn("authProvider not configured yet"),Promise.resolve();var r=function(){if("undefined"!=typeof window)return a("LOGIN"),Promise.resolve(u.handleLoginCallback(a)).then(function(t){t&&s(n)})}();return Promise.resolve(r&&r.then?r.then(function(){}):void 0)}catch(t){return Promise.reject(t)}},[u,s]),g=function(){return!(!o.context.expiresAt||!n.isAfter(o.context.expiresAt,new Date))};return{isAuthenticating:o.context.isAuthenticating,isAuthenticated:g,isAuthorized:function(t){var e=Array.isArray(t)?t:[t],n=null==u?void 0:u.userRoles(o.context.user);return!(!g()||!n)&&e.some(function(t){return n.includes(t)})},user:o.context.user,userId:null==u?void 0:u.userId(o.context.user),authResult:o.context.authResult,login:function(){null==u||u.authorize()},signup:function(){null==u||u.signup()},logout:function(t){"string"==typeof t?null==u||u.logout(""+l+t):null==u||u.logout(),a("LOGOUT"),s("string"==typeof t?t:"/")},handleAuthentication:f,dispatch:a}};exports.AuthConfig=function(e){var n=e.authProvider,r=e.params,o=e.navigate,a=e.children,i=l().dispatch,s="undefined"!=typeof window?window.location.protocol+"//"+window.location.host:"http://localhost:8000";return t.useEffect(function(){var t=new n(u({dispatch:i},n.addDefaultParams(r,s)));i("SET_CONFIG",{authProvider:t,navigate:o,callbackDomain:s}),i("CHECK_SESSION")},[i,n,r,o]),t.createElement(t.Fragment,null,a)},exports.AuthProvider=function(e){var n=e.children;return t.useEffect(function(){console.error("The AuthProvider root component no longer works. Please take 5min to migrate to AuthConfig. 👉 https://useauth.dev/docs/upgrading")},[]),i.default.createElement(i.default.Fragment,null,n)},exports.useAuth=l;
//# sourceMappingURL=react-use-auth.js.map
