@frank-auth/react
Version:
Flexible and customizable React UI components for Frank Authentication
4 lines (3 loc) • 8.23 kB
JavaScript
'use client';
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const e=require("react/jsx-runtime"),y=require("react"),N=require("framer-motion"),b=require("../../../hooks/use-oauth.cjs"),R=require("../../../hooks/use-magic-link.cjs"),P=require("../../../hooks/use-organization.cjs"),g=require("./loading-spinner.cjs"),E=require("./error-boundary.cjs"),S=t=>t&&t.__esModule?t:{default:t},c=S(y);function _(){return typeof window>"u"?new URLSearchParams:new URLSearchParams(window.location.search)}function M(){if(typeof window>"u")return new URLSearchParams;const t=window.location.hash;return new URLSearchParams(t.startsWith("#")?t.slice(1):t)}function w(){const t=_();return M().forEach((s,r)=>{t.has(r)||t.set(r,s)}),t}function k(){const t=w();if(t.has("code")&&t.has("state"))return"oauth";if(t.has("token")&&t.has("type")){const a=t.get("type");if(a==="magic_link"||a==="email_verification")return"magic-link"}return t.has("invitation_token")||t.has("invite")?"invitation":t.has("verification_token")||t.has("verify")?"verification":t.has("error")||t.has("error_description")?"error":null}function T({onSuccess:t,onError:a,successUrl:s,errorUrl:r}){const{handleCallback:o}=b.useOAuth(),[h,u]=c.default.useState(!0),[d,l]=c.default.useState(null);return c.default.useEffect(()=>{(async()=>{try{const n=w(),i=n.get("code"),p=n.get("state"),v=n.get("provider")||"google",x=n.get("error");if(x){const j=n.get("error_description")||"OAuth authentication failed";throw new Error(`OAuth Error: ${x} - ${j}`)}if(!i)throw new Error("No authorization code received from OAuth provider");const f=await o(v,i,p);if(f.success)t?.(f),s?window.location.href=s:(window.history.replaceState({},document.title,window.location.pathname),window.location.href="/dashboard");else throw new Error(f.error||"OAuth authentication failed")}catch(n){const i=n instanceof Error?n:new Error("OAuth processing failed");l(i),a?.(i),r&&setTimeout(()=>{window.location.href=r},3e3)}finally{u(!1)}})()},[o,t,a,s,r]),d?e.jsxs("div",{className:"text-center",children:[e.jsxs("div",{className:"text-danger-600 mb-4",children:[e.jsx("svg",{className:"w-12 h-12 mx-auto mb-2",fill:"none",stroke:"currentColor",viewBox:"0 0 24 24",children:e.jsx("path",{strokeLinecap:"round",strokeLinejoin:"round",strokeWidth:2,d:"M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-2.5L13.732 4c-.77-.833-1.732-.833-2.5 0L4.732 16.5c-.77.833.192 2.5 1.732 2.5z"})}),e.jsx("h3",{className:"text-lg font-semibold",children:"Authentication Failed"}),e.jsx("p",{className:"text-sm text-default-500 mt-2",children:d.message})]}),r&&e.jsx("p",{className:"text-xs text-default-400",children:"Redirecting to sign in page..."})]}):e.jsx(g.LoadingSpinner,{variant:"spinner",size:"lg",showText:!0,text:"Completing sign in...",centered:!0})}function A({onSuccess:t,onError:a,successUrl:s,errorUrl:r}){const{verifyFromUrl:o}=R.useMagicLink(),[h,u]=c.default.useState(!0),[d,l]=c.default.useState(null);return c.default.useEffect(()=>{(async()=>{try{const n=await o();if(n.success){if(t?.(n),n.requiresAdditionalVerification){const i=new URLSearchParams;i.set("mfa_token",n.mfaToken||""),window.location.href=`/auth/mfa?${i.toString()}`;return}s?window.location.href=s:(window.history.replaceState({},document.title,window.location.pathname),window.location.href="/dashboard")}else throw new Error(n.error||"Magic link verification failed")}catch(n){const i=n instanceof Error?n:new Error("Magic link processing failed");l(i),a?.(i),r&&setTimeout(()=>{window.location.href=r},3e3)}finally{u(!1)}})()},[o,t,a,s,r]),d?e.jsxs("div",{className:"text-center",children:[e.jsxs("div",{className:"text-danger-600 mb-4",children:[e.jsx("svg",{className:"w-12 h-12 mx-auto mb-2",fill:"none",stroke:"currentColor",viewBox:"0 0 24 24",children:e.jsx("path",{strokeLinecap:"round",strokeLinejoin:"round",strokeWidth:2,d:"M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-2.5L13.732 4c-.77-.833-1.732-.833-2.5 0L4.732 16.5c-.77.833.192 2.5 1.732 2.5z"})}),e.jsx("h3",{className:"text-lg font-semibold",children:"Verification Failed"}),e.jsx("p",{className:"text-sm text-default-500 mt-2",children:d.message})]}),r&&e.jsx("p",{className:"text-xs text-default-400",children:"Redirecting to sign in page..."})]}):e.jsx(g.LoadingSpinner,{variant:"pulse",size:"lg",showText:!0,text:"Verifying magic link...",centered:!0})}function O({onSuccess:t,onError:a,successUrl:s,errorUrl:r}){const{acceptInvitation:o}=P.useOrganization(),[h,u]=c.default.useState(!0),[d,l]=c.default.useState(null);return c.default.useEffect(()=>{(async()=>{try{const n=w(),i=n.get("invitation_token")||n.get("invite");if(!i)throw new Error("No invitation token found");await o(i),t?.({invitationToken:i}),s?window.location.href=s:(window.history.replaceState({},document.title,window.location.pathname),window.location.href="/dashboard")}catch(n){const i=n instanceof Error?n:new Error("Invitation processing failed");l(i),a?.(i),r&&setTimeout(()=>{window.location.href=r},3e3)}finally{u(!1)}})()},[o,t,a,s,r]),d?e.jsxs("div",{className:"text-center",children:[e.jsxs("div",{className:"text-danger-600 mb-4",children:[e.jsx("svg",{className:"w-12 h-12 mx-auto mb-2",fill:"none",stroke:"currentColor",viewBox:"0 0 24 24",children:e.jsx("path",{strokeLinecap:"round",strokeLinejoin:"round",strokeWidth:2,d:"M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-2.5L13.732 4c-.77-.833-1.732-.833-2.5 0L4.732 16.5c-.77.833.192 2.5 1.732 2.5z"})}),e.jsx("h3",{className:"text-lg font-semibold",children:"Invitation Failed"}),e.jsx("p",{className:"text-sm text-default-500 mt-2",children:d.message})]}),r&&e.jsx("p",{className:"text-xs text-default-400",children:"Redirecting to sign in page..."})]}):e.jsx(g.LoadingSpinner,{variant:"dots",size:"lg",showText:!0,text:"Processing invitation...",centered:!0})}function L({type:t="auto",successUrl:a,errorUrl:s,onSuccess:r,onError:o,loadingComponent:h,errorComponent:u,showLoading:d=!0,loadingText:l="Processing...",className:m="",children:n}){const i=t==="auto"?k():t;if(!i&&!n)return null;if(n)return e.jsx("div",{className:m,children:n});const p=d?h||e.jsx("div",{className:"flex items-center justify-center min-h-64",children:e.jsx(g.LoadingSpinner,{variant:"spinner",size:"lg",showText:!0,text:l,centered:!0})}):null,v=()=>{switch(i){case"oauth":return e.jsx(T,{onSuccess:r,onError:o,successUrl:a,errorUrl:s});case"magic-link":return e.jsx(A,{onSuccess:r,onError:o,successUrl:a,errorUrl:s});case"invitation":return e.jsx(O,{onSuccess:r,onError:o,successUrl:a,errorUrl:s});case"error":const x=w(),f=x.get("error_description")||x.get("error")||"An error occurred",j=new Error(f);return u?e.jsx(u,{error:j,retry:()=>window.location.reload()}):e.jsxs("div",{className:"text-center",children:[e.jsxs("div",{className:"text-danger-600 mb-4",children:[e.jsx("svg",{className:"w-12 h-12 mx-auto mb-2",fill:"none",stroke:"currentColor",viewBox:"0 0 24 24",children:e.jsx("path",{strokeLinecap:"round",strokeLinejoin:"round",strokeWidth:2,d:"M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-2.5L13.732 4c-.77-.833-1.732-.833-2.5 0L4.732 16.5c-.77.833.192 2.5 1.732 2.5z"})}),e.jsx("h3",{className:"text-lg font-semibold",children:"Authentication Error"}),e.jsx("p",{className:"text-sm text-default-500 mt-2",children:f})]}),s&&e.jsx("p",{className:"text-xs text-default-400",children:"Redirecting to sign in page..."})]});default:return p}};return e.jsx(N.motion.div,{initial:{opacity:0},animate:{opacity:1},className:`${m}`,children:e.jsx(E.ErrorBoundary,{title:"Redirect Processing Error",subtitle:"There was a problem processing the authentication redirect.",onError:o,showDetails:process.env.NODE_ENV==="development",children:v()})})}function z(){const[t,a]=c.default.useState(null),[s,r]=c.default.useState(!1),[o,h]=c.default.useState(null);c.default.useEffect(()=>{const l=k();a(l),r(!!l)},[]);const u=c.default.useCallback(l=>{r(!1),h(null)},[]),d=c.default.useCallback(l=>{r(!1),h(l)},[]);return{redirectType:t,isProcessing:s,error:o,handleSuccess:u,handleError:d,hasRedirect:!!t}}var C=L;exports.RedirectHandler=L;exports.default=C;exports.useRedirectHandler=z;
//# sourceMappingURL=redirect-handler.cjs.map