import{WebAuth as e}from"auth0-js";function t(){return(t=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var s=arguments[t];for(var r in s)Object.prototype.hasOwnProperty.call(s,r)&&(e[r]=s[r])}return e}).apply(this,arguments)}class s{constructor(s){this.dispatch=s.dispatch,this.customPropertyNamespace=s.customPropertyNamespace,this.auth0=new e(t({},s))}static addDefaultParams(e,s){return t({redirectUri:`${s}/auth0_callback`,audience:`https://${e.domain}/api/v2/`,responseType:"token id_token",scope:"openid profile email"},e)}authorize(){this.auth0.authorize()}signup(){this.auth0.authorize({mode:"signUp",screen_hint:"signup"})}logout(e){this.auth0.logout({returnTo:e})}userId(e){return e.sub}userRoles(e){const t=e[`${this.customPropertyNamespace}/user_metadata`.replace(/\/+user_metadata/,"/user_metadata")];return(null==t?void 0:t.roles)||null}async handleLoginCallback(){var e=this;return new Promise((t,s)=>{this.auth0.parseHash(async function(s,r){s&&(e.dispatch("ERROR",{error:s,errorType:"authResult"}),t(!1));try{const s=await e.handleAuthResult(r);t(s)}catch(s){e.dispatch("ERROR",{error:s,errorType:"handleAuth"}),t(!1)}})})}async checkSession(){var e=this;return new Promise((t,s)=>{this.auth0.checkSession({},async function(r,a){if(!r&&a&&a.accessToken&&a.idToken)try{const s=await e.fetchUser(a);t({user:s,authResult:a})}catch(e){s(e)}else s(r||new Error("Session invalid"))})})}async handleAuthResult(e){if(e&&e.accessToken&&e.idToken){const t=await this.fetchUser(e);return this.dispatch("AUTHENTICATED",{authResult:e,user:t}),!0}return!1}async fetchUser(e){return new Promise((t,s)=>{this.auth0.client.userInfo((null==e?void 0:e.accessToken)||"",(e,r)=>{e?s(e):t(r)})})}}export{s as Auth0};
//# sourceMappingURL=auth0.modern.js.map
