"use client"; import * as React from "react"; import { authClient } from "@/lib/auth-client"; import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger } from "@/components/ui/dialog"; import { Input } from "@/components/ui/input"; import { Field, Hint, Label } from "@/components/ui/label"; import { Alert } from "@/components/ui/alert"; export function ChangeEmailDialog({ currentEmail }: { currentEmail: string }) { const [open, setOpen] = React.useState(false); const [email, setEmail] = React.useState(""); const [error, setError] = React.useState(null); const [sent, setSent] = React.useState(false); const [loading, setLoading] = React.useState(false); async function submit(e: React.FormEvent) { e.preventDefault(); setError(null); const newEmail = email.trim().toLowerCase(); if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(newEmail)) { setError("Enter a valid email address."); return; } if (newEmail === currentEmail.toLowerCase()) { setError("That is already your email address."); return; } setLoading(true); const { error } = await authClient.changeEmail({ newEmail, callbackURL: "/dashboard/settings?email-changed=1" }); setLoading(false); if (error) { setError(error.message ?? "Could not start the email change. Try again."); return; } setSent(true); } return ( { setOpen(o); if (!o) { setSent(false); setError(null); setEmail(""); } }} > Change email address For your security we first send an approval link to your current address ({currentEmail}). Once you approve it, the new address becomes your login and must be verified in turn. {sent ? ( <> Open the link we sent to {currentEmail} to approve switching to {email.trim()}. The link expires in 24 hours. Until then you keep signing in with your current address. ) : (
{error ? {error} : null} setEmail(e.target.value)} placeholder="you@company.com" required autoFocus /> API keys, projects and usage are unaffected.
)}
); }