Files
sorsfonalak/services/frontend/src/app/belepes/GoogleLogin.tsx
T

136 lines
4.8 KiB
TypeScript
Raw Normal View History

"use client";
import Script from "next/script";
import { useRouter } from "next/navigation";
import { useCallback, useEffect, useRef, useState } from "react";
import { Waitlist } from "@/components/Waitlist";
import { ApiError, api, errorText } from "@/lib/api";
2026-11-17 05:51:30 +00:00
/** Google's "G" mark (branding guidelines: unmodified, on the left of the label). */
function GoogleMark() {
return (
<svg width="20" height="20" viewBox="0 0 48 48" aria-hidden="true">
<path
fill="#EA4335"
d="M24 9.5c3.54 0 6.71 1.22 9.21 3.6l6.85-6.85C35.9 2.38 30.47 0 24 0 14.62 0 6.51 5.38 2.56 13.22l7.98 6.19C12.43 13.72 17.74 9.5 24 9.5z"
/>
<path
fill="#4285F4"
d="M46.98 24.55c0-1.57-.15-3.09-.38-4.55H24v9.02h12.94c-.58 2.96-2.26 5.48-4.78 7.18l7.73 6c4.51-4.18 7.09-10.36 7.09-17.65z"
/>
<path
fill="#FBBC05"
d="M10.53 28.59c-.48-1.45-.76-2.99-.76-4.59s.27-3.14.76-4.59l-7.98-6.19C.92 16.46 0 20.12 0 24c0 3.88.92 7.54 2.56 10.78l7.97-6.19z"
/>
<path
fill="#34A853"
d="M24 48c6.48 0 11.93-2.13 15.89-5.81l-7.73-6c-2.15 1.45-4.92 2.3-8.16 2.3-6.26 0-11.57-4.22-13.47-9.91l-7.98 6.19C6.51 42.62 14.62 48 24 48z"
/>
</svg>
);
}
/** The e-mail address inside Google's ID token (only for prefilling a form; the backend verifies the token). */
function tokenEmail(credential: string): string {
try {
const part = credential.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 "";
}
}
2026-11-17 05:51:30 +00:00
/**
* "Continue with Google" in the site's own button style. Google's real
* (rendered, invisible) button lies on top of it and receives the click, so
* the sign-in still returns an ID token.
*/
export function GoogleLogin({
clientId,
target,
inviteToken = null,
}: {
clientId: string;
target: string;
/** From an invitation link: lets in any Google account. */
inviteToken?: string | null;
}) {
const router = useRouter();
const slot = useRef<HTMLDivElement>(null);
const [ready, setReady] = useState(false);
const [error, setError] = useState<string | null>(null);
const [busy, setBusy] = useState(false);
/** Set when the address has no invitation: offer the waitlist for it. */
const [uninvited, setUninvited] = useState<string | null>(null);
const onCredential = useCallback(
async (response: GoogleCredentialResponse) => {
setBusy(true);
setError(null);
setUninvited(null);
try {
await api("/auth/google", { body: { id_token: response.credential, invite_token: inviteToken } });
router.replace(target);
router.refresh();
} catch (e) {
if (e instanceof ApiError && e.code === "not_invited") setUninvited(tokenEmail(response.credential));
else setError(errorText(e));
setBusy(false);
}
},
[router, target, inviteToken],
);
useEffect(() => {
const google = window.google;
if (!ready || !google || !slot.current) return;
google.accounts.id.initialize({ client_id: clientId, callback: onCredential, ux_mode: "popup", itp_support: true });
google.accounts.id.renderButton(slot.current, {
2026-11-17 05:51:30 +00:00
theme: "outline",
size: "large",
text: "continue_with",
shape: "rectangular",
locale: "hu",
width: Math.min(400, slot.current.clientWidth || 400),
});
}, [ready, clientId, onCredential]);
return (
<div className="field">
<Script
src="https://accounts.google.com/gsi/client"
strategy="afterInteractive"
onReady={() => setReady(true)}
onError={() => setError("Nem sikerült betölteni a Google-belépést. Ellenőrizd a hálózatot vagy a reklámblokkolót.")}
/>
2026-11-17 05:51:30 +00:00
<div className="social-login" data-ready={ready} aria-busy={busy}>
<span className="btn-social" aria-hidden="true">
<span className="btn-social-icon">
<GoogleMark />
</span>
{busy ? "Belépés…" : "Folytatás Google-fiókkal"}
</span>
<div ref={slot} className="gsi-overlay" />
</div>
{error && (
<p className="msg msg-error" role="alert">
{error}
</p>
)}
{uninvited !== null && (
<>
<p className="msg msg-error" role="alert">
{inviteToken
? "Ez a meghívó már nem érvényes, és ehhez a Google-fiókhoz sem tartozik másik."
: `A Sorsfonalak jelenleg meghívásos, és ${uninvited ? `a(z) ${uninvited} címhez` : "ehhez a fiókhoz"} nem tartozik meghívó. Ha kaptál meghívót egy másik címre, lépj be azzal a Google-fiókkal, vagy nyisd meg a levélben kapott linket.`}
</p>
<Waitlist email={uninvited} />
</>
)}
</div>
);
}