"use client"; import Script from "next/script"; import { useRouter } from "next/navigation"; import { useState } from "react"; import { Waitlist } from "@/components/Waitlist"; import { ApiError, api, errorText } from "@/lib/api"; /** The part of Sign in with Apple JS (appleid.auth.js) we use. */ interface AppleSignInResponse { authorization: { id_token: string; code: string; state?: string }; /** Only at the first sign-in of this Apple ID on the site. */ user?: { email?: string; name?: { firstName?: string; lastName?: string } }; } declare global { interface Window { AppleID?: { auth: { init(options: { clientId: string; scope: string; redirectURI: string; state?: string; usePopup: boolean }): void; signIn(): Promise; }; }; } } /** Apple's logo (Sign in with Apple guidelines: the logo left of the label). */ function AppleMark() { return ( ); } /** The e-mail address inside Apple's ID token (only for the waitlist form; the backend verifies the token). */ function tokenEmail(idToken: string): string { try { const part = idToken.split(".")[1].replace(/-/g, "+").replace(/_/g, "/"); const bytes = Uint8Array.from(atob(part), (c) => c.charCodeAt(0)); const email = JSON.parse(new TextDecoder().decode(bytes)).email; return typeof email === "string" ? email : ""; } catch { return ""; } } /** * "Continue with Apple": Apple's sign-in opens in a popup and returns an ID * token, which the backend verifies. Apple gives the name only at the first * sign-in, so it is sent along then (Hungarian order: family name first). */ export function AppleLogin({ clientId, target, inviteToken = null, }: { clientId: string; target: string; inviteToken?: string | null; }) { const router = useRouter(); const [ready, setReady] = useState(false); const [busy, setBusy] = useState(false); const [error, setError] = useState(null); const [uninvited, setUninvited] = useState(null); function loaded() { window.AppleID?.auth.init({ clientId, scope: "name email", redirectURI: `${window.location.origin}/belepes`, usePopup: true, }); setReady(true); } async function signIn() { const apple = window.AppleID; if (!apple) return; setError(null); setUninvited(null); let response: AppleSignInResponse; try { response = await apple.auth.signIn(); } catch (e) { // Closing the popup is not an error worth showing. const code = (e as { error?: string } | null)?.error; if (code !== "popup_closed_by_user" && code !== "user_cancelled_authorize") { setError("Az Apple-belépés nem sikerült. Próbáld újra."); } return; } setBusy(true); const name = [response.user?.name?.lastName, response.user?.name?.firstName].filter(Boolean).join(" ") || null; try { let referral: string | null = null; try { referral = localStorage.getItem("sf_ref"); } catch { // storage blocked } await api("/auth/apple", { body: { id_token: response.authorization.id_token, name, invite_token: inviteToken, referral }, }); try { localStorage.removeItem("sf_ref"); } catch { // storage blocked } router.replace(target); router.refresh(); } catch (e) { if (e instanceof ApiError && e.code === "not_invited") setUninvited(tokenEmail(response.authorization.id_token)); else setError(errorText(e)); setBusy(false); } } return (