1import{s as I,r as l,j as e,p as J,i as B,d as Q}from"./index-CSK3bPC6.js";import{m as y,d as Z,i as ee,L as D,j as U,t as k,w as F,l as W,k as C,x as te}from"./App-nKNgOv09.js";import{B as O}from"./button-_K2oSow6.js";import{I as $}from"./input-DqLKVDvQ.js";import{L as G}from"./label-BLhIBBeT.js";import{H as re,S as se}from"./Header-CXmYEO--.js";import{A as H}from"./index-IoLnhEvD.js";import{S as ie}from"./sparkles-Vw9s-0LC.js";import{L as z}from"./lock-DKnAu3gF.js";import{U as ne}from"./useStoreConnection-BtornHOQ.js";import{B as ae}from"./building-2-CtvlfthV.js";import{R as V}from"./refresh-cw-DIVrm8eo.js";import{M as oe}from"./mail-DxJ9fEuN.js";import{S as le}from"./shopping-bag-COx-WOiH.js";import{A as ce}from"./arrow-left-BQ1qISjb.js";import"./search-29zovqeQ.js";import"./circle-check-CGp2GF1v.js";import"./log-out-CZK6izlW.js";var de="authorization_response",ue="/~oauth/initiate",me=["https://oauth.lovable.app","https://lovable.dev"],he="lovable://oauth-callback",pe="http://127.0.0.1/iframe-oauth/callback",fe=500,ge=12e4;function xe(t){let r;const s=new Promise(c=>{r=c}),o=c=>{if(!t.some(p=>c.origin===p))return;const a=c.data;!a||typeof a!="object"||a.type===de&&r(a.response)},h=()=>{window.removeEventListener("message",o)};return window.addEventListener("message",o),{cleanup:h,messagePromise:s}}function we(t){const r=window.screenX!==0||window.screenY!==0||!t,s=r?window.outerWidth*.5:window.screen.width*.5,o=r?window.outerHeight*.5:window.screen.height*.5,h=r?window.screenX+(window.outerWidth-s)/2:(window.screen.width-s)/2,c=r?window.screenY+(window.outerHeight-o)/2:(window.screen.height-o)/2;return{width:s,height:o,left:h,top:c}}function K(t,r){return t.state!==r?{error:new Error("State is invalid")}:t.error?t.error==="legacy_flow"?{error:new Error("This flow is not supported in Preview mode. Please open the app in a new tab to sign in.")}:{error:new Error(t.error_description??"Sign in failed")}:!t.access_token||!t.refresh_token?{error:new Error("No tokens received")}:{tokens:{access_token:t.access_token,refresh_token:t.refresh_token},error:null}}function ve(){const t=navigator.userAgent;return!!(/iPhone|iPad|iPod|Android/i.test(t)||/Macintosh/i.test(t)&&navigator.maxTouchPoints>1)}function ye(){return typeof crypto<"u"&&crypto.getRandomValues?[...crypto.getRandomValues(new Uint8Array(16))].map(t=>t.toString(16).padStart(2,"0")).join(""):Math.random().toString(36).substring(2)+Date.now().toString(36)}function je(t={}){const r=t.oauthBrokerUrl??ue,s=t.supportedOAuthOrigins??me;async function o(h,c={}){let d=!1;try{d=window.self!==window.top}catch{d=!0}const a=navigator.userAgent,p=/LovableApp\//.test(a),x=!p&&/lovable/i.test(a),f=ye();let E=c.redirect_uri??window.location.origin;p&&d?E=he:x&&d&&(E=pe);const b=new URLSearchParams({...c.extraParams,provider:h,redirect_uri:E,state:f});if(!d)return window.location.href=`${r}?${b.toString()}`,{error:null,redirected:!0};!p&&!x&&b.set("response_mode","web_message");const T=`${r}?${b.toString()}`,g=x?[...s,window.location.origin]:s,{messagePromise:_,cleanup:S}=xe(g);let m;if(ve())m=window.open(T,"_blank");else{const{width:u,height:j,left:w,top:P}=we(d);m=window.open(T,"oauth",`width=${u},height=${j},left=${w},top=${P}`)}if(!m&&(p||x)){let u;const j=new Promise((w,P)=>{u=setTimeout(()=>{P(new Error("OAuth timed out waiting for response"))},ge)});try{const w=await Promise.race([_,j]);return K(w,f)}catch(w){return{error:w instanceof Error?w:new Error(String(w))}}finally{u&&clearTimeout(u),S()}}if(!m)return S(),{error:new Error("Popup was blocked")};let N;const R=new Promise((u,j)=>{N=setInterval(()=>{m.closed&&(clearInterval(N),j(new Error("Sign in was cancelled")))},fe)});try{const u=await Promise.race([_,R]);return K(u,f)}catch(u){return{error:u instanceof Error?u:new Error(String(u))}}finally{clearInterval(N),S(),m==null||m.close()}}return{signInWithOAuth:o}}var be={name:"@lovable.dev/cloud-auth-js",version:"1.1.1",description:"Lovable Cloud Auth JS",main:"./dist/index.cjs",module:"./dist/index.js",types:"./dist/index.d.ts",type:"module",files:["dist"],scripts:{build:"tsup src/index.ts --format cjs,esm --dts --outDir dist",dev:"tsup src/index.ts --format cjs,esm --dts --watch",test:"vitest run",typecheck:"tsgo --noEmit",prepublishOnly:"npm run clean && npm run build",clean:"rimraf dist"},keywords:["lovable","oauth","authentication"],license:"MIT",devDependencies:{"@types/node":"^22",jsdom:"^26",rimraf:"^5.0.0",tsup:"^7.2.0",typescript:"^5",vitest:"^4.1.0"}};typeof window<"u"&&(window.__lovable_cloud_auth_js_version=be.ver
1sion);function _e(t={}){return je(t)}const Ne=_e(),Ee={auth:{signInWithOAuth:async(t,r)=>{const s=await Ne.signInWithOAuth(t,{redirect_uri:r==null?void 0:r.redirect_uri,extraParams:{...r==null?void 0:r.extraParams}});if(s.redirected||s.error)return s;try{await I.auth.setSession(s.tokens)}catch(o){return{error:o instanceof Error?o:new Error(String(o))}}return s}}},q=["Loading intelligence modulesâ¦","Preparing your dashboardâ¦","Syncing AI insightsâ¦","Initializing growth engineâ¦"],Se=({phase:t,onComplete:r})=>{const[s,o]=l.useState(0),[h,c]=l.useState(0);return l.useEffect(()=>{if(t!=="loading")return;const d=setInterval(()=>{o(a=>(a+1)%q.length)},1800);return()=>clearInterval(d)},[t]),l.useEffect(()=>{if(t!=="loading")return;c(0);const d=Date.now(),a=2400,p=()=>{const x=Date.now()-d,f=Math.min(x/a,.92);c(f*100),x<a&&requestAnimationFrame(p)};requestAnimationFrame(p)},[t]),l.useEffect(()=>{if(t!=="success")return;c(100);const d=setTimeout(r,1500);return()=>clearTimeout(d)},[t,r]),t?e.jsx(H,{children:e.jsx(y.div,{initial:{opacity:0}
1,animate:{opacity:1},exit:{opacity:0},transition:{duration:.35},className:"fixed inset-0 z-[200] flex items-center justify-center bg-background",children:e.jsxs("div",{className:"text-center px-6 max-w-md w-full",children:[t==="loading"&&e.jsxs(y.div,{initial:{opacity:0,y:12},animate:{opacity:1,y:0},transition:{duration:.4},children:[e.jsxs("div",{className:"relative mx-auto mb-8",children:[e.jsx("div",{className:"h-20 w-20 mx-auto rounded-full bg-primary/10 flex items-center justify-center shadow-[0_0_50px_-6px_hsl(var(--primary)/0.45)]",children:e.jsx(ie,{className:"h-8 w-8 text-primary"})}),e.jsx(y.div,{className:"absolute inset-0 h-20 w-20 mx-auto rounded-full border-2 border-primary/25",animate:{scale:[1,1.5,1],opacity:[.4,0,.4]},transition:{duration:2.2,repeat:1/0,ease:"easeInOut"}})]}),e.jsx("h1",{className:"text-xl font-bold text-foreground mb-2",children:"Initializing your AI Growth Engine"}),e.jsx(H,{mode:"wait",children:e.jsx(y.p,{initial:{opacity:0,y:6},animate:{opacity:1,y:0},exit:{opacity:0,y:-6},transition:{duration:.25},className:"text-sm text-muted-foreground mb-8",children:q[s]},s)}),e.jsx("div",{className:"w-full max-w-xs mx-auto h-1.5 rounded-full bg-muted overflow-hidden",children:e.jsx(y.div,{className:"h-full rounded-full bg-primary shadow-[0_0_12px_hsl(var(--primary)/0.5)]",style:{width:`${h}%`},transition:{ease:"easeOut"}})})]}),t==="success"&&e.jsxs(y.div,{initial:{opacity:0,scale:.9},animate:{opacity:1,scale:1},transition:{type:"spring",stiffness:300,damping:24},children:[e.jsx("div",{className:"h-20 w-20 mx-auto mb-6 rounded-full bg-emerald-500/10 flex items-center justify-center shadow-[0_0_40px_-8px_hsl(142_71%_45%/0.35)]",children:e.jsx(y.svg,{className:"h-10 w-10 text-emerald-500",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:2.5,strokeLinecap:"round",strokeLinejoin:"round",children:e.jsx(y.path,{d:"M5 13l4 4L19 7",initial:{pathLength:0},animate:{pathLength:1},transition:{duration:.45,ease:"easeOut"}})})}),e.jsx("h1",{className:"text-xl font-bold text-foreground mb-2",children:"You're in. Let's grow your store ð"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"Redirecting to your dashboardâ¦"})]})]})},"login-transition")}):null},ze=()=>{const t=Z(),[r]=ee(),s=l.useMemo(()=>{const i=r.get("next")||"";return i.startsWith("/")&&!i.startsWith("//")?i:""},[r]),o=l.useMemo(()=>s||r.get("redirect")||"",[r,s]),[h,c]=l.useState("individual"),[d,a]=l.useState(!1),[p,x]=l.useState(!1),[f,E]=l.useState(""),[b,T]=l.useState(""),[g,_]=l.useState(!1);l.useState("user");const[S,m]=l.useState(null),[N,R]=l.useState("/dashboard"),u=async i=>{if(o)return o;if(i)return"/admin";if(h==="agency")return"/agency";try{const{data:n,error:v}=await I.from("store_audits").select("id").limit(1);return v||n&&n.length>0?"/tori":"/store-audit"}catch{return"/tori"}},j=l.useCallback(()=>{t(N,{replace:!0})},[t,N]),w=async i=>{i.preventDefault(),a(!0);try{U(`login:${f.trim().toLowerCase()}`,5,6e4)}catch{k({title:"Too many attempts",description:"Please wait a moment and try again.",variant:"destructive"}),a(!1),m(null);return}try{J();const n=await F(async()=>{const{data:L,error:M}=await I.auth.signInWithPassword({email:f.trim(),password:b});if(M)throw M;return L},{maxAttempts:1,timeoutMs:1e4});m("loading");const{data:v}=await I.rpc("has_role",{_user_id:n.user.id,_role:"admin"});W("login_success",f,"primary");const A=await u(!!v);R(A),localStorage.removeItem("sa_first_visit_done"),m("success")}catch(n){W("login_failure",C(n)),B(n)&&Q();const v=n instanceof Error?n.message:String(n);let A="Login failed",L=C(n);v.includes("Email not confirmed")?(A="Email not verified",L="Please check your inbox and verify your email before logging in."):v.includes("Invalid login credentials")&&(A="Invalid credentials",L="The email or password you entered is incorrect. If you signed up with Google, please use the 'Continue with Google' button instead."),k({title:A,description:L,variant:"destructive"}),m(null)}finally{a(!1)}},P=async()=>{x(!0);try{const{error:i}=await Ee.auth.signInWithOAuth("google",{redirect_uri:window.location.origin+(s||"")});if(i)throw i}catch(i){const n=C(i),v=B(i)||n.toLowerCase().includes("fetch");k({title:v?"Google sign-in temporarily unavailable":"Google sign-in failed",description:v?"The authentication service is currently unreachable. Please try email/password or try again later.":n,variant:"destructive"})}finally{x(!1)}},Y=async i=>{i.preventDefault(),a(!0);try{U(`reset:${f.trim().toLowerCase()}`,3,12e4),await F(async()=>{const{error:n}=await I.auth.resetPasswordForEmail(f.trim(),{redirectTo:window.location.origin+"/admin/reset-password"});if(n)throw n},{maxAttempts:2}),k({title:"Check your email",description:"We sent you a password reset link."}),_(!1)}catch(n){k({title:"Reset failed",description:C(n),variant:"destructive"})}finally{a(!1)}},X=te();return e.jsxs(e.Fragment,{children:[e.jsx(Se,{phase:S,onComplete:j}),e.jsxs("div",{className:"min-h-screen bg-background flex flex-col",children:[e.jsx(re,{}),e.jsx("div",{className:"flex-1 flex items-center justify-center p-4",children:e.jsx("div",{className:"w-full max-w-md",children:e.jsxs("div",{className:"rounded-xl border border-border bg-card p-6 sm:p-8 shadow-sm",children:[e.jsx("div",{className:"flex items-center justify-center gap-2 mb-6",children:e.jsx("div",{className:"flex h-10 w-10 items-center justify-center rounded-lg bg-primary/10",children:e.jsx(z,{className:"h-5 w-5 text-primary"})})}),e.jsx("h1",{className:"text-xl font-bold text-foreground text-center mb-1",children:g?"Reset Password":"Welcome Back"}),e.jsx("p",{className:"text-sm text-muted-foreground text-center mb-6",children:g?"Enter your email to receive a reset link":"Sign in to your account"}),!g&&e.jsxs("div",{className:"flex rounded-lg border border-border overflow-hidden mb-6",children:[e.jsxs("button",{type:"button",onClick:()=>c("individual"),className:`flex-1 flex items-center justify-center gap-2 py-2.5 text-sm font-medium transition-colors ${h==="individual"?"bg-primary text-primary-foreground":"bg-card text-muted-foreground hover:text-foreground"}`,children:[e.jsx(ne,{className:"h-4 w-4"}),"Individual"]}),e.jsxs("button",{type:"button",onClick:()=>c("agency"),className:`flex-1 flex items-center justify-center gap-2 py-2.5 text-sm font-medium transition-colors ${h==="agency"?"bg-primary text-primary-foreground":"bg-card text-muted-foreground hover:text-foreground"}`,children:[e.jsx(ae,{className:"h-4 w-4"}),"Agency"]})]}),!g&&e.jsx("div",{className:"mb-4",children:e.jsxs(O,{type:"button",variant:"outline",className:"w-full min-h-[44px] gap-2",disabled:p,onClick:P,children:[p?e.jsx(V,{className:"h-4 w-4 animate-spin"}):e.jsxs("svg",{viewBox:"0 0 24 24",className:"h-4 w-4","aria-hidden":!0,
1children:[e.jsx("path",{d:"M22.56 12.25c0-.78-.07-1.53-.2-2.25H12v4.26h5.92a5.06 5.06 0 0 1-2.2 3.32v2.77h3.57c2.08-1.92 3.28-4.74 3.28-8.1z",fill:"#4285F4"}),e.jsx("path",{d:"M12 23c2.97 0 5.46-.98 7.28-2.66l-3.57-2.77c-.98.66-2.23 1.06-3.71 1.06-2.86 0-5.29-1.93-6.16-4.53H2.18v2.84C3.99 20.53 7.7 23 12 23z",fill:"#34A853"}),e.jsx("path",{d:"M5.84 14.09c-.22-.66-.35-1.36-.35-2.09s.13-1.43.35-2.09V7.07H2.18C1.43 8.55 1 10.22 1 12s.43 3.45 1.18 4.93l2.85-2.22.81-.62z",fill:"#FBBC05"}),e.jsx("path",{d:"M12 5.38c1.62 0 3.06.56 4.21 1.64l3.15-3.15C17.45 2.09 14.97 1 12 1 7.7 1 3.99 3.47 2.18 7.07l3.66 2.84c.87-2.6 3.3-4.53 6.16-4.53z",fill:"#EA4335"})]}),"Continue with Google"]})}),!g&&e.jsxs("div",{className:"relative my-4",children:[e.jsx("div",{className:"absolute inset-0 flex items-center",children:e.jsx("span",{className:"w-full border-t border-border"})}),e.jsx("div",{className:"relative flex justify-center text-xs uppercase",children:e.jsx("span",{className:"bg-card px-2 text-muted-foreground",children:"or continue with email"})})]}),X,e.jsxs("form",{onSubmit:g?Y:w,className:"grid gap-4",children:[e.jsxs("div",{className:"grid gap-1.5",children:[e.jsx(G,{htmlFor:"email",children:"Email"}),e.jsxs("div",{className:"relative",children:[e.jsx(oe,{className:"absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-muted-foreground"}),e.jsx($,{id:"email",type:"email",required:!0,value:f,onChange:i=>E(i.target.value),placeholder:"[email protected]",className:"pl-10"})]})]}),!g&&e.jsxs("div",{className:"grid gap-1.5",children:[e.jsx(G,{htmlFor:"password",children:"Password"}),e.jsxs("div",{className:"relative",children:[e.jsx(z,{className:"absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-muted-foreground"}),e.jsx($,{id:"password",type:"password",required:!0,value:b,onChange:i=>T(i.target.value),placeholder:"â¢â¢â¢â¢â¢â¢â¢â¢",minLength:6,className:"pl-10"})]})]}),e.jsx(O,{type:"submit",disabled:d,className:"w-full min-h-[44px]",children:d?e.jsxs(e.Fragment,{children:[e.jsx(V,{className:"h-4 w-4 animate-spin mr-2"})," Please waitâ¦"]}):g?"Send Reset Link":"Sign In"})]}),!g&&e.jsxs("div",{className:"mt-4 grid grid-cols-2 gap-2",children:[e.jsx(O,{variant:"outline",size:"sm",asChild:!0,className:"text-xs",children:e.jsxs(D,{to:"/auth/shopify",children:[e.jsx(le,{className:"h-3.5 w-3.5 mr-1.5"})," Shopify"]})}),e.jsx(O,{variant:"outline",size:"sm",asChild:!0,className:"text-xs",children:e.jsxs(D,{to:"/auth/sso",children:[e.jsx(se,{className:"h-3.5 w-3.5 mr-1.5"})," Enterprise SSO"]})})]}),e.jsx("div",{className:"mt-4 flex flex-col items-center gap-2 text-sm",children:g?e.jsxs("button",{onClick:()=>_(!1),className:"text-primary hover:underline flex items-center gap-1",children:[e.jsx(ce,{className:"h-3 w-3"})," Back to login"]}):e.jsxs(e.Fragment,{children:[e.jsx("button",{onClick:()=>_(!0),className:"text-muted-foreground hover:text-foreground",children:"Forgot password?"}),e.jsxs("p",{className:"text-muted-foreground",children:["Don't have an account?"," ",e.jsx(D,{to:h==="agency"?"/signup?type=agency":"/signup",className:"text-primary hover:underline",children:"Sign up"})]})]})})]})})})]})]})};export{ze as default};
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.