'use client' import { useState, useEffect, Suspense } from 'react' import { useRouter, useSearchParams } from 'next/navigation' import { createClient } from '@/lib/supabase/client' import { Button } from '@/components/ui/button' import { Input } from '@/components/ui/input' import { Label } from '@/components/ui/label' import { useToast } from '@/components/ui/use-toast' import { Loader2, KeyRound } from 'lucide-react' import { userHasPassword } from '@/lib/auth/has-password' import { safeReturnTo } from '@/lib/auth/safe-return-to' export default function SetPasswordPage() { return ( ) } function SetPasswordContent() { const [password, setPassword] = useState('') const [confirmPassword, setConfirmPassword] = useState('') const [isLoading, setIsLoading] = useState(false) const { toast } = useToast() const router = useRouter() const searchParams = useSearchParams() const supabase = createClient() const returnTo = safeReturnTo(searchParams.get('returnTo'), '/settings/account') // Users who already have a password don't belong here: bounce them away. useEffect(() => { let cancelled = false ;(async () => { const { data: { user }, } = await supabase.auth.getUser() if (cancelled) return if (!user) { router.replace('/login') return } if (userHasPassword(user)) { router.replace(returnTo) } })() return () => { cancelled = true } // eslint-disable-next-line react-hooks/exhaustive-deps }, []) const handleSetPassword = async (e: React.FormEvent) => { e.preventDefault() setIsLoading(true) const strong = password.length >= 8 && /[a-z]/.test(password) && /[A-Z]/.test(password) && /[0-9]/.test(password) && /[^a-zA-Z0-9]/.test(password) if (!strong) { toast({ title: 'Lösenordet är för svagt', description: 'Lösenordet måste vara minst 8 tecken och innehålla versaler, gemener, siffror och specialtecken.', variant: 'destructive', }) setIsLoading(false) return } if (password !== confirmPassword) { toast({ title: 'Lösenorden matchar inte', description: 'Kontrollera att du skrev samma lösenord i båda fälten.', variant: 'destructive', }) setIsLoading(false) return } try { const res = await fetch('/api/account/password', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ password }), }) if (!res.ok) { const body = (await res.json().catch(() => ({}))) as { error?: string } toast({ title: 'Kunde inte spara lösenord', description: body.error || 'Försök igen senare.', variant: 'destructive', }) return } toast({ title: 'Lösenord sparat', description: 'Du kan nu aktivera tvåfaktorsautentisering.', }) router.push(returnTo) router.refresh() } catch { toast({ title: 'Något gick fel', description: 'Försök igen senare.', variant: 'destructive', }) } finally { setIsLoading(false) } } return (

Sätt ett lösenord

Du loggade in med BankID. För att aktivera tvåfaktorsautentisering eller logga in med e-post behöver du först sätta ett lösenord.

setPassword(e.target.value)} required minLength={8} disabled={isLoading} className="h-11" />
setConfirmPassword(e.target.value)} required minLength={8} disabled={isLoading} className="h-11" />
) }