1(self.webpackChunk_N_E=self.webpackChunk_N_E||[]).push([[6555],{7674:function(e,t,i){(window.__NEXT_P=window.__NEXT_P||[]).push(["/invite/[token]",function(){return i(2568)}])},2568:function(e,t,i){"use strict";i.r(t),i.d(t,{default:function(){return x}});var a=i(5893),s=i(7294),n=i(1163),o=i(8655),r=i(3050),l=i(6691),d=i(2220),c=i(8267),m=i(2126),u=i(2967),g=i(5518);function x(){var e,t,i,x;let h=(0,n.useRouter)(),{token:f}=h.query,{signUp:v,signIn:b,confirmSignUp:p,isAuthenticated:N,user:w,isLoading:j}=(0,u.aC)(),[y,I]=(0,s.useState)(null),[E,T]=(0,s.useState)(!0),[k,C]=(0,s.useState)(!1),[z,V]=(0,s.useState)(""),[S,O]=(0,s.useState)(!1),[P,F]=(0,s.useState)("form"),[U,A]=(0,s.useState)({firstName:"",lastName:"",password:"",confirmPassword:""}),[Z,R]=(0,s.useState)(!1),[q,_]=(0,s.useState)(null),[D,L]=(0,s.useState)(!1),[Y,J]=(0,s.useState)("");(0,s.useEffect)(()=>{!j&&f&&B()},[f,N,j]);let B=async()=>{try{T(!0);let e=await fetch("/api/v1/invitations/validate/".concat(f)),t=await e.json();if(!e.ok)throw Error(t.message||"Invalid invitation");I(t.data.invitation);let i=t.data.invitation.invitedEmail;i?(console.log("[INVITE] Checking existing user for invited email:",i),await G(i)):(console.error("[INVITE] No invitedEmail found in invitation data"),V("Invalid invitation: missing email"))}catch(e){console.error("Error validating invitation:",e),V(e.message)}finally{T(!1)}},G=async e=>{try{L(!0);let t=null==e?void 0:e.toLowerCase().trim();if(!t){console.error("[INVITE] Invalid email provided to checkExistingUser"),R(!0);return}console.log("[INVITE] Checking if user exists for email:",t);let i=await fetch("/api/v1/invitations/check-user",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({email:t})}),a=await i.json();if(i.ok&&a.success){console.log("[INVITE] User check response:",a.data),_(a.data);let e=!0===a.data.userExists,t=!0===a.data.hasCognitoAccount,i=!0===a.data.hasOrganizations;if(console.log("[INVITE] User existence check:",{userExists:e,hasCognitoAccount:t,hasOrganizations:i,isAuthenticated:N}),t&&!N){console.log("[INVITE] Existing user with Cognito account detected but not authenticated, redirecting to sign-in..."),localStorage.setItem("pendingInvitationToken",f),h.push("/login?redirect=/invite/".concat(f,"&invitation=accepted"));return}R(!t&&!i)}else console.warn("[INVITE] User check failed or returned error, assuming new user:",a.message),R(!0)}catch(e){console.error("[INVITE] Error checking user:",e),R(!0)}finally{L(!1)}},W=async e=>{var t,i,a,s,n;e.preventDefault(),C(!0),V("");try{if(Z){if(U.password!==U.confirmPassword)throw Error("Passwords do not match");if(U.password.length<8)throw Error("Password must be at least 8 characters long");if(!U.firstName||!U.lastName)throw Error("Please provide your first and last name");if(console.log("Creating Cognito account for new user..."),(await v({email:y.invitedEmail,password:U.password,firstName:U.firstName,lastName:U.lastName,organizationName:(null===(a=y.organization)||void 0===a?void 0:a.name)||"Default Org",invitationToken:f})).needsVerification){localStorage.setItem("pendingInvitationToken",f),F("verification"),C(!1);return}}let e={};N&&w?(e={existingUser:{id:w.sub||w.id||(null==q?void 0:q.userId)||(null==q?void 0:q.cognitoId),email:w.email||y.invitedEmail,name:w.name||(null==y?void 0:y.invitedName)||(null==q?void 0:q.name)||w.email}},console.log("[INVITE] Authenticated user accepting invitation:",e)):!Z&&q&&(e={existingUser:{id:q.userId||q.cognitoId||"unknown",email:q.email,name:(null==y?void 0:y.invitedName)||q.name||q.email}},console.log("[INVITE] Existing user accepting invitation:",e)),console.log("[INVITE] Accepting invitation with:",{isNewUser:Z,isAuthenticated:N,requestBody:e});let s=await fetch("/api/v1/invitations/accept/".concat(f),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(e)}),n=await s.json();if(!s.ok)throw Error(n.message||"Failed to accept invitation");if(O(!0),F("success"),N&&(null===(i=n.data)||void 0===i?void 0:null===(t=i.organization)||void 0===t?void 0:t.id))try{let e=n.data.organization.id,t=n.data.organization.name||"the organization";console.log("[INVITE] Automatically switching to new organization:",e);let i=await g.default.request("/organization-switch-requests/set-primary",{method:"POST",body:JSON.stringify({organizationId:e})});if(i.ok){let e=await i.json();console.log("[INVITE] Successfully switched to new organization:",e),console.log("[INVITE] Redirecting to dashboard after successful invitation acceptance"),window.location.href="/?invitation=accepted&org="+encodeURIComponent(t)}else{let e=await i.json().catch(()=>({}));(console.warn("[INVITE] Failed to switch organization:",e),"This is already your primary organization"===e.error)?(console.log("[INVITE] Organization is already primary, redirecting to dashboard"),
1console.log("[INVITE] Redirecting to dashboard after invitation acceptance"),window.location.href="/?invitation=accepted&org="+encodeURIComponent(t)):window.location.href="/organizations?invitation=accepted"}}catch(e){console.error("[INVITE] Error switching organization:",e),window.location.href="/?invitation=accepted"}else N?(console.log("[INVITE] Authenticated user accepted invitation, redirecting to dashboard"),window.location.href="/?invitation=accepted"):(console.log("[INVITE] User accepted invitation, redirecting to login"),window.location.href="/login?invitation=accepted")}catch(e){if(console.error("Error accepting invitation:",e),"UsernameExistsException"===e.name||(null===(s=e.message)||void 0===s?void 0:s.includes("User already exists"))||(null===(n=e.message)||void 0===n?void 0:n.includes("UsernameExistsException"))){console.log("[INVITE] User already exists in Cognito, redirecting to login..."),localStorage.setItem("pendingInvitationToken",f),h.push("/login?redirect=/invite/".concat(f,"&invitation=accepted"));return}V(e.message)}finally{C(!1)}},X=async e=>{var t,i,a,s,n,o,r;e.preventDefault(),C(!0),V("");try{console.log("Verifying email with code..."),await p(y.invitedEmail,Y),console.log("User verified, signing in automatically..."),await b(y.invitedEmail,U.password),console.log("Signed in, accepting invitation...");let e={newUser:{email:y.invitedEmail,firstName:U.firstName,lastName:U.lastName,name:"".concat(U.firstName," ").concat(U.lastName)}},o=await fetch("/api/v1/invitations/accept/".concat(f),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(e)}),r=await o.json();if(!o.ok)throw Error(r.message||"Failed to accept invitation");localStorage.removeItem("pendingInvitationToken"),O(!0),F("success");let l=null===(i=r.data)||void 0===i?void 0:null===(t=i.organization)||void 0===t?void 0:t.id,d=(null===(s=r.data)||void 0===s?void 0:null===(a=s.organization)||void 0===a?void 0:a.name)||(null==y?void 0:null===(n=y.organization)||void 0===n?void 0:n.name)||"the organization";if(l)try{(await g.default.request("/organization-switch-requests/set-primary",{method:"POST",body:JSON.stringify({organizationId:l})})).ok||console.warn("[INVITE] Could not switch org, proceeding to dashboard")}catch(e){console.warn("[INVITE] Org switch error:",e.message)}setTimeout(()=>{window.location.href="/?invitation=accepted&org="+encodeURIComponent(d)},1500)}catch(e){console.error("Error verifying and accepting invitation:",e),(null===(o=e.message)||void 0===o?void 0:o.includes("Incorrect username or password"))||(null===(r=e.message)||void 0===r?void 0:r.includes("NotAuthorizedException"))?(V("Email verified successfully but auto sign-in failed. Please sign in manually."),setTimeout(()=>{h.push("/login?verified=true")},3e3)):V(e.message||"Verification failed. Please check your code and try again.")}finally{C(!1)}};return E?(0,a.jsx)("div",{className:"min-h-screen bg-gray-50 flex items-center justify-center",children:(0,a.jsxs)("div",{className:"text-center",children:[(0,a.jsx)("div",{className:"animate-spin rounded-full h-12 w-12 border-b-2 border-blue-600 mx-auto"}),(0,a.jsx)("p",{className:"mt-4 text-gray-600",children:"Validating invitation..."})]})}):z?(0,a.jsx)("div",{className:"min-h-screen bg-gray-50 flex items-center justify-center",children:(0,a.jsxs)("div",{className:"max-w-md w-full bg-white rounded-lg shadow-lg p-8 text-center",children:[(0,a.jsx)(o.Z,{className:"h-16 w-16 text-red-500 mx-auto mb-4"}),(0,a.jsx)("h1",{className:"text-2xl font-bold text-gray-900 mb-2",children:"Invalid Invitation"}),(0,a.jsx)("p",{className:"text-gray-600 mb-6",children:z}),(0,a.jsx)("button",{onClick:()=>h.push("/"),className:"px-4 py-2 bg-blue-600 text-white rounded-md hover:bg-blue-700",children:"Go to Home"})]})}):"success"===P?(0,a.jsx)("div",{className:"min-h-screen bg-gray-50 flex items-center justify-center",children:(0,a.jsxs)("div",{className:"max-w-md w-full bg-white rounded-lg shadow-lg p-8 text-center",children:[(0,a.jsx)(r.Z,{className:"h-16 w-16 text-green-500 mx-auto mb-4"}),(0,a.jsx)("h1",{className:"text-2xl font-bold text-gray-900 mb-2",children:"Welcome to the Team!"}),(0,a.jsxs)("p",{className:"text-gray-600 mb-6",children:["You've successfully joined ",null==y?void 0:null===(x=y.organization)||void 0===x?void 0:x.name,". Redirecting you..."]}),(0,a.jsx)("div",{className:"animate-spin rounded-full h-8 w-8 border-b-2 border-blue-600 mx-auto"})]})}):"verification"===P?(0,a.jsx)("div",{className:"min-h-screen bg-gray-50 flex items-center justify-center",children:(0,a.jsxs)("div",{className:"max-w-md w-full bg-white rounded-lg shadow-lg p-8",children:[(0,a.jsxs)("div",{className:"text-center mb-8",children:[(0,a.jsx)(l.Z,{className:"h-16 w-16 text-blue-600 mx-auto mb-4"}),(0,a.jsx)("h1",{className:"text-2xl font-bold text-gray-900 mb-2",children:"Verify Your Email"}),(0,a.jsxs)("p",{className:"text-gray-600",children:["We've sent a verification code to",(0,a.jsx)("br",{}),(0,a.jsx)("strong",{children:null==y?void 0:y.invitedEmail})]})]}),(0,a.jsxs)("form",{onSubmit:X,className:"space-y-6",children:[(0,a.jsxs)("div",{children:[(0,a.jsx)("label",{htmlFor:"verificationCode",className:"block text-sm font-medium text-gray-700",children:"Verification Code"}),(0,a.jsx)("input",{type:"text",id:"verificationCode",required:!0,value:Y,onChange:e=>J(e.target.value),className:"mt-1 block w-full border-gray-300 rounded-md shadow-sm focus:ring-blue-500 focus:border-blue-500 sm:text-sm",placeholder:"Enter 6-digit code",maxLength:6})]}),z&&(0,a.jsx)("div",{className:"bg-red-50 border border-red-200 rounded-md p-4",children:(0,a.jsxs)("div",{className:"flex",children:[(0,a.jsx)(o.Z,{className:"h-5 w-5 text-red-400 mt-0.5"}),(0,a.jsx)("div",{className:"ml-3",children:(0,a.jsx)("p",{className:"text-sm text-red-700",children:z})})]})}),(0,a.jsx)("button",{type:"submit",disabled:k||6!==Y.length,className:"w-full flex justify-center py-3 px-4 border border-transparent roun
1ded-md shadow-sm text-sm font-medium text-white bg-blue-600 hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500 disabled:opacity-50 disabled:cursor-not-allowed",children:k?(0,a.jsxs)(a.Fragment,{children:[(0,a.jsx)("div",{className:"animate-spin rounded-full h-4 w-4 border-b-2 border-white mr-2"}),"Verifying..."]}):"Verify & Join Team"})]}),(0,a.jsx)("div",{className:"mt-6 text-center",children:(0,a.jsx)("p",{className:"text-xs text-gray-500",children:"Didn't receive the code? Check your spam folder."})})]})}):(0,a.jsx)("div",{className:"min-h-screen bg-gray-50 flex items-center justify-center",children:(0,a.jsxs)("div",{className:"max-w-md w-full bg-white rounded-lg shadow-lg p-8",children:[(0,a.jsxs)("div",{className:"text-center mb-8",children:[(0,a.jsx)(d.Z,{className:"h-16 w-16 text-blue-600 mx-auto mb-4"}),(0,a.jsx)("h1",{className:"text-2xl font-bold text-gray-900 mb-2",children:"You're Invited!"}),(0,a.jsxs)("p",{className:"text-gray-600",children:[(0,a.jsx)("strong",{children:null==y?void 0:null===(e=y.organization)||void 0===e?void 0:e.name})," has invited you to join their team as a"," ",(0,a.jsx)("span",{className:"inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-blue-100 text-blue-800",children:null==y?void 0:y.role})]})]}),Z&&(0,a.jsx)("div",{className:"mb-6 p-4 bg-blue-50 border border-blue-200 rounded-md",children:(0,a.jsxs)("div",{className:"flex",children:[(0,a.jsx)(l.Z,{className:"h-5 w-5 text-blue-400 mt-0.5"}),(0,a.jsxs)("div",{className:"ml-3 flex-1",children:[(0,a.jsx)("h3",{className:"text-sm font-medium text-blue-800",children:"New Account Required"}),(0,a.jsx)("p",{className:"mt-1 text-sm text-blue-700",children:"You'll need to create a BIDOR account to accept this invitation."}),(0,a.jsx)("button",{onClick:()=>{localStorage.setItem("pendingInvitationToken",f),h.push("/signup?invitation=".concat(f))},className:"mt-3 inline-flex items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md text-blue-900 bg-blue-100 hover:bg-blue-200 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500",children:"Create Account"})]})]})}),!Z&&q&&q.hasCognitoAccount&&!q.hasOrganizations&&(0,a.jsx)("div",{className:"mb-6 p-4 bg-yellow-50 border border-yellow-200 rounded-md",children:(0,a.jsxs)("div",{className:"flex",children:[(0,a.jsx)(o.Z,{className:"h-5 w-5 text-yellow-400 mt-0.5"}),(0,a.jsxs)("div",{className:"ml-3",children:[(0,a.jsx)("h3",{className:"text-sm font-medium text-yellow-800",children:"Login Required"}),(0,a.jsx)("p",{className:"mt-1 text-sm text-yellow-700",children:"You already have a BIDOR account. Please login to accept this invitation."}),(0,a.jsx)("button",{onClick:()=>{localStorage.setItem("pendingInvitationToken",f),h.push("/login?redirect=/invite/".concat(f))},className:"mt-3 inline-flex items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md text-yellow-900 bg-yellow-100 hover:bg-yellow-200 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-yellow-500",children:"Go to Login"})]})]})}),N&&!Z&&(0,a.jsx)("div",{className:"mb-6 p-4 bg-green-50 border border-green-200 rounded-md",children:(0,a.jsxs)("div",{className:"flex",children:[(0,a.jsx)(r.Z,{className:"h-5 w-5 text-green-400 mt-0.5"}),(0,a.jsxs)("div",{className:"ml-3 flex-1",children:[(0,a.jsx)("h3",{className:"text-sm font-medium text-green-800",children:"You're Already Logged In"}),(0,a.jsxs)("p",{className:"mt-1 text-sm text-green-700",children:["Click the button below to accept this invitation and join ",(0,a.jsx)("strong",{children:null==y?void 0:null===(t=y.organization)||void 0===t?void 0:t.name}),"."]}),(0,a.jsx)("button",{onClick:W,disabled:k,className:"mt-3 inline-flex items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md text-white bg-green-600 hover:bg-green-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-green-500 disabled:opacity-50 disabled:cursor-not-allowed",children:k?(0,a.jsxs)(a.Fragment,{children:[(0,a.jsx)("div",{className:"animate-spin rounded-full h-4 w-4 border-b-2 border-white mr-2"}),"Accepting..."]}):(0,a.jsxs)(a.Fragment,{children:[(0,a.jsx)(r.Z,{className:"h-4 w-4 mr-2"}),"Accept Invitation"]})})]})]})}),!Z&&q&&q.hasOrganizations&&!N&&(0,a.jsx)("div",{className:"mb-6 p-4 bg-green-50 border border-green-200 rounded-md",children:(0,a.jsxs)("div",{className:"flex",children:[(0,a.jsx)(r.Z,{className:"h-5 w-5 text-green-400 mt-0.5"}),(0,a.jsxs)("div",{className:"ml-3",children:[(0,a.jsx)("h3",{className:"text-sm font-medium text-green-800",children:"Existing Account Found"}),(0,a.jsx)("p",{className:"mt-1 text-sm text-green-700",children:"You already have a BI
1DOR account. You'll be added to this organization."}),q.organizations.length>0&&(0,a.jsxs)("div",{className:"mt-2",children:[(0,a.jsx)("p",{className:"text-xs text-green-600",children:"Current organizations:"}),(0,a.jsx)("ul",{className:"text-xs text-green-600 mt-1",children:q.organizations.map((e,t)=>(0,a.jsxs)("li",{children:["⢠",e.name," (",e.role,")"]},t))})]})]})]})}),!N&&(0,a.jsxs)("form",{onSubmit:W,className:"space-y-6",children:[Z&&(0,a.jsxs)(a.Fragment,{children:[(0,a.jsxs)("div",{className:"grid grid-cols-2 gap-4",children:[(0,a.jsxs)("div",{children:[(0,a.jsx)("label",{htmlFor:"firstName",className:"block text-sm font-medium text-gray-700",children:"First Name"}),(0,a.jsxs)("div",{className:"mt-1 relative",children:[(0,a.jsx)("input",{type:"text",id:"firstName",required:!0,value:U.firstName,onChange:e=>A({...U,firstName:e.target.value}),className:"block w-full border-gray-300 rounded-md shadow-sm focus:ring-blue-500 focus:border-blue-500 sm:text-sm pr-10",placeholder:"First name"}),(0,a.jsx)(c.Z,{className:"h-5 w-5 text-gray-400 absolute right-3 top-1/2 transform -translate-y-1/2"})]})]}),(0,a.jsxs)("div",{children:[(0,a.jsx)("label",{htmlFor:"lastName",className:"block text-sm font-medium text-gray-700",children:"Last Name"}),(0,a.jsxs)("div",{className:"mt-1 relative",children:[(0,a.jsx)("input",{type:"text",id:"lastName",required:!0,value:U.lastName,onChange:e=>A({...U,lastName:e.target.value}),className:"block w-full border-gray-300 rounded-md shadow-sm focus:ring-blue-500 focus:border-blue-500 sm:text-sm pr-10",placeholder:"Last name"}),(0,a.jsx)(c.Z,{className:"h-5 w-5 text-gray-400 absolute right-3 top-1/2 transform -translate-y-1/2"})]})]})]}),(0,a.jsxs)("div",{children:[(0,a.jsx)("label",{htmlFor:"password",className:"block text-sm font-medium text-gray-700",children:"Password"}),(0,a.jsxs)("div",{className:"mt-1 relative",children:[(0,a.jsx)("input",{type:"password",id:"password",required:!0,value:U.password,onChange:e=>A({...U,password:e.target.value}),className:"block w-full border-gray-300 rounded-md shadow-sm focus:ring-blue-500 focus:border-blue-500 sm:text-sm pr-10",placeholder:"Enter your password"}),(0,a.jsx)(m.Z,{className:"h-5 w-5 text-gray-400 absolute right-3 top-1/2 transform -translate-y-1/2"})]}),(0,a.jsx)("p",{className:"mt-1 text-xs text-gray-500",children:"Minimum 8 characters"})]}),(0,a.jsxs)("div",{children:[(0,a.jsx)("label",{htmlFor:"confirmPassword",className:"block text-sm font-medium text-gray-700",children:"Confirm Password"}),(0,a.jsxs)("div",{className:"mt-1 relative",children:[(0,a.jsx)("input",{type:"password",id:"confirmPassword",required:!0,value:U.confirmPassword,onChange:e=>A({...U,confirmPassword:e.target.value}),className:"block w-full border-gray-300 rounded-md shadow-sm focus:ring-blue-500 focus:border-blue-500 sm:text-sm pr-10",placeholder:"Confirm your password"}),(0,a.jsx)(m.Z,{className:"h-5 w-5 text-gray-400 absolute right-3 top-1/2 transform -translate-y-1/2"})]})]})]}),z&&(0,a.jsx)("div",{className:"bg-red-50 border border-red-200 rounded-md p-4",children:(0,a.jsxs)("div",{className:"flex",children:[(0,a.jsx)(o.Z,{className:"h-5 w-5 text-red-400 mt-0.5"}),(0,a.jsxs)("div",{className:"ml-3",children:[(0,a.jsx)("h3",{className:"text-sm font-medium text-red-800",children:"Error"}),(0,a.jsx)("p",{className:"mt-1 text-sm text-red-700",children:z})]})]})}),(0,a.jsxs)("div",{className:"space-y-4",children:[(Z||q&&q.hasOrganizations)&&(0,a.jsx)("button",{type:"submit",disabled:k,className:"w-full flex justify-center py-3 px-4 border border-transparent rounded-md shadow-sm text-sm font-medium text-white bg-blue-600 hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500 disabled:opacity-50 disabled:cursor-not-allowed",children:k?(0,a.jsxs)(a.Fragment,{children:[(0,a.jsx)("div",{className:"animate-spin rounded-full h-4 w-4 border-b-2 border-white mr-2"}),"Accepting Invitation..."]}):"Accept Invitation & Join Team"}),(0,a.jsx)("button",{type:"button",onClick:()=>h.push("/"),className:"w-full flex justify-center py-3 px-4 border border-gray-300 rounded-md shadow-sm text-sm font-medium text-gray-700 bg-white hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500",children:"Cancel"})]})]}),(0,a.jsx)("div",{className:"mt-8 text-center",children:(0,a.jsxs)("p",{className:"text-xs text-gray-500",children:["By accepting this invitation, you agree to join ",null==y?void 0:null===(i=y.organization)||void 0===i?void 0:i.name," on BIDOR."]})})]})})}}},function(e){e.O(0,[9351,1309,2888,179],function(){return e(e.s=7674)}),_N_E=e.O()}]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.