|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { ArrowRight, CheckCircle2 } from "lucide-react"; |
| 4 | +import { usePathname, useRouter } from "next/navigation"; |
| 5 | +import { useEffect, useRef, useState, type ReactNode } from "react"; |
| 6 | +import { isSamePathname } from "../_lib/client-route"; |
| 7 | +import type { AuthMode } from "../_lib/den-flow"; |
| 8 | +import { useDenFlow } from "../_providers/den-flow-provider"; |
| 9 | + |
| 10 | +function getDesktopGrant(url: string | null) { |
| 11 | + if (!url) return null; |
| 12 | + try { |
| 13 | + const parsed = new URL(url); |
| 14 | + const grant = parsed.searchParams.get("grant")?.trim() ?? ""; |
| 15 | + return grant || null; |
| 16 | + } catch { |
| 17 | + return null; |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +function GitHubLogo() { |
| 22 | + return ( |
| 23 | + <svg viewBox="0 0 16 16" aria-hidden="true" className="h-4 w-4 shrink-0"> |
| 24 | + <path |
| 25 | + fill="currentColor" |
| 26 | + d="M8 0C3.58 0 0 3.58 0 8a8 8 0 0 0 5.47 7.59c.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.5-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82a7.5 7.5 0 0 1 4 0c1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8 8 0 0 0 16 8c0-4.42-3.58-8-8-8Z" |
| 27 | + /> |
| 28 | + </svg> |
| 29 | + ); |
| 30 | +} |
| 31 | + |
| 32 | +function GoogleLogo() { |
| 33 | + return ( |
| 34 | + <svg viewBox="0 0 18 18" aria-hidden="true" className="h-4 w-4 shrink-0"> |
| 35 | + <path fill="#4285F4" d="M17.64 9.2c0-.64-.06-1.25-.16-1.84H9v3.48h4.84a4.14 4.14 0 0 1-1.8 2.72v2.26h2.92c1.71-1.57 2.68-3.89 2.68-6.62Z" /> |
| 36 | + <path fill="#34A853" d="M9 18c2.43 0 4.47-.8 5.96-2.18l-2.92-2.26c-.81.54-1.84.86-3.04.86-2.34 0-4.31-1.58-5.01-3.7H.96v2.33A9 9 0 0 0 9 18Z" /> |
| 37 | + <path fill="#FBBC05" d="M3.99 10.72A5.41 5.41 0 0 1 3.71 9c0-.6.1-1.18.28-1.72V4.95H.96A9 9 0 0 0 0 9c0 1.45.35 2.82.96 4.05l3.03-2.33Z" /> |
| 38 | + <path fill="#EA4335" d="M9 3.58c1.32 0 2.5.45 3.43 1.33l2.57-2.57C13.46.9 11.43 0 9 0A9 9 0 0 0 .96 4.95l3.03 2.33c.7-2.12 2.67-3.7 5.01-3.7Z" /> |
| 39 | + </svg> |
| 40 | + ); |
| 41 | +} |
| 42 | + |
| 43 | +function SocialButton({ |
| 44 | + children, |
| 45 | + onClick, |
| 46 | + disabled, |
| 47 | +}: { |
| 48 | + children: ReactNode; |
| 49 | + onClick: () => void; |
| 50 | + disabled: boolean; |
| 51 | +}) { |
| 52 | + return ( |
| 53 | + <button |
| 54 | + type="button" |
| 55 | + className="flex w-full items-center justify-center gap-3 rounded-xl border border-gray-200 bg-white px-4 py-3 text-[14px] font-medium text-gray-700 transition-colors hover:bg-gray-50 disabled:cursor-not-allowed disabled:opacity-60" |
| 56 | + onClick={onClick} |
| 57 | + disabled={disabled} |
| 58 | + > |
| 59 | + {children} |
| 60 | + </button> |
| 61 | + ); |
| 62 | +} |
| 63 | + |
| 64 | +export function AuthPanel({ |
| 65 | + panelTitle, |
| 66 | + panelCopy, |
| 67 | + prefilledEmail, |
| 68 | + prefillKey, |
| 69 | + initialMode = "sign-up", |
| 70 | + lockEmail = false, |
| 71 | + hideSocialAuth = false, |
| 72 | + hideEmailField = false, |
| 73 | + eyebrow = "Account", |
| 74 | +}: { |
| 75 | + panelTitle?: string; |
| 76 | + panelCopy?: string; |
| 77 | + prefilledEmail?: string; |
| 78 | + prefillKey?: string; |
| 79 | + initialMode?: AuthMode; |
| 80 | + lockEmail?: boolean; |
| 81 | + hideSocialAuth?: boolean; |
| 82 | + hideEmailField?: boolean; |
| 83 | + eyebrow?: string; |
| 84 | +}) { |
| 85 | + const router = useRouter(); |
| 86 | + const pathname = usePathname(); |
| 87 | + const prefillRef = useRef<string | null>(null); |
| 88 | + const [copiedDesktopField, setCopiedDesktopField] = useState<"link" | "code" | null>(null); |
| 89 | + const { |
| 90 | + authMode, |
| 91 | + setAuthMode, |
| 92 | + email, |
| 93 | + setEmail, |
| 94 | + password, |
| 95 | + setPassword, |
| 96 | + verificationCode, |
| 97 | + setVerificationCode, |
| 98 | + verificationRequired, |
| 99 | + authBusy, |
| 100 | + authInfo, |
| 101 | + authError, |
| 102 | + desktopAuthRequested, |
| 103 | + desktopRedirectUrl, |
| 104 | + desktopRedirectBusy, |
| 105 | + showAuthFeedback, |
| 106 | + submitAuth, |
| 107 | + submitVerificationCode, |
| 108 | + resendVerificationCode, |
| 109 | + cancelVerification, |
| 110 | + beginSocialAuth, |
| 111 | + resolveUserLandingRoute, |
| 112 | + } = useDenFlow(); |
| 113 | + |
| 114 | + const desktopGrant = getDesktopGrant(desktopRedirectUrl); |
| 115 | + const resolvedPanelTitle = verificationRequired |
| 116 | + ? "Verify your email." |
| 117 | + : panelTitle ?? (authMode === "sign-up" ? "Create your Cloud account." : "Sign in to Cloud."); |
| 118 | + const resolvedPanelCopy = verificationRequired |
| 119 | + ? "Enter the six-digit code from your inbox to finish setup." |
| 120 | + : panelCopy ?? ( |
| 121 | + authMode === "sign-up" |
| 122 | + ? "Start with email, GitHub, or Google." |
| 123 | + : "Welcome back. Keep your team setup in sync across Cloud and desktop." |
| 124 | + ); |
| 125 | + const showLockedEmailSummary = Boolean(prefilledEmail && lockEmail && hideEmailField); |
| 126 | + |
| 127 | + useEffect(() => { |
| 128 | + const key = prefillKey ?? prefilledEmail?.trim() ?? null; |
| 129 | + if (!key || prefillRef.current === key) { |
| 130 | + return; |
| 131 | + } |
| 132 | + |
| 133 | + prefillRef.current = key; |
| 134 | + setAuthMode(initialMode); |
| 135 | + setEmail(prefilledEmail?.trim() ?? ""); |
| 136 | + setPassword(""); |
| 137 | + setVerificationCode(""); |
| 138 | + }, [initialMode, prefillKey, prefilledEmail, setAuthMode, setEmail, setPassword, setVerificationCode]); |
| 139 | + |
| 140 | + const copyDesktopValue = async (field: "link" | "code", value: string | null) => { |
| 141 | + if (!value) return; |
| 142 | + await navigator.clipboard.writeText(value); |
| 143 | + setCopiedDesktopField(field); |
| 144 | + window.setTimeout(() => { |
| 145 | + setCopiedDesktopField((current) => (current === field ? null : current)); |
| 146 | + }, 1800); |
| 147 | + }; |
| 148 | + |
| 149 | + return ( |
| 150 | + <div className="rounded-[28px] border border-gray-100 bg-white p-6 shadow-[0_10px_30px_-24px_rgba(15,23,42,0.22)] md:p-7"> |
| 151 | + <div className="grid gap-2"> |
| 152 | + <p className="text-[11px] font-semibold uppercase tracking-[0.18em] text-gray-400"> |
| 153 | + {eyebrow} |
| 154 | + </p> |
| 155 | + <h2 className="text-[28px] font-semibold tracking-[-0.04em] text-gray-900">{resolvedPanelTitle}</h2> |
| 156 | + <p className="text-[14px] leading-relaxed text-gray-500">{resolvedPanelCopy}</p> |
| 157 | + </div> |
| 158 | + |
| 159 | + {desktopAuthRequested ? ( |
| 160 | + <div className="mt-5 rounded-2xl border border-sky-100 bg-sky-50 p-4 text-[13px] text-sky-900"> |
| 161 | + Finish auth here and we'll send you back into the OpenWork desktop app. |
| 162 | + {desktopRedirectUrl ? ( |
| 163 | + <div className="mt-4 grid gap-2"> |
| 164 | + <div className="flex flex-wrap gap-2"> |
| 165 | + <button |
| 166 | + type="button" |
| 167 | + className="rounded-full border border-sky-200 bg-white px-4 py-2 text-xs font-medium text-sky-900 transition-colors hover:bg-sky-100" |
| 168 | + onClick={() => window.location.assign(desktopRedirectUrl)} |
| 169 | + > |
| 170 | + Open OpenWork |
| 171 | + </button> |
| 172 | + <button |
| 173 | + type="button" |
| 174 | + className="rounded-full border border-sky-200 bg-white px-4 py-2 text-xs font-medium text-sky-900 transition-colors hover:bg-sky-100" |
| 175 | + onClick={() => void copyDesktopValue("link", desktopRedirectUrl)} |
| 176 | + > |
| 177 | + {copiedDesktopField === "link" ? "Copied link" : "Copy sign-in link"} |
| 178 | + </button> |
| 179 | + {desktopGrant ? ( |
| 180 | + <button |
| 181 | + type="button" |
| 182 | + className="rounded-full border border-sky-200 bg-white px-4 py-2 text-xs font-medium text-sky-900 transition-colors hover:bg-sky-100" |
| 183 | + onClick={() => void copyDesktopValue("code", desktopGrant)} |
| 184 | + > |
| 185 | + {copiedDesktopField === "code" ? "Copied code" : "Copy one-time code"} |
| 186 | + </button> |
| 187 | + ) : null} |
| 188 | + </div> |
| 189 | + <p className="text-xs leading-5 text-sky-800/80"> |
| 190 | + If OpenWork does not open automatically, copy the sign-in link or one-time code and paste it into the OpenWork desktop app. |
| 191 | + </p> |
| 192 | + </div> |
| 193 | + ) : null} |
| 194 | + </div> |
| 195 | + ) : null} |
| 196 | + |
| 197 | + <form |
| 198 | + className="mt-5 grid gap-3" |
| 199 | + onSubmit={async (event) => { |
| 200 | + const next = verificationRequired |
| 201 | + ? await submitVerificationCode(event) |
| 202 | + : await submitAuth(event); |
| 203 | + if (next === "dashboard" || next === "join-org") { |
| 204 | + const target = await resolveUserLandingRoute(); |
| 205 | + if (target && !isSamePathname(pathname, target)) { |
| 206 | + router.replace(target); |
| 207 | + } |
| 208 | + } else if (next === "checkout" && !isSamePathname(pathname, "/checkout")) { |
| 209 | + router.replace("/checkout"); |
| 210 | + } |
| 211 | + }} |
| 212 | + > |
| 213 | + {!verificationRequired && !hideSocialAuth ? ( |
| 214 | + <> |
| 215 | + <SocialButton |
| 216 | + onClick={() => void beginSocialAuth("github")} |
| 217 | + disabled={authBusy || desktopRedirectBusy} |
| 218 | + > |
| 219 | + <GitHubLogo /> |
| 220 | + <span>Continue with GitHub</span> |
| 221 | + </SocialButton> |
| 222 | + |
| 223 | + <SocialButton |
| 224 | + onClick={() => void beginSocialAuth("google")} |
| 225 | + disabled={authBusy || desktopRedirectBusy} |
| 226 | + > |
| 227 | + <GoogleLogo /> |
| 228 | + <span>Continue with Google</span> |
| 229 | + </SocialButton> |
| 230 | + |
| 231 | + <div |
| 232 | + className="flex items-center gap-3 text-[11px] font-semibold uppercase tracking-[0.18em] text-gray-400" |
| 233 | + aria-hidden="true" |
| 234 | + > |
| 235 | + <span className="h-px flex-1 bg-gray-200" /> |
| 236 | + <span>or</span> |
| 237 | + <span className="h-px flex-1 bg-gray-200" /> |
| 238 | + </div> |
| 239 | + </> |
| 240 | + ) : null} |
| 241 | + |
| 242 | + {showLockedEmailSummary ? ( |
| 243 | + <div className="rounded-2xl border border-gray-100 bg-gray-50 px-4 py-3"> |
| 244 | + <p className="text-[11px] font-semibold uppercase tracking-[0.16em] text-gray-400"> |
| 245 | + Invited email |
| 246 | + </p> |
| 247 | + <p className="mt-1 text-[14px] font-medium text-gray-900">{prefilledEmail}</p> |
| 248 | + </div> |
| 249 | + ) : null} |
| 250 | + |
| 251 | + {!hideEmailField ? ( |
| 252 | + <label className="grid gap-2"> |
| 253 | + <span className="text-[11px] font-semibold uppercase tracking-[0.16em] text-gray-400"> |
| 254 | + Email |
| 255 | + </span> |
| 256 | + <input |
| 257 | + className="w-full rounded-xl border border-gray-200 bg-white px-4 py-3 text-[14px] text-gray-900 outline-none transition focus:border-gray-300 focus:ring-4 focus:ring-gray-900/5 disabled:cursor-not-allowed disabled:bg-gray-50 disabled:text-gray-500" |
| 258 | + type="email" |
| 259 | + value={email} |
| 260 | + onChange={(event) => setEmail(event.target.value)} |
| 261 | + autoComplete="email" |
| 262 | + readOnly={lockEmail} |
| 263 | + disabled={lockEmail} |
| 264 | + required |
| 265 | + /> |
| 266 | + </label> |
| 267 | + ) : null} |
| 268 | + |
| 269 | + {!verificationRequired ? ( |
| 270 | + <label className="grid gap-2"> |
| 271 | + <span className="text-[11px] font-semibold uppercase tracking-[0.16em] text-gray-400"> |
| 272 | + Password |
| 273 | + </span> |
| 274 | + <input |
| 275 | + className="w-full rounded-xl border border-gray-200 bg-white px-4 py-3 text-[14px] text-gray-900 outline-none transition focus:border-gray-300 focus:ring-4 focus:ring-gray-900/5" |
| 276 | + type="password" |
| 277 | + value={password} |
| 278 | + onChange={(event) => setPassword(event.target.value)} |
| 279 | + autoComplete={authMode === "sign-up" ? "new-password" : "current-password"} |
| 280 | + required |
| 281 | + /> |
| 282 | + </label> |
| 283 | + ) : ( |
| 284 | + <label className="grid gap-2"> |
| 285 | + <span className="text-[11px] font-semibold uppercase tracking-[0.16em] text-gray-400"> |
| 286 | + Verification code |
| 287 | + </span> |
| 288 | + <input |
| 289 | + className="w-full rounded-xl border border-gray-200 bg-white px-4 py-3 text-center text-[18px] font-semibold tracking-[0.35em] text-gray-900 outline-none transition focus:border-gray-300 focus:ring-4 focus:ring-gray-900/5" |
| 290 | + type="text" |
| 291 | + inputMode="numeric" |
| 292 | + pattern="[0-9]*" |
| 293 | + value={verificationCode} |
| 294 | + onChange={(event) => |
| 295 | + setVerificationCode(event.target.value.replace(/\D+/g, "").slice(0, 6)) |
| 296 | + } |
| 297 | + autoComplete="one-time-code" |
| 298 | + required |
| 299 | + /> |
| 300 | + </label> |
| 301 | + )} |
| 302 | + |
| 303 | + <button |
| 304 | + type="submit" |
| 305 | + className="flex w-full items-center justify-center gap-2 rounded-full bg-gray-900 px-5 py-3 text-[14px] font-medium text-white transition-colors hover:bg-gray-800 disabled:cursor-not-allowed disabled:opacity-60" |
| 306 | + disabled={authBusy || desktopRedirectBusy} |
| 307 | + > |
| 308 | + {authBusy || desktopRedirectBusy |
| 309 | + ? "Working..." |
| 310 | + : verificationRequired |
| 311 | + ? "Verify email" |
| 312 | + : authMode === "sign-in" |
| 313 | + ? "Sign in to Cloud" |
| 314 | + : "Create Cloud account"} |
| 315 | + {!authBusy && !desktopRedirectBusy ? <ArrowRight className="h-4 w-4" /> : null} |
| 316 | + </button> |
| 317 | + |
| 318 | + {verificationRequired ? ( |
| 319 | + <div className="flex flex-col gap-3 sm:flex-row"> |
| 320 | + <button |
| 321 | + type="button" |
| 322 | + className="w-full rounded-full border border-gray-200 bg-white px-4 py-3 text-[13px] font-medium text-gray-700 transition-colors hover:bg-gray-50 disabled:cursor-not-allowed disabled:opacity-60" |
| 323 | + onClick={() => void resendVerificationCode()} |
| 324 | + disabled={authBusy || desktopRedirectBusy} |
| 325 | + > |
| 326 | + Resend code |
| 327 | + </button> |
| 328 | + <button |
| 329 | + type="button" |
| 330 | + className="w-full rounded-full border border-gray-200 bg-white px-4 py-3 text-[13px] font-medium text-gray-700 transition-colors hover:bg-gray-50 disabled:cursor-not-allowed disabled:opacity-60" |
| 331 | + onClick={() => cancelVerification()} |
| 332 | + disabled={authBusy || desktopRedirectBusy} |
| 333 | + > |
| 334 | + Change email |
| 335 | + </button> |
| 336 | + </div> |
| 337 | + ) : null} |
| 338 | + </form> |
| 339 | + |
| 340 | + {!verificationRequired ? ( |
| 341 | + <div className="mt-4 flex items-center justify-between gap-3 border-t border-gray-200 pt-4 text-sm text-gray-500"> |
| 342 | + <p>{authMode === "sign-in" ? "Need an account?" : "Already have an account?"}</p> |
| 343 | + <button |
| 344 | + type="button" |
| 345 | + className="font-medium text-gray-900 transition hover:opacity-70" |
| 346 | + onClick={() => setAuthMode(authMode === "sign-in" ? "sign-up" : "sign-in")} |
| 347 | + > |
| 348 | + {authMode === "sign-in" ? "Create account" : "Switch to sign in"} |
| 349 | + </button> |
| 350 | + </div> |
| 351 | + ) : null} |
| 352 | + |
| 353 | + {showAuthFeedback ? ( |
| 354 | + <div |
| 355 | + className="mt-4 grid gap-1 rounded-2xl border border-gray-100 bg-gray-50 px-4 py-3 text-center text-[13px] text-gray-500" |
| 356 | + aria-live="polite" |
| 357 | + > |
| 358 | + <p>{authInfo}</p> |
| 359 | + {authError ? <p className="font-medium text-rose-600">{authError}</p> : null} |
| 360 | + {!authError && verificationRequired ? ( |
| 361 | + <div className="mt-1 inline-flex items-center justify-center gap-1 text-emerald-600"> |
| 362 | + <CheckCircle2 className="h-3.5 w-3.5" /> |
| 363 | + <span>Waiting for your verification code</span> |
| 364 | + </div> |
| 365 | + ) : null} |
| 366 | + </div> |
| 367 | + ) : null} |
| 368 | + </div> |
| 369 | + ); |
| 370 | +} |
0 commit comments