diff --git a/src/components/PlansSection.tsx b/src/components/PlansSection.tsx index 398ba33..72c66f1 100644 --- a/src/components/PlansSection.tsx +++ b/src/components/PlansSection.tsx @@ -1,5 +1,5 @@ import { useState, useMemo, useEffect } from "react"; -import { Loader2, CheckCircle2, CreditCard, FileText, ChevronsUpDown, Check } from "lucide-react"; +import { Loader2, CheckCircle2, CreditCard, FileText, ChevronsUpDown, Check, Lock, Eye, EyeOff, Sparkles } from "lucide-react"; import { supabase } from "@/integrations/supabase/client"; import { useAuth } from "@/hooks/useAuth"; import { toast } from "sonner"; @@ -86,6 +86,7 @@ const formSchema = z.object({ .refine(isStockholmPostalCode, "Vi levererar endast inom Storstockholm (100 00 – 199 99)"), stad: z.string().trim().min(1, "Fyll i stad").max(100), kommentarer: z.string().trim().max(1000).optional(), + password: z.string().max(100).optional(), }); type Method = "card" | "invoice"; @@ -100,6 +101,7 @@ const PlansSection = () => { const [errors, setErrors] = useState>({}); const [clientSecret, setClientSecret] = useState(null); const [cityOpen, setCityOpen] = useState(false); + const [showPassword, setShowPassword] = useState(false); const [form, setForm] = useState({ företag: "", epost: "", @@ -108,6 +110,7 @@ const PlansSection = () => { postnummer: "", stad: "", kommentarer: "", + password: "", }); const filteredCities = useMemo(() => { @@ -187,6 +190,15 @@ const PlansSection = () => { return; } + // Password required for guests (auto-create account) + if (!user) { + const pw = form.password || ""; + if (pw.length < 8) { + setErrors((prev) => ({ ...prev, password: "Lösenordet måste vara minst 8 tecken" })); + return; + } + } + if (!hasPaymentsConfigured()) { toast.error("Betalningar är inte konfigurerade ännu."); return; @@ -194,6 +206,33 @@ const PlansSection = () => { setSubmitting(true); try { + // Auto-create account for guests, then sign in + if (!user && form.password) { + const { data: accData, error: accErr } = await supabase.functions.invoke("create-account", { + body: { + email: result.data.epost, + password: form.password, + company: result.data.företag, + phone: result.data.telefon ?? "", + }, + }); + if (accErr) throw accErr; + if ((accData as any)?.error) throw new Error((accData as any).error); + + const { error: signInErr } = await supabase.auth.signInWithPassword({ + email: result.data.epost, + password: form.password, + }); + if (signInErr) { + if ((accData as any)?.existed) { + throw new Error( + "Ett konto finns redan för denna e-post. Fel lösenord — logga in eller använd 'Glömt lösenord'.", + ); + } + throw signInErr; + } + } + const body = { method, employees, @@ -453,6 +492,58 @@ const PlansSection = () => { + {!user && ( +
+
+
+
+ +
+
+
+ Ditt konto skapas automatiskt +
+

+ Välj ett lösenord så loggar vi in dig direkt. Hantera ditt abonnemang, ändra leveranser och se fakturor i "Mitt konto" när som helst. +

+
+
+ + +
+ + + +
+ {errors.password ? ( +

{errors.password}

+ ) : ( +

+ Använd minst 8 tecken. Vi rekommenderar en blandning av bokstäver och siffror. +

+ )} +
+ )} + +