@happykit/auth-email
Version:
- A `useAuth` hook which returns the current user - An optional `getServerSideAuth` for server-side rendering - HappyAuth is tiny - it adds only 4.6 kB to the first load JS - it adds less than 0.04 kB if you're transitioning from another page - Extremely
1 lines • 49.8 kB
JavaScript
module.exports=function(e){var t={};function r(n){if(t[n])return t[n].exports;var o=t[n]={i:n,l:!1,exports:{}};return e[n].call(o.exports,o,o.exports,r),o.l=!0,o.exports}return r.m=e,r.c=t,r.d=function(e,t,n){r.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:n})},r.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},r.t=function(e,t){if(1&t&&(e=r(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)r.d(n,o,function(t){return e[t]}.bind(null,o));return n},r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,"a",t),t},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.p="",r(r.s=10)}([function(e,t){e.exports=require("react")},,function(e,t){e.exports=require("@xstate/fsm")},function(e,t){e.exports=require("@xstate/react/lib/fsm")},,,,,,,function(e,t,r){"use strict";var n=this&&this.__assign||function(){return(n=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)},o=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(t,"__esModule",{value:!0}),t.ok=t.jwtExpired=t.unexpectedError=t.authenticationFailed=t.noContent=t.unauthorized=t.redirect=t.serializeAuthCookie=t.createAuthRouteHandler=t.createGetServerSideAuth=t.sendForgotPasswordMailToConsole=t.sendConfirmAccountMailToConsole=void 0;var i=r(25),a=r(26),s=r(27);Object.defineProperty(t,"sendConfirmAccountMailToConsole",{enumerable:!0,get:function(){return s.sendConfirmAccountMailToConsole}});var u=r(28),c=r(29);Object.defineProperty(t,"sendForgotPasswordMailToConsole",{enumerable:!0,get:function(){return c.sendForgotPasswordMailToConsole}});var l=r(30),f=r(31),d=r(32),h=r(33),p=r(34),v=r(37),y=o(r(38)),g=o(r(14)),w=o(r(15)),b=r(39);Object.defineProperty(t,"createFaunaEmailDriver",{enumerable:!0,get:function(){return b.createFaunaEmailDriver}});function m(e){var t={sameSite:"lax",secure:e.secure,httpOnly:!1,path:"/"};return g.default.serialize("syncAuthState","login",t)}t.createGetServerSideAuth=function(e){return function(t){var r;if(!e.tokenSecret)throw new Error("HappyAuth: Missing serverConfig.tokenSecret");if(!e.cookieName)throw new Error("HappyAuth: Missing serverConfig.cookieName");var n=g.default.parse(null!==(r=t.headers.cookie)&&void 0!==r?r:"")[e.cookieName];try{return{value:"signedIn",context:{tokenData:w.default.verify(n,e.tokenSecret),error:null}}}catch(e){return{value:"signedOut",context:{tokenData:null,error:null}}}}},t.createAuthRouteHandler=function(e){var t={login:i.createLogin(e),logout:a.createLogout(e),signup:s.createSignup(e),tokencontent:u.createTokenContent(e),"forgot-password":c.createForgotPassword(e),"reset-password":l.createResetPassword(e),"change-password":f.createChangePassword(e),"confirm-account":d.createConfirmAccount(e),"resend-confirmation-email":h.createResendConfirmationEmail(e),oauth:p.createOAuth(e),connect:v.createConnect(e)};return function(e,r){var n,o=(n=e.query.params,Array.isArray(n)?n[0]:n),i=t.hasOwnProperty(o)?t[o]:null;return i?i(e,r):r.status(404).end()}},t.serializeAuthCookie=function(e,t,r){void 0===r&&(r={rememberMe:!1});var n=y.default("7d");if(!e.tokenSecret)throw new Error("HappyAuth: Missing token secret");if(!e.tokenSecret)throw new Error("HappyAuth: Missing cookie name");var o=w.default.sign(t,e.tokenSecret,{expiresIn:n}),i={sameSite:"lax",secure:e.secure,httpOnly:!0,path:"/"};return r.rememberMe&&(i.maxAge=n-30),[g.default.serialize(e.cookieName,o,i),m(e)]},t.redirect=function(e,t){return void 0===t&&(t="/login"),e.writeHead(302,{Location:t}).end(),{props:{}}},t.unauthorized=function(e,t){e.status(200).json({error:n({code:"unauthorized"},t)})},t.noContent=function(e){e.status(200).json({error:{code:"no content"}})},t.authenticationFailed=function(e,t){e.status(200).json({error:n({code:"authentication failed"},t)})},t.unexpectedError=function(e,t){e.status(500).json({error:n({code:"unexpected error"},t?{message:t.message}:{})})},t.jwtExpired=function(e,t){e.status(200).json({error:n({code:"jwt expired"},t)})},t.ok=function(e){e.status(200).json({data:{ok:!0}})}},,,function(e,t,r){"use strict";var n=this&&this.__createBinding||(Object.create?function(e,t,r,n){void 0===n&&(n=r),Object.defineProperty(e,n,{enumerable:!0,get:function(){return t[r]}})}:function(e,t,r,n){void 0===n&&(n=r),e[n]=t[r]}),o=this&&this.__setModuleDefault||(Object.create?function(e,t){Object.defineProperty(e,"default",{enumerable:!0,value:t})}:function(e,t){e.default=t}),i=this&&this.__importStar||function(e){if(e&&e.__esModule)return e;var t={};if(null!=e)for(var r in e)"default"!==r&&Object.hasOwnProperty.call(e,r)&&n(t,e,r);return o(t,e),t},a=this&&this.__awaiter||function(e,t,r,n){return new(r||(r=Promise))((function(o,i){function a(e){try{u(n.next(e))}catch(e){i(e)}}function s(e){try{u(n.throw(e))}catch(e){i(e)}}function u(e){var t;e.done?o(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(a,s)}u((n=n.apply(e,t||[])).next())}))},s=this&&this.__generator||function(e,t){var r,n,o,i,a={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function s(i){return function(s){return function(i){if(r)throw new TypeError("Generator is already executing.");for(;a;)try{if(r=1,n&&(o=2&i[0]?n.return:i[0]?n.throw||((o=n.return)&&o.call(n),0):n.next)&&!(o=o.call(n,i[1])).done)return o;switch(n=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return a.label++,{value:i[1],done:!1};case 5:a.label++,n=i[1],i=[0];continue;case 7:i=a.ops.pop(),a.trys.pop();continue;default:if(!(o=a.trys,(o=o.length>0&&o[o.length-1])||6!==i[0]&&2!==i[0])){a=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){a.label=i[1];break}if(6===i[0]&&a.label<o[1]){a.label=o[1],o=i;break}if(o&&a.label<o[2]){a.label=o[2],a.ops.push(i);break}o[2]&&a.ops.pop(),a.trys.pop();continue}i=t.call(e,a)}catch(e){i=[6,e],n=0}finally{r=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,s])}}},u=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(t,"__esModule",{value:!0}),t.createUseAuth=t.AccountStatus=t.Provider=t.AuthProvider=void 0;var c=i(r(0)),l=r(2),f=r(3),d=u(r(14)),h=c.createContext([null,null]),p=function(e){var t=c.useState(null),r=t[0],n=t[1],o=c.useCallback((function(e){n((function(t){if(null===t)return e;if(t===e)return t;throw new Error("AuthProvider: Only one useAuth may be used per page.")}))}),[r,n]),i=c.useCallback((function(e){n((function(t){if(t===e)return null;throw new Error("AuthProvider: Tried to unregister an inactive useAuth hook.")}))}),[r,n]);return c.createElement(h.Provider,{value:[o,i]},e.children)};t.AuthProvider=function(e){return c.createElement(p,null,e.children)},function(e){e.email="email"}(t.Provider||(t.Provider={})),function(e){e.unconfirmed="unconfirmed",e.confirmed="confirmed"}(t.AccountStatus||(t.AccountStatus={})),t.createUseAuth=function(e){return function(t){var r=this;void 0===t&&(t={value:"authenticating",context:{tokenData:null,error:null}}),function(){var e=c.useRef(Symbol()),t=c.useContext(h),r=t[0],n=t[1];if(null===r||null===n)throw new Error("Missing AuthProvider. Add it to _app.js or _app.tsx.");c.useEffect((function(){return r(e.current),function(){n(e.current)}}),[])}();var n=c.useMemo((function(){return l.createMachine({id:"userMachine",initial:t.value,context:t.context,states:{authenticating:{entry:["refetchTokenContent"],on:{SIGN_IN_SUCCESS:{target:"signedIn",actions:l.assign({tokenData:function(e,t){return t.tokenData}})},SIGN_IN_FAILURE:{target:"signedOut",actions:l.assign({tokenData:function(e,t){return null},error:function(e,t){return null}})},SIGN_IN_ERROR:{target:"signedOut",actions:l.assign({error:function(e,t){return t.error}})}}},signedIn:{},signedOut:{},signInError:{}}})}),[]),o=f.useMachine(n),i=o[0],u=o[1],p=c.useCallback((function(e,t,n){return void 0===n&&(n=!1),a(r,void 0,void 0,(function(){var r,o;return s(this,(function(i){switch(i.label){case 0:return[4,fetch("/api/auth/login",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({email:e,password:t,rememberMe:n})})];case 1:return 200!==(r=i.sent()).status?[3,2]:[2,r.json()];case 2:return o=Error.bind,[4,r.text()];case 3:throw new(o.apply(Error,[void 0,i.sent()]))}}))}))}),[u]),v=function(e){"logout"===e.key&&(window.localStorage.removeItem("logout"),window.location.reload()),"login"===e.key&&(window.localStorage.removeItem("login"),window.location.reload())};c.useEffect((function(){return window.addEventListener("storage",v),function(){window.removeEventListener("storage",v)}}),[]),c.useEffect((function(){"login"===d.default.parse(document.cookie).syncAuthState&&(document.cookie=d.default.serialize("syncAuthState","",{maxAge:-1}),window.localStorage.setItem("login",String(Date.now())))}),[]);var y=c.useCallback((function(t){return void 0===t&&(t=(null===(o=null==e?void 0:e.redirects)||void 0===o?void 0:o.afterSignOut)||"/"),a(r,void 0,void 0,(function(){var e,r;return s(this,(function(n){switch(n.label){case 0:return[4,fetch("/api/auth/logout",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({})})];case 1:return 200!==(e=n.sent()).status?[3,2]:(window.localStorage.setItem("logout",String(Date.now())),window.location.href=t,[2,e.json()]);case 2:return r=Error.bind,[4,e.text()];case 3:throw new(r.apply(Error,[void 0,n.sent()]))}}))}))}),[u]),g=c.useCallback((function(e,t){return a(r,void 0,void 0,(function(){var r,n;return s(this,(function(o){switch(o.label){case 0:return[4,fetch("/api/auth/signup",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({email:e,password:t})})];case 1:return 200!==(r=o.sent()).status?[3,2]:[2,r.json()];case 2:return n=Error.bind,[4,r.text()];case 3:throw new(n.apply(Error,[void 0,o.sent()]))}}))}))}),[u]),w=c.useCallback((function(e,t){return a(r,void 0,void 0,(function(){var r,n;return s(this,(function(o){switch(o.label){case 0:return[4,fetch("/api/auth/reset-password",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({token:e,password:t})})];case 1:return 200!==(r=o.sent()).status?[3,2]:[2,r.json()];case 2:return n=Error.bind,[4,r.text()];case 3:throw new(n.apply(Error,[void 0,o.sent()]))}}))}))}),[u]),b=c.useCallback((function(e){return a(r,void 0,void 0,(function(){return s(this,(function(t){switch(t.label){case 0:return[4,fetch("/api/auth/confirm-account",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({token:e})})];case 1:return[2,t.sent().json()]}}))}))}),[u]),m=c.useCallback((function(e,t){return a(r,void 0,void 0,(function(){var r,n;return s(this,(function(o){switch(o.label){case 0:return[4,fetch("/api/auth/change-password",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({currentPassword:e,newPassword:t})})];case 1:return 200!==(r=o.sent()).status?[3,2]:[2,r.json()];case 2:return n=Error.bind,[4,r.text()];case 3:throw new(n.apply(Error,[void 0,o.sent()]))}}))}))}),[u]),k=c.useCallback((function(e){return a(r,void 0,void 0,(function(){var t,r;return s(this,(function(n){switch(n.label){case 0:return[4,fetch("/api/auth/forgot-password",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({email:e})})];case 1:return 200!==(t=n.sent()).status?[3,2]:[2,t.json()];case 2:return r=Error.bind,[4,t.text()];case 3:throw new(r.apply(Error,[void 0,n.sent()]))}}))}))}),[u]),_=c.useMemo((function(){return{signIn:p,signOut:y,signUp:g,resetPassword:w,confirmAccount:b,changePassword:m,forgotPassword:k}}),[p,y,g,w,b,m,k]);return c.useEffect((function(){Array.isArray(i.actions)&&i.actions.forEach((function(e){switch(e.type){case"refetchTokenContent":fetch("/api/auth/tokencontent").then((function(e){return a(r,void 0,void 0,(function(){var t,r;return s(this,(function(n){switch(n.label){case 0:return 200!==e.status?[3,2]:[4,e.json()];case 1:return t=n.sent(),(r=t?t.data:null)?("signedIn"===r.value?u({type:"SIGN_IN_SUCCESS",tokenData:r.context.tokenData}):u({type:"SIGN_IN_FAILURE",error:""}),[3,3]):(u({type:"SIGN_IN_FAILURE",error:""}),[2]);case 2:u({type:"SIGN_IN_ERROR",error:"unexpected error"}),n.label=3;case 3:return[2]}}))}))}),(function(e){console.error(e),u({type:"SIGN_IN_ERROR",error:"json decoding failed or another unexpected error"})}))}}))}),[u,i.actions]),c.useMemo((function(){return function(e,t){switch(e.value){case"authenticating":return{state:"authenticating",tokenData:null,error:null,signIn:null,signUp:null,signOut:null,changePassword:null,confirmAccount:null,forgotPassword:null,resetPassword:null};case"signedIn":return{state:"signedIn",tokenData:e.context.tokenData,error:null,signIn:null,signUp:null,signOut:t.signOut,changePassword:t.changePassword,confirmAccount:null,forgotPassword:null,resetPassword:null};case"signedOut":return{state:"signedOut",tokenData:null,error:null,signIn:t.signIn,signUp:t.signUp,signOut:null,changePassword:null,confirmAccount:t.confirmAccount,forgotPassword:t.forgotPassword,resetPassword:t.resetPassword};case"signInError":return{state:"signInError",tokenData:null,error:e.context.error,signIn:null,signUp:null,signOut:null,changePassword:null,confirmAccount:null,forgotPassword:null,resetPassword:null}}}(i,_)}),[i,_])}}},function(e,t){e.exports=require("cookie")},function(e,t){e.exports=require("jsonwebtoken")},,,,,,,,,,function(e,t,r){"use strict";var n=this&&this.__assign||function(){return(n=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)},o=this&&this.__awaiter||function(e,t,r,n){return new(r||(r=Promise))((function(o,i){function a(e){try{u(n.next(e))}catch(e){i(e)}}function s(e){try{u(n.throw(e))}catch(e){i(e)}}function u(e){var t;e.done?o(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(a,s)}u((n=n.apply(e,t||[])).next())}))},i=this&&this.__generator||function(e,t){var r,n,o,i,a={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function s(i){return function(s){return function(i){if(r)throw new TypeError("Generator is already executing.");for(;a;)try{if(r=1,n&&(o=2&i[0]?n.return:i[0]?n.throw||((o=n.return)&&o.call(n),0):n.next)&&!(o=o.call(n,i[1])).done)return o;switch(n=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return a.label++,{value:i[1],done:!1};case 5:a.label++,n=i[1],i=[0];continue;case 7:i=a.ops.pop(),a.trys.pop();continue;default:if(!(o=a.trys,(o=o.length>0&&o[o.length-1])||6!==i[0]&&2!==i[0])){a=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){a.label=i[1];break}if(6===i[0]&&a.label<o[1]){a.label=o[1],o=i;break}if(o&&a.label<o[2]){a.label=o[2],a.ops.push(i);break}o[2]&&a.ops.pop(),a.trys.pop();continue}i=t.call(e,a)}catch(e){i=[6,e],n=0}finally{r=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,s])}}};Object.defineProperty(t,"__esModule",{value:!0}),t.createLogin=void 0;var a=r(13),s=r(10);t.createLogin=function(e){return function(t,r){return o(this,void 0,void 0,(function(){var o,u,c,l,f,d,h,p,v;return i(this,(function(i){switch(i.label){case 0:if(o=t.body,u=o.email,c=o.password,l=o.rememberMe,"string"!=typeof u)return r.status(200).json({error:{code:"invalid email",message:"Invalid email."}}),[2];if("string"!=typeof c)return r.status(200).json({error:{code:"invalid password",message:"Invalid password."}}),[2];if(!u||!c)return r.status(200).json({error:{code:"missing email or password",message:"Email and password must be provided."}}),[2];i.label=1;case 1:return i.trys.push([1,6,,7]),[4,e.serverConfig.driver.attemptEmailPasswordLogin(u,c)];case 2:return(f=i.sent()).success?f.data.accountStatus!==a.AccountStatus.confirmed?(r.status(200).json({error:{code:"account not confirmed",message:"Your account is not confirmed yet. You need to confirm it before you can sign in."}}),[2]):e.serverConfig.triggers.fetchAdditionalTokenContent?[4,e.serverConfig.triggers.fetchAdditionalTokenContent({userId:f.data.userId})]:[3,4]:[2,s.authenticationFailed(r)];case 3:return h=i.sent(),[3,5];case 4:h={},i.label=5;case 5:return d=h,p=s.serializeAuthCookie(e.serverConfig,n(n({userId:f.data.userId},d),{provider:a.Provider.email,accountStatus:f.data.accountStatus}),{rememberMe:Boolean(l)}),r.setHeader("Set-Cookie",p),s.ok(r),[3,7];case 6:return v=i.sent(),s.unexpectedError(r,v),[3,7];case 7:return[2]}}))}))}}},function(e,t,r){"use strict";var n=this&&this.__awaiter||function(e,t,r,n){return new(r||(r=Promise))((function(o,i){function a(e){try{u(n.next(e))}catch(e){i(e)}}function s(e){try{u(n.throw(e))}catch(e){i(e)}}function u(e){var t;e.done?o(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(a,s)}u((n=n.apply(e,t||[])).next())}))},o=this&&this.__generator||function(e,t){var r,n,o,i,a={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function s(i){return function(s){return function(i){if(r)throw new TypeError("Generator is already executing.");for(;a;)try{if(r=1,n&&(o=2&i[0]?n.return:i[0]?n.throw||((o=n.return)&&o.call(n),0):n.next)&&!(o=o.call(n,i[1])).done)return o;switch(n=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return a.label++,{value:i[1],done:!1};case 5:a.label++,n=i[1],i=[0];continue;case 7:i=a.ops.pop(),a.trys.pop();continue;default:if(!(o=a.trys,(o=o.length>0&&o[o.length-1])||6!==i[0]&&2!==i[0])){a=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){a.label=i[1];break}if(6===i[0]&&a.label<o[1]){a.label=o[1],o=i;break}if(o&&a.label<o[2]){a.label=o[2],a.ops.push(i);break}o[2]&&a.ops.pop(),a.trys.pop();continue}i=t.call(e,a)}catch(e){i=[6,e],n=0}finally{r=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,s])}}},i=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(t,"__esModule",{value:!0}),t.createLogout=void 0;var a=i(r(14)),s=r(10);t.createLogout=function(e){return function(t,r){return n(this,void 0,void 0,(function(){var t;return o(this,(function(n){return t=a.default.serialize(e.serverConfig.cookieName,"",{sameSite:"lax",secure:e.serverConfig.secure,maxAge:-1,httpOnly:!0,path:"/"}),r.setHeader("Set-Cookie",t),s.ok(r),[2]}))}))}}},function(e,t,r){"use strict";var n=this&&this.__awaiter||function(e,t,r,n){return new(r||(r=Promise))((function(o,i){function a(e){try{u(n.next(e))}catch(e){i(e)}}function s(e){try{u(n.throw(e))}catch(e){i(e)}}function u(e){var t;e.done?o(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(a,s)}u((n=n.apply(e,t||[])).next())}))},o=this&&this.__generator||function(e,t){var r,n,o,i,a={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function s(i){return function(s){return function(i){if(r)throw new TypeError("Generator is already executing.");for(;a;)try{if(r=1,n&&(o=2&i[0]?n.return:i[0]?n.throw||((o=n.return)&&o.call(n),0):n.next)&&!(o=o.call(n,i[1])).done)return o;switch(n=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return a.label++,{value:i[1],done:!1};case 5:a.label++,n=i[1],i=[0];continue;case 7:i=a.ops.pop(),a.trys.pop();continue;default:if(!(o=a.trys,(o=o.length>0&&o[o.length-1])||6!==i[0]&&2!==i[0])){a=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){a.label=i[1];break}if(6===i[0]&&a.label<o[1]){a.label=o[1],o=i;break}if(o&&a.label<o[2]){a.label=o[2],a.ops.push(i);break}o[2]&&a.ops.pop(),a.trys.pop();continue}i=t.call(e,a)}catch(e){i=[6,e],n=0}finally{r=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,s])}}},i=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(t,"__esModule",{value:!0}),t.createSignup=t.sendConfirmAccountMailToConsole=void 0;var a=r(10),s=i(r(15));t.sendConfirmAccountMailToConsole=function(e,t){return n(void 0,void 0,void 0,(function(){return o(this,(function(r){return console.log(["","***********************************************************************","To: "+e,"***********************************************************************","","Welcome,","","your account has been created.","","Click the link below to activate it:",t,"","Cheers","","PS: If you did not sign up, you can simply ignore this email.","","***********************************************************************",""].join("\n")),[2]}))}))};t.createSignup=function(e){if(!e.serverConfig.tokenSecret)throw new Error("HappyAuth: Missing token secret");return function(t,r){return n(this,void 0,void 0,(function(){var n,i,u,c,l,f,d,h,p;return o(this,(function(o){switch(o.label){case 0:if(n=t.body,i=n.email,u=n.password,"string"!=typeof i)return r.status(200).json({error:{code:"invalid email",message:"Invalid email."}}),[2];if("string"!=typeof u)return r.status(200).json({error:{code:"invalid password",message:"Invalid password."}}),[2];if(!i||!u)return r.status(200).json({error:{code:"missing email or password",message:"Email and password must be provided."}}),[2];o.label=1;case 1:return o.trys.push([1,5,,6]),c=i.trim().toLowerCase(),l=u.trim(),void 0===v&&(v=200),f=new Promise((function(e){return setTimeout(e,v)})),[4,e.serverConfig.driver.createEmailUser(c,l)];case 2:return d=o.sent(),[4,f];case 3:return o.sent(),d.success?(h=s.default.sign({userId:d.data.userId},e.serverConfig.tokenSecret,{expiresIn:"1h"}),p=e.publicConfig.baseUrl+"/confirm-account#token="+h,a.ok(r),[4,e.serverConfig.triggers.sendConfirmAccountMail(i,p)]):(a.ok(r),[2]);case 4:return o.sent(),[3,6];case 5:return o.sent(),a.unexpectedError(r),[3,6];case 6:return[2]}var v}))}))}}},function(e,t,r){"use strict";var n=this&&this.__awaiter||function(e,t,r,n){return new(r||(r=Promise))((function(o,i){function a(e){try{u(n.next(e))}catch(e){i(e)}}function s(e){try{u(n.throw(e))}catch(e){i(e)}}function u(e){var t;e.done?o(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(a,s)}u((n=n.apply(e,t||[])).next())}))},o=this&&this.__generator||function(e,t){var r,n,o,i,a={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function s(i){return function(s){return function(i){if(r)throw new TypeError("Generator is already executing.");for(;a;)try{if(r=1,n&&(o=2&i[0]?n.return:i[0]?n.throw||((o=n.return)&&o.call(n),0):n.next)&&!(o=o.call(n,i[1])).done)return o;switch(n=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return a.label++,{value:i[1],done:!1};case 5:a.label++,n=i[1],i=[0];continue;case 7:i=a.ops.pop(),a.trys.pop();continue;default:if(!(o=a.trys,(o=o.length>0&&o[o.length-1])||6!==i[0]&&2!==i[0])){a=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){a.label=i[1];break}if(6===i[0]&&a.label<o[1]){a.label=o[1],o=i;break}if(o&&a.label<o[2]){a.label=o[2],a.ops.push(i);break}o[2]&&a.ops.pop(),a.trys.pop();continue}i=t.call(e,a)}catch(e){i=[6,e],n=0}finally{r=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,s])}}};Object.defineProperty(t,"__esModule",{value:!0}),t.createTokenContent=void 0,t.createTokenContent=function(e){var t=this;return function(r,i){return n(t,void 0,void 0,(function(){var t;return o(this,(function(n){return t=e.getServerSideAuth(r),i.status(200).json(t?{data:t}:{data:null}),[2]}))}))}}},function(e,t,r){"use strict";var n=this&&this.__awaiter||function(e,t,r,n){return new(r||(r=Promise))((function(o,i){function a(e){try{u(n.next(e))}catch(e){i(e)}}function s(e){try{u(n.throw(e))}catch(e){i(e)}}function u(e){var t;e.done?o(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(a,s)}u((n=n.apply(e,t||[])).next())}))},o=this&&this.__generator||function(e,t){var r,n,o,i,a={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function s(i){return function(s){return function(i){if(r)throw new TypeError("Generator is already executing.");for(;a;)try{if(r=1,n&&(o=2&i[0]?n.return:i[0]?n.throw||((o=n.return)&&o.call(n),0):n.next)&&!(o=o.call(n,i[1])).done)return o;switch(n=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return a.label++,{value:i[1],done:!1};case 5:a.label++,n=i[1],i=[0];continue;case 7:i=a.ops.pop(),a.trys.pop();continue;default:if(!(o=a.trys,(o=o.length>0&&o[o.length-1])||6!==i[0]&&2!==i[0])){a=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){a.label=i[1];break}if(6===i[0]&&a.label<o[1]){a.label=o[1],o=i;break}if(o&&a.label<o[2]){a.label=o[2],a.ops.push(i);break}o[2]&&a.ops.pop(),a.trys.pop();continue}i=t.call(e,a)}catch(e){i=[6,e],n=0}finally{r=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,s])}}},i=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(t,"__esModule",{value:!0}),t.createForgotPassword=t.sendForgotPasswordMailToConsole=void 0;var a=i(r(15)),s=r(10);t.sendForgotPasswordMailToConsole=function(e,t){return n(void 0,void 0,void 0,(function(){return o(this,(function(r){return console.log(["","***********************************************************************","To: "+e,"***********************************************************************","","Hello,","","somebody requested a reset of your password.","Click the link below to reset it:",t,"","Cheers","","***********************************************************************",""].join("\n")),[2]}))}))};t.createForgotPassword=function(e){if(!e.serverConfig.tokenSecret)throw new Error("HappyAuth: Missing token secret");return function(t,r){return n(this,void 0,void 0,(function(){var n,i,u,c;return o(this,(function(o){switch(o.label){case 0:if("string"!=typeof(n=t.body.email))return r.status(200).json({error:{code:"invalid email",message:"Email must be provided as a string."}}),[2];if(""===n.trim())return r.status(200).json({error:{code:"missing email",message:"Email must be provided."}}),[2];o.label=1;case 1:return o.trys.push([1,4,,5]),[4,e.serverConfig.driver.getUserIdByEmail(n)];case 2:return i=o.sent(),void 0===l&&(l=200),u=new Promise((function(e){return setTimeout(e,l)})),i&&new Promise((function(t,r){a.default.sign({userId:i},e.serverConfig.tokenSecret,{expiresIn:"1h"},(function(o,i){if(o)return r(o);var a=e.publicConfig.baseUrl+"/reset-password#token="+i;t(e.serverConfig.triggers.sendForgotPasswordMail(n,a))}))})),[4,u];case 3:return o.sent(),s.ok(r),[3,5];case 4:return c=o.sent(),s.unexpectedError(r,c),[3,5];case 5:return[2]}var l}))}))}}},function(e,t,r){"use strict";var n=this&&this.__assign||function(){return(n=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)},o=this&&this.__awaiter||function(e,t,r,n){return new(r||(r=Promise))((function(o,i){function a(e){try{u(n.next(e))}catch(e){i(e)}}function s(e){try{u(n.throw(e))}catch(e){i(e)}}function u(e){var t;e.done?o(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(a,s)}u((n=n.apply(e,t||[])).next())}))},i=this&&this.__generator||function(e,t){var r,n,o,i,a={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function s(i){return function(s){return function(i){if(r)throw new TypeError("Generator is already executing.");for(;a;)try{if(r=1,n&&(o=2&i[0]?n.return:i[0]?n.throw||((o=n.return)&&o.call(n),0):n.next)&&!(o=o.call(n,i[1])).done)return o;switch(n=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return a.label++,{value:i[1],done:!1};case 5:a.label++,n=i[1],i=[0];continue;case 7:i=a.ops.pop(),a.trys.pop();continue;default:if(!(o=a.trys,(o=o.length>0&&o[o.length-1])||6!==i[0]&&2!==i[0])){a=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){a.label=i[1];break}if(6===i[0]&&a.label<o[1]){a.label=o[1],o=i;break}if(o&&a.label<o[2]){a.label=o[2],a.ops.push(i);break}o[2]&&a.ops.pop(),a.trys.pop();continue}i=t.call(e,a)}catch(e){i=[6,e],n=0}finally{r=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,s])}}},a=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(t,"__esModule",{value:!0}),t.createResetPassword=void 0;var s=a(r(15)),u=r(13),c=r(10);t.createResetPassword=function(e){if(!e.serverConfig.tokenSecret)throw new Error("HappyAuth: Missing token secret");return function(t,r){return o(this,void 0,void 0,(function(){var o,a,l,f,d,h,p,v,y;return i(this,(function(i){switch(i.label){case 0:if(o=t.body,a=o.token,l=o.password,"string"!=typeof a)return r.status(200).json({error:{code:"invalid token",message:"Invalid token."}}),[2];if("string"!=typeof l)return r.status(200).json({error:{code:"invalid password",message:"Invalid password."}}),[2];if(!a)return r.status(200).json({error:{code:"missing token",message:"Token must be provided."}}),[2];if(!l)return r.status(200).json({error:{code:"missing password",message:"Password must be provided."}}),[2];i.label=1;case 1:return i.trys.push([1,6,,7]),f=s.default.verify(a,e.serverConfig.tokenSecret),d=f.userId,[4,e.serverConfig.driver.updateEmailUserPassword(d,l.trim())];case 2:return i.sent(),e.serverConfig.triggers.fetchAdditionalTokenContent?[4,e.serverConfig.triggers.fetchAdditionalTokenContent({userId:d})]:[3,4];case 3:return p=i.sent(),[3,5];case 4:p={},i.label=5;case 5:return h=p,v=c.serializeAuthCookie(e.serverConfig,n(n({userId:d},h),{provider:u.Provider.email,accountStatus:u.AccountStatus.confirmed}),{rememberMe:!1}),r.setHeader("Set-Cookie",v),c.ok(r),[3,7];case 6:return"jwt expired"===(y=i.sent()).message?c.jwtExpired(r):c.unexpectedError(r,y),[3,7];case 7:return[2]}}))}))}}},function(e,t,r){"use strict";var n=this&&this.__awaiter||function(e,t,r,n){return new(r||(r=Promise))((function(o,i){function a(e){try{u(n.next(e))}catch(e){i(e)}}function s(e){try{u(n.throw(e))}catch(e){i(e)}}function u(e){var t;e.done?o(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(a,s)}u((n=n.apply(e,t||[])).next())}))},o=this&&this.__generator||function(e,t){var r,n,o,i,a={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function s(i){return function(s){return function(i){if(r)throw new TypeError("Generator is already executing.");for(;a;)try{if(r=1,n&&(o=2&i[0]?n.return:i[0]?n.throw||((o=n.return)&&o.call(n),0):n.next)&&!(o=o.call(n,i[1])).done)return o;switch(n=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return a.label++,{value:i[1],done:!1};case 5:a.label++,n=i[1],i=[0];continue;case 7:i=a.ops.pop(),a.trys.pop();continue;default:if(!(o=a.trys,(o=o.length>0&&o[o.length-1])||6!==i[0]&&2!==i[0])){a=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){a.label=i[1];break}if(6===i[0]&&a.label<o[1]){a.label=o[1],o=i;break}if(o&&a.label<o[2]){a.label=o[2],a.ops.push(i);break}o[2]&&a.ops.pop(),a.trys.pop();continue}i=t.call(e,a)}catch(e){i=[6,e],n=0}finally{r=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,s])}}};Object.defineProperty(t,"__esModule",{value:!0}),t.createChangePassword=void 0;var i=r(10);t.createChangePassword=function(e){if(!e.serverConfig.tokenSecret)throw new Error("HappyAuth: Missing token secret");return function(t,r){return n(this,void 0,void 0,(function(){var n,a,s,u,c;return o(this,(function(o){switch(o.label){case 0:if("signedIn"!==(n=e.getServerSideAuth(t)).value)return[2,i.unauthorized(r)];if(a=t.body,s=a.currentPassword,u=a.newPassword,"string"!=typeof s)return r.status(200).json({error:{code:"invalid current password",message:"Invalid current password."}}),[2];if("string"!=typeof u)return r.status(200).json({error:{code:"invalid new password",message:"Invalid new password."}}),[2];o.label=1;case 1:return o.trys.push([1,3,,4]),s?u?[4,e.serverConfig.driver.changeEmailUserPassword(n.context.tokenData.userId,s.trim(),u.trim())]:(r.status(200).json({error:{code:"missing new password",message:"New password must be provided."}}),[2]):(r.status(200).json({error:{code:"missing current password",message:"Current password must be provided."}}),[2]);case 2:return o.sent(),i.ok(r),[3,4];case 3:return"jwt expired"===(c=o.sent()).message?i.jwtExpired(r):(c.message="authentication failed")?i.authenticationFailed(r):(console.log(c),i.unexpectedError(r,c)),[3,4];case 4:return[2]}}))}))}}},function(e,t,r){"use strict";var n=this&&this.__assign||function(){return(n=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)},o=this&&this.__awaiter||function(e,t,r,n){return new(r||(r=Promise))((function(o,i){function a(e){try{u(n.next(e))}catch(e){i(e)}}function s(e){try{u(n.throw(e))}catch(e){i(e)}}function u(e){var t;e.done?o(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(a,s)}u((n=n.apply(e,t||[])).next())}))},i=this&&this.__generator||function(e,t){var r,n,o,i,a={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function s(i){return function(s){return function(i){if(r)throw new TypeError("Generator is already executing.");for(;a;)try{if(r=1,n&&(o=2&i[0]?n.return:i[0]?n.throw||((o=n.return)&&o.call(n),0):n.next)&&!(o=o.call(n,i[1])).done)return o;switch(n=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return a.label++,{value:i[1],done:!1};case 5:a.label++,n=i[1],i=[0];continue;case 7:i=a.ops.pop(),a.trys.pop();continue;default:if(!(o=a.trys,(o=o.length>0&&o[o.length-1])||6!==i[0]&&2!==i[0])){a=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){a.label=i[1];break}if(6===i[0]&&a.label<o[1]){a.label=o[1],o=i;break}if(o&&a.label<o[2]){a.label=o[2],a.ops.push(i);break}o[2]&&a.ops.pop(),a.trys.pop();continue}i=t.call(e,a)}catch(e){i=[6,e],n=0}finally{r=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,s])}}},a=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(t,"__esModule",{value:!0}),t.createConfirmAccount=void 0;var s=r(13),u=r(10),c=a(r(15));t.createConfirmAccount=function(e){if(!e.serverConfig.tokenSecret)throw new Error("HappyAuth: Missing token secret");return function(t,r){return o(this,void 0,void 0,(function(){var o,a,l,f,d,h,p;return i(this,(function(i){switch(i.label){case 0:if(!(o=t.body.token))return r.status(500).json({error:{code:"token missing"}}),[2];i.label=1;case 1:return i.trys.push([1,8,,9]),a=c.default.verify(o,e.serverConfig.tokenSecret),[4,e.serverConfig.driver.confirmAccount(a.userId)];case 2:return i.sent()?[3,3]:(r.status(200).json({error:{code:"no user or user in invalid state"}}),[2]);case 3:return l=a.userId,e.serverConfig.triggers.fetchAdditionalTokenContent?[4,e.serverConfig.triggers.fetchAdditionalTokenContent({userId:l})]:[3,5];case 4:return d=i.sent(),[3,6];case 5:d={},i.label=6;case 6:f=d,h=u.serializeAuthCookie(e.serverConfig,n(n({userId:l},f),{provider:s.Provider.email,accountStatus:s.AccountStatus.confirmed}),{rememberMe:!1}),r.setHeader("Set-Cookie",h),u.ok(r),i.label=7;case 7:return[3,9];case 8:return p=i.sent(),u.unexpectedError(r,p),[3,9];case 9:return[2]}}))}))}}},function(e,t,r){"use strict";var n=this&&this.__awaiter||function(e,t,r,n){return new(r||(r=Promise))((function(o,i){function a(e){try{u(n.next(e))}catch(e){i(e)}}function s(e){try{u(n.throw(e))}catch(e){i(e)}}function u(e){var t;e.done?o(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(a,s)}u((n=n.apply(e,t||[])).next())}))},o=this&&this.__generator||function(e,t){var r,n,o,i,a={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function s(i){return function(s){return function(i){if(r)throw new TypeError("Generator is already executing.");for(;a;)try{if(r=1,n&&(o=2&i[0]?n.return:i[0]?n.throw||((o=n.return)&&o.call(n),0):n.next)&&!(o=o.call(n,i[1])).done)return o;switch(n=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return a.label++,{value:i[1],done:!1};case 5:a.label++,n=i[1],i=[0];continue;case 7:i=a.ops.pop(),a.trys.pop();continue;default:if(!(o=a.trys,(o=o.length>0&&o[o.length-1])||6!==i[0]&&2!==i[0])){a=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){a.label=i[1];break}if(6===i[0]&&a.label<o[1]){a.label=o[1],o=i;break}if(o&&a.label<o[2]){a.label=o[2],a.ops.push(i);break}o[2]&&a.ops.pop(),a.trys.pop();continue}i=t.call(e,a)}catch(e){i=[6,e],n=0}finally{r=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,s])}}},i=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(t,"__esModule",{value:!0}),t.createResendConfirmationEmail=void 0;var a=r(10),s=i(r(15));t.createResendConfirmationEmail=function(e){if(!e.serverConfig.tokenSecret)throw new Error("HappyAuth: Missing token secret");return function(t,r){return n(this,void 0,void 0,(function(){var n,i,u,c,l;return o(this,(function(o){switch(o.label){case 0:if("string"!=typeof(n=t.body.email))return r.status(200).json({error:{code:"invalid email",message:"Invalid email."}}),[2];if(""===n.trim())return r.status(200).json({error:{code:"missing email",message:"Email must be provided."}}),[2];o.label=1;case 1:return o.trys.push([1,4,,5]),[4,e.serverConfig.driver.getUserIdByEmail(n.trim().toLowerCase())];case 2:return(i=o.sent())?(u=s.default.sign({userId:i},e.serverConfig.tokenSecret,{expiresIn:"1h"}),c=e.publicConfig.baseUrl+"/confirm-account#token="+u,[4,e.serverConfig.triggers.sendConfirmAccountMail(n,c)]):(a.ok(r),[2]);case 3:return o.sent(),a.ok(r),[3,5];case 4:return l=o.sent(),a.unexpectedError(r,l),[3,5];case 5:return[2]}}))}))}}},function(e,t,r){"use strict";var n=this&&this.__assign||function(){return(n=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)},o=this&&this.__awaiter||function(e,t,r,n){return new(r||(r=Promise))((function(o,i){function a(e){try{u(n.next(e))}catch(e){i(e)}}function s(e){try{u(n.throw(e))}catch(e){i(e)}}function u(e){var t;e.done?o(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(a,s)}u((n=n.apply(e,t||[])).next())}))},i=this&&this.__generator||function(e,t){var r,n,o,i,a={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function s(i){return function(s){return function(i){if(r)throw new TypeError("Generator is already executing.");for(;a;)try{if(r=1,n&&(o=2&i[0]?n.return:i[0]?n.throw||((o=n.return)&&o.call(n),0):n.next)&&!(o=o.call(n,i[1])).done)return o;switch(n=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return a.label++,{value:i[1],done:!1};case 5:a.label++,n=i[1],i=[0];continue;case 7:i=a.ops.pop(),a.trys.pop();continue;default:if(!(o=a.trys,(o=o.length>0&&o[o.length-1])||6!==i[0]&&2!==i[0])){a=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){a.label=i[1];break}if(6===i[0]&&a.label<o[1]){a.label=o[1],o=i;break}if(o&&a.label<o[2]){a.label=o[2],a.ops.push(i);break}o[2]&&a.ops.pop(),a.trys.pop();continue}i=t.call(e,a)}catch(e){i=[6,e],n=0}finally{r=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,s])}}},a=this&&this.__spreadArrays||function(){for(var e=0,t=0,r=arguments.length;t<r;t++)e+=arguments[t].length;var n=Array(e),o=0;for(t=0;t<r;t++)for(var i=arguments[t],a=0,s=i.length;a<s;a++,o++)n[o]=i[a];return n},s=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(t,"__esModule",{value:!0}),t.createOAuth=void 0;var u=s(r(14)),c=r(13),l=r(10),f=r(35),d=s(r(36));var h=function(e){return Array.isArray(e)?e[0]:e};t.createOAuth=function(e){if(!e.serverConfig.tokenSecret)throw new Error("HappyAuth: Missing token secret");return function(t,r){var s;return o(this,void 0,void 0,(function(){var o,p,v,y,g,w,b,m,k,_,S,C,x,P,j;return i(this,(function(i){switch(i.label){case 0:if((o=(I=t.query.params,Array.isArray(I)?I:[I]).slice(1)).length<2)return r.status(200).json({error:{code:"missing identity_provider or method",message:"Provide an identity_provider and a method."}}),[2];if(!(p=o[0]))return[2,r.status(200).json({error:{code:"missing identity_provider"}})];if(A=e.serverConfig.identityProviders,O=p,!Object.prototype.hasOwnProperty.call(A,O))return[2,r.status(200).json({error:{code:"unknown identity_provider"}})];if(v=o[1],y=e.serverConfig.identityProviders[p],g=e.publicConfig.baseUrl+"/api/auth/oauth/"+p+"/idpresponse","authorize"===v&&(void 0===(E=32)&&(E=21),w=d.default.randomBytes(E).toString("hex").slice(0,E),m=f.create(y.credentials),b=m.authorizationCode.authorizeURL({redirect_uri:g,scope:y.scope,state:w}),r.setHeader("Set-Cookie",u.default.serialize("oauth2state",w,{sameSite:"lax",secure:e.serverConfig.secure,httpOnly:!0,path:"/"})),r.setHeader("Location",b),r.setHeader("Cache-Control","no-cache"),r.statusMessage="Found",r.status(302).end()),"idpresponse"!==v)return[3,8];if(m=f.create(y.credentials),t.query.state!==t.cookies.oauth2state)return[2,r.status(500).json({error:{code:"open authentication failed",message:"Open Authentication failed"}})];i.label=1;case 1:return i.trys.push([1,7,,8]),[4,m.authorizationCode.getToken({code:h(t.query.code),redirect_uri:g})];case 2:return k=i.sent(),[4,e.serverConfig.identityProviders[p].upsertUser(k)];case 3:return _=i.sent(),e.serverConfig.triggers.fetchAdditionalTokenContent?[4,e.serverConfig.triggers.fetchAdditionalTokenContent({userId:_,oauthToken:k})]:[3,5];case 4:return C=i.sent(),[3,6];case 5:C={},i.label=6;case 6:return S=C,x=l.serializeAuthCookie(e.serverConfig,n(n({userId:_},S),{provider:String(p),accountStatus:c.AccountStatus.confirmed}),{rememberMe:!1}),P=u.default.serialize("oauth2state","",{sameSite:"lax",secure:e.serverConfig.secure,httpOnly:!0,path:"/",maxAge:-1}),r.setHeader("Set-Cookie",a([P],x)),r.setHeader("Location",e.publicConfig.baseUrl+((null===(s=e.publicConfig.redirects)||void 0===s?void 0:s.afterSignIn)||"/")),r.statusMessage="Found",r.status(302).end(),[2];case 7:return j=i.sent(),console.error("Access Token Error",j.message),[2,r.status(200).json({error:{code:"authentication failed",message:"Authentication failed"}})];case 8:return[2]}var E,A,O,I}))}))}}},function(e,t){e.exports=require("simple-oauth2")},function(e,t){e.exports=require("crypto")},function(e,t,r){"use strict";var n=this&&this.__awaiter||function(e,t,r,n){return new(r||(r=Promise))((function(o,i){function a(e){try{u(n.next(e))}catch(e){i(e)}}function s(e){try{u(n.throw(e))}catch(e){i(e)}}function u(e){var t;e.done?o(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(a,s)}u((n=n.apply(e,t||[])).next())}))},o=this&&this.__generator||function(e,t){var r,n,o,i,a={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function s(i){return function(s){return function(i){if(r)throw new TypeError("Generator is already executing.");for(;a;)try{if(r=1,n&&(o=2&i[0]?n.return:i[0]?n.throw||((o=n.return)&&o.call(n),0):n.next)&&!(o=o.call(n,i[1])).done)return o;switch(n=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return a.label++,{value:i[1],done:!1};case 5:a.label++,n=i[1],i=[0];continue;case 7:i=a.ops.pop(),a.trys.pop();continue;default:if(!(o=a.trys,(o=o.length>0&&o[o.length-1])||6!==i[0]&&2!==i[0])){a=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){a.label=i[1];break}if(6===i[0]&&a.label<o[1]){a.label=o[1],o=i;break}if(o&&a.label<o[2]){a.label=o[2],a.ops.push(i);break}o[2]&&a.ops.pop(),a.trys.pop();continue}i=t.call(e,a)}catch(e){i=[6,e],n=0}finally{r=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,s])}}};Object.defineProperty(t,"__esModule",{value:!0}),t.createConnect=void 0;var i=r(10);t.createConnect=function(e){return function(e,t){return n(this,void 0,void 0,(function(){return o(this,(function(e){return i.ok(t),[2]}))}))}}},function(e,t){e.exports=require("ms")},function(e,t,r){"use strict";var n=this&&this.__awaiter||function(e,t,r,n){return new(r||(r=Promise))((function(o,i){function a(e){try{u(n.next(e))}catch(e){i(e)}}function s(e){try{u(n.throw(e))}catch(e){i(e)}}function u(e){var t;e.done?o(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(a,s)}u((n=n.apply(e,t||[])).next())}))},o=this&&this.__generator||function(e,t){var r,n,o,i,a={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function s(i){return function(s){return function(i){if(r)throw new TypeError("Generator is already executing.");for(;a;)try{if(r=1,n&&(o=2&i[0]?n.return:i[0]?n.throw||((o=n.return)&&o.call(n),0):n.next)&&!(o=o.call(n,i[1])).done)return o;switch(n=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return a.label++,{value:i[1],done:!1};case 5:a.label++,n=i[1],i=[0];continue;case 7:i=a.ops.pop(),a.trys.pop();continue;default:if(!(o=a.trys,(o=o.length>0&&o[o.length-1])||6!==i[0]&&2!==i[0])){a=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){a.label=i[1];break}if(6===i[0]&&a.label<o[1]){a.label=o[1],o=i;break}if(o&&a.label<o[2]){a.label=o[2],a.ops.push(i);break}o[2]&&a.ops.pop(),a.trys.pop();continue}i=t.call(e,a)}catch(e){i=[6,e],n=0}finally{r=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,s])}}};Object.defineProperty(t,"__esModule",{value:!0}),t.createFaunaEmailDriver=void 0;var i=r(13),a=r(40);t.createFaunaEmailDriver=function(e){var t=this;return{attemptEmailPasswordLogin:function(r,i){return n(t,void 0,void 0,(function(){var t,n;return o(this,(function(o){switch(o.label){case 0:return o.trys.push([0,2,,3]),[4,e.query(a.query.Let({loginData:a.query.Login(a.query.Match(a.query.Index("users_by_email"),r),{password:i})},{userRef:a.query.Select(["instance"],a.query.Var("loginData")),accountStatus:a.query.Select(["data","accountStatus"],a.query.Get(a.query.Select("instance",a.query.Var("loginData"))))}))];case 1:return[2,{success:!0,data:{userId:(t=o.sent()).userRef.id,accountStatus:t.accountStatus}}];case 2:if("authentication failed"===(n=o.sent()).message)return[2,{success:!1,reason:"authentication failed"}];throw n;case 3:return[2]}}))}))},createEmailUser:function(r,s){return n(t,void 0,void 0,(function(){var t;return o(this,(function(n){switch(n.label){case 0:return n.trys.push([0,2,,3]),[4,e.query(a.query.Create(a.query.Collection("User"),{credentials:{password:s},data:{email:r,created:a.query.Now(),accountStatus:i.AccountStatus.unconfirmed}}))];case 1:return[2,{success:!0,data:{userId:n.sent().ref.id}}];case 2:if("instance not unique"===(t=n.sent()).message)return[2,{success:!1,reason:"instance not unique"}];throw t;case 3:return[2]}}))}))},getUserIdByEmail:function(r){return n(t,void 0,void 0,(function(){return o(this,(function(t){switch(t.label){case 0:return[4,e.query(a.query.Let({match:a.query.Match(a.query.Index("users_by_email"),r.toLowerCase())},a.query.If(a.query.Exists(a.query.Var("match")),a.query.Select(["ref","id"],a.query.Get(a.query.Var("match"))),!1)))];case 1:return[2,t.sent()||null]}}))}))},updateEmailUserPassword:function(r,i){return n(t,void 0,void 0,(function(){return o(this,(function(t){switch(t.label){case 0:return[4,e.query(a.query.Update(a.query.Ref(a.query.Collection("User"),r),{credentials:{password:i}}))];case 1:return t.sent(),[2]}}))}))},changeEmailUserPassword:function(r,i,s){return n(t,void 0,void 0,(function(){return o(this,(function(t){switch(t.label){case 0:return[4,e.query(a.query.Do(a.query.Login(a.query.Ref(a.query.Collection("User"),r),{password:i}),a.query.Update(a.query.Ref(a.query.Collection("User"),r),{credentials:{password:s}})))];case 1:return t.sent(),[2]}}))}))},confirmAccount:function(r){return n(t,void 0,void 0,(function(){return o(this,(function(t){return[2,e.query(a.query.Let({userRef:a.query.Ref(a.query.Collection("User"),r)},a.query.If(a.query.Exists(a.query.Var("userRef")),a.query.Let({accountStatus:a.query.Select(["data","accountStatus"],a.query.Get(a.query.Var("userRef")))},a.query.If(a.query.Or(a.query.Equals(a.query.Var("accountStatus"),i.AccountStatus.confirmed),a.query.Equals(a.query.Var("accountStatus"),i.AccountStatus.unconfirmed)),a.query.Do(a.query.Update(a.query.Var("userRef"),{data:{accountStatus:i.AccountStatus.confirmed}}),!0),!1)),!1)))]}))}))}}}},function(e,t){e.exports=require("faunadb")}]);