|
| 1 | +import { Dialog } from "@headlessui/react"; |
| 2 | +import { XIcon } from "@heroicons/react/outline"; |
| 3 | +import { useState } from "react"; |
| 4 | +import { httpPost } from "../utils/http"; |
| 5 | + |
| 6 | +interface VisitorAuthModalProps { |
| 7 | + isOpen: boolean; |
| 8 | + onClose: () => void; |
| 9 | +} |
| 10 | + |
| 11 | +export default function VisitorAuthModal({ |
| 12 | + isOpen, |
| 13 | + onClose, |
| 14 | +}: VisitorAuthModalProps) { |
| 15 | + const [email, setEmail] = useState(""); |
| 16 | + const [isLoading, setIsLoading] = useState(false); |
| 17 | + const [isEmailSent, setIsEmailSent] = useState(false); |
| 18 | + const [error, setError] = useState(""); |
| 19 | + |
| 20 | + const handleSubmit = async (e: React.FormEvent) => { |
| 21 | + e.preventDefault(); |
| 22 | + setIsLoading(true); |
| 23 | + setError(""); |
| 24 | + |
| 25 | + try { |
| 26 | + await httpPost({ |
| 27 | + url: "/api/auth/request-magic-link", |
| 28 | + data: { email }, |
| 29 | + }); |
| 30 | + |
| 31 | + setIsEmailSent(true); |
| 32 | + if (typeof window !== "undefined") { |
| 33 | + sessionStorage.setItem("auth_redirect", window.location.href); |
| 34 | + } |
| 35 | + } catch (err) { |
| 36 | + setError( |
| 37 | + err instanceof Error |
| 38 | + ? err.message |
| 39 | + : "Something went wrong. Please try again." |
| 40 | + ); |
| 41 | + } finally { |
| 42 | + setIsLoading(false); |
| 43 | + } |
| 44 | + }; |
| 45 | + |
| 46 | + const handleClose = () => { |
| 47 | + setEmail(""); |
| 48 | + setIsEmailSent(false); |
| 49 | + setError(""); |
| 50 | + onClose(); |
| 51 | + }; |
| 52 | + |
| 53 | + return ( |
| 54 | + <Dialog open={isOpen} onClose={handleClose} className="relative z-50"> |
| 55 | + <div className="fixed inset-0 bg-black/30" aria-hidden="true" /> |
| 56 | + |
| 57 | + <div className="fixed inset-0 flex items-center justify-center p-4"> |
| 58 | + <Dialog.Panel className="mx-auto max-w-md rounded-lg bg-white p-6 shadow-lg dark:bg-gray-800"> |
| 59 | + <div className="flex items-center justify-between mb-4"> |
| 60 | + <Dialog.Title className="text-lg font-medium text-gray-900 dark:text-gray-100"> |
| 61 | + {isEmailSent ? "Check your email" : "Sign in to continue"} |
| 62 | + </Dialog.Title> |
| 63 | + <button |
| 64 | + onClick={handleClose} |
| 65 | + className="text-gray-400 hover:text-gray-600 dark:hover:text-gray-300" |
| 66 | + > |
| 67 | + <XIcon className="h-5 w-5" /> |
| 68 | + </button> |
| 69 | + </div> |
| 70 | + |
| 71 | + {isEmailSent ? ( |
| 72 | + <div className="text-center"> |
| 73 | + <div className="mx-auto mb-4 h-12 w-12 rounded-full bg-green-100 dark:bg-green-900 flex items-center justify-center"> |
| 74 | + <svg |
| 75 | + className="h-6 w-6 text-green-600 dark:text-green-400" |
| 76 | + fill="none" |
| 77 | + viewBox="0 0 24 24" |
| 78 | + stroke="currentColor" |
| 79 | + > |
| 80 | + <path |
| 81 | + strokeLinecap="round" |
| 82 | + strokeLinejoin="round" |
| 83 | + strokeWidth={2} |
| 84 | + d="M5 13l4 4L19 7" |
| 85 | + /> |
| 86 | + </svg> |
| 87 | + </div> |
| 88 | + <p className="text-gray-600 dark:text-gray-300 mb-4"> |
| 89 | + We have sent a magic link to <strong>{email}</strong> |
| 90 | + </p> |
| 91 | + <p className="text-sm text-gray-500 dark:text-gray-400"> |
| 92 | + Click the link in your email to complete your sign-in. The link |
| 93 | + will expire in 15 minutes. |
| 94 | + </p> |
| 95 | + </div> |
| 96 | + ) : ( |
| 97 | + <form onSubmit={handleSubmit}> |
| 98 | + <div className="mb-4"> |
| 99 | + <label |
| 100 | + htmlFor="email" |
| 101 | + className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2" |
| 102 | + > |
| 103 | + Email address |
| 104 | + </label> |
| 105 | + <input |
| 106 | + type="email" |
| 107 | + id="email" |
| 108 | + value={email} |
| 109 | + onChange={(e) => setEmail(e.target.value)} |
| 110 | + required |
| 111 | + className="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 dark:bg-gray-700 dark:text-gray-100" |
| 112 | + placeholder="your@email.com" |
| 113 | + /> |
| 114 | + </div> |
| 115 | + |
| 116 | + {error && ( |
| 117 | + <div className="mb-4 p-3 bg-red-50 dark:bg-red-900/20 border border-red-200 dark:border-red-800 rounded-md"> |
| 118 | + <p className="text-sm text-red-600 dark:text-red-400"> |
| 119 | + {error} |
| 120 | + </p> |
| 121 | + </div> |
| 122 | + )} |
| 123 | + |
| 124 | + <button |
| 125 | + type="submit" |
| 126 | + disabled={isLoading || !email} |
| 127 | + className="w-full flex justify-center py-2 px-4 border border-transparent rounded-md shadow-sm text-sm font-medium text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500 disabled:opacity-50 disabled:cursor-not-allowed dark:bg-indigo-700 dark:hover:bg-indigo-600" |
| 128 | + > |
| 129 | + {isLoading ? "Sending..." : "Send magic link"} |
| 130 | + </button> |
| 131 | + |
| 132 | + <p className="mt-4 text-xs text-gray-500 dark:text-gray-400 text-center"> |
| 133 | + We will send you a secure link to sign in without a password. |
| 134 | + </p> |
| 135 | + </form> |
| 136 | + )} |
| 137 | + </Dialog.Panel> |
| 138 | + </div> |
| 139 | + </Dialog> |
| 140 | + ); |
| 141 | +} |
0 commit comments