@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 • 10.4 kB
JavaScript
module.exports=function(e){var t={};function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}return n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)n.d(r,o,function(t){return e[t]}.bind(null,o));return r},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=13)}([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,n){"use strict";var r=this&&this.__createBinding||(Object.create?function(e,t,n,r){void 0===r&&(r=n),Object.defineProperty(e,r,{enumerable:!0,get:function(){return t[n]}})}:function(e,t,n,r){void 0===r&&(r=n),e[r]=t[n]}),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 n in e)"default"!==n&&Object.hasOwnProperty.call(e,n)&&r(t,e,n);return o(t,e),t},u=this&&this.__awaiter||function(e,t,n,r){return new(n||(n=Promise))((function(o,i){function u(e){try{s(r.next(e))}catch(e){i(e)}}function a(e){try{s(r.throw(e))}catch(e){i(e)}}function s(e){var t;e.done?o(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(u,a)}s((r=r.apply(e,t||[])).next())}))},a=this&&this.__generator||function(e,t){var n,r,o,i,u={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:a(0),throw:a(1),return:a(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function a(i){return function(a){return function(i){if(n)throw new TypeError("Generator is already executing.");for(;u;)try{if(n=1,r&&(o=2&i[0]?r.return:i[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,i[1])).done)return o;switch(r=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return u.label++,{value:i[1],done:!1};case 5:u.label++,r=i[1],i=[0];continue;case 7:i=u.ops.pop(),u.trys.pop();continue;default:if(!(o=u.trys,(o=o.length>0&&o[o.length-1])||6!==i[0]&&2!==i[0])){u=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){u.label=i[1];break}if(6===i[0]&&u.label<o[1]){u.label=o[1],o=i;break}if(o&&u.label<o[2]){u.label=o[2],u.ops.push(i);break}o[2]&&u.ops.pop(),u.trys.pop();continue}i=t.call(e,u)}catch(e){i=[6,e],r=0}finally{n=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,a])}}},s=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(n(0)),l=n(2),f=n(3),d=s(n(14)),h=c.createContext([null,null]),p=function(e){var t=c.useState(null),n=t[0],r=t[1],o=c.useCallback((function(e){r((function(t){if(null===t)return e;if(t===e)return t;throw new Error("AuthProvider: Only one useAuth may be used per page.")}))}),[n,r]),i=c.useCallback((function(e){r((function(t){if(t===e)return null;throw new Error("AuthProvider: Tried to unregister an inactive useAuth hook.")}))}),[n,r]);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 n=this;void 0===t&&(t={value:"authenticating",context:{tokenData:null,error:null}}),function(){var e=c.useRef(Symbol()),t=c.useContext(h),n=t[0],r=t[1];if(null===n||null===r)throw new Error("Missing AuthProvider. Add it to _app.js or _app.tsx.");c.useEffect((function(){return n(e.current),function(){r(e.current)}}),[])}();var r=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(r),i=o[0],s=o[1],p=c.useCallback((function(e,t,r){return void 0===r&&(r=!1),u(n,void 0,void 0,(function(){var n,o;return a(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:r})})];case 1:return 200!==(n=i.sent()).status?[3,2]:[2,n.json()];case 2:return o=Error.bind,[4,n.text()];case 3:throw new(o.apply(Error,[void 0,i.sent()]))}}))}))}),[s]),g=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",g),function(){window.removeEventListener("storage",g)}}),[]),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 w=c.useCallback((function(t){return void 0===t&&(t=(null===(o=null==e?void 0:e.redirects)||void 0===o?void 0:o.afterSignOut)||"/"),u(n,void 0,void 0,(function(){var e,n;return a(this,(function(r){switch(r.label){case 0:return[4,fetch("/api/auth/logout",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({})})];case 1:return 200!==(e=r.sent()).status?[3,2]:(window.localStorage.setItem("logout",String(Date.now())),window.location.href=t,[2,e.json()]);case 2:return n=Error.bind,[4,e.text()];case 3:throw new(n.apply(Error,[void 0,r.sent()]))}}))}))}),[s]),v=c.useCallback((function(e,t){return u(n,void 0,void 0,(function(){var n,r;return a(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!==(n=o.sent()).status?[3,2]:[2,n.json()];case 2:return r=Error.bind,[4,n.text()];case 3:throw new(r.apply(Error,[void 0,o.sent()]))}}))}))}),[s]),y=c.useCallback((function(e,t){return u(n,void 0,void 0,(function(){var n,r;return a(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!==(n=o.sent()).status?[3,2]:[2,n.json()];case 2:return r=Error.bind,[4,n.text()];case 3:throw new(r.apply(Error,[void 0,o.sent()]))}}))}))}),[s]),b=c.useCallback((function(e){return u(n,void 0,void 0,(function(){return a(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()]}}))}))}),[s]),m=c.useCallback((function(e,t){return u(n,void 0,void 0,(function(){var n,r;return a(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!==(n=o.sent()).status?[3,2]:[2,n.json()];case 2:return r=Error.bind,[4,n.text()];case 3:throw new(r.apply(Error,[void 0,o.sent()]))}}))}))}),[s]),S=c.useCallback((function(e){return u(n,void 0,void 0,(function(){var t,n;return a(this,(function(r){switch(r.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=r.sent()).status?[3,2]:[2,t.json()];case 2:return n=Error.bind,[4,t.text()];case 3:throw new(n.apply(Error,[void 0,r.sent()]))}}))}))}),[s]),P=c.useMemo((function(){return{signIn:p,signOut:w,signUp:v,resetPassword:y,confirmAccount:b,changePassword:m,forgotPassword:S}}),[p,w,v,y,b,m,S]);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 u(n,void 0,void 0,(function(){var t,n;return a(this,(function(r){switch(r.label){case 0:return 200!==e.status?[3,2]:[4,e.json()];case 1:return t=r.sent(),(n=t?t.data:null)?("signedIn"===n.value?s({type:"SIGN_IN_SUCCESS",tokenData:n.context.tokenData}):s({type:"SIGN_IN_FAILURE",error:""}),[3,3]):(s({type:"SIGN_IN_FAILURE",error:""}),[2]);case 2:s({type:"SIGN_IN_ERROR",error:"unexpected error"}),r.label=3;case 3:return[2]}}))}))}),(function(e){console.error(e),s({type:"SIGN_IN_ERROR",error:"json decoding failed or another unexpected error"})}))}}))}),[s,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,P)}),[i,P])}}},function(e,t){e.exports=require("cookie")}]);