import React, { ReactNode, useState } from "react"; import { Button } from "antd"; import { GithubOutlined, GoogleOutlined, KeyOutlined } from "@ant-design/icons"; import { getLog } from "juava"; import { AuthType } from "./SignInOrUp"; const log = getLog("oauth-buttons"); type OAuthProvider = "firebase-google" | "firebase-github" | "nextauth-github" | "nextauth-oidc"; interface OAuthButtonsProps { providers?: Array; prefix?: string; onSSOLogin: (type: AuthType, providerId: string) => Promise; // Optional badge floated over the top of a specific provider's button (e.g. // the work-email hint on "Continue with Google" — JITSU-70). notes?: Partial>; } export const OAuthButtons: React.FC = ({ providers = ["firebase-google", "firebase-github", "nextauth-github", "nextauth-oidc"], prefix = "Sign in", onSSOLogin, notes, }) => { const [loading, setLoading] = useState(); const handleLogin = async (provider: AuthType) => { try { setLoading(provider); await onSSOLogin(provider, provider); } finally { setLoading(undefined); } }; // Float the note as a small badge over the button's top-right corner. It's a // sibling of the button (not a child), so it doesn't intercept the button's // click — and it stays interactive so a tooltip on it works. const withBadge = (provider: OAuthProvider, button: ReactNode) => { const note = notes?.[provider]; if (!note) { return button; } return (
{button}
{note}
); }; const renderButton = (provider: string) => { switch (provider) { case "firebase-google": return withBadge( "firebase-google", ); case "firebase-github": return withBadge( "firebase-github", ); case "nextauth-github": return withBadge( "nextauth-github", ); case "nextauth-oidc": return withBadge( "nextauth-oidc", ); default: return null; } }; return (
{providers.map(renderButton)}
); };