diff --git a/CHANGELOG.md b/CHANGELOG.md index 41fc9f2..697b930 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added +- **OAuth providers on `/device`.** Opt-in Google + GitHub sign-in + buttons render above the magic-link form when the provider's + `*_CLIENT_ID` and `*_CLIENT_SECRET` env vars are configured on the + server. Buttons stay hidden until configured, and the email + magic-link path always remains available as a fallback. Cold-path + TTHW drops from ~60s (email round-trip) to ~20s (one-click OAuth). + ## [helpbase 0.5.0] — 2026-04-17 ### Added diff --git a/apps/web/.env.example b/apps/web/.env.example index 86b1d85..90c9986 100644 --- a/apps/web/.env.example +++ b/apps/web/.env.example @@ -31,6 +31,24 @@ RESEND_API_KEY= # From-address for sign-in emails. Requires the domain to be verified in Resend. RESEND_FROM_EMAIL="helpbase " +# ── OAuth providers (optional) ─────────────────────────────────────────────── +# Both pairs are optional. Set either/both to enable "Continue with Google" / +# "Continue with GitHub" on /device. Buttons render only when both id + secret +# for a provider are present; email magic-link stays available regardless. +# +# Google Cloud Console → APIs & Services → Credentials → OAuth client ID +# Application type: Web application +# Authorized redirect URI: {BETTER_AUTH_URL}/api/auth/callback/google +# (e.g. https://helpbase.dev/api/auth/callback/google for prod) +GOOGLE_CLIENT_ID= +GOOGLE_CLIENT_SECRET= + +# GitHub → Settings → Developer settings → OAuth Apps → New OAuth App +# Homepage URL: {BETTER_AUTH_URL} +# Authorization callback URL: {BETTER_AUTH_URL}/api/auth/callback/github +GITHUB_CLIENT_ID= +GITHUB_CLIENT_SECRET= + # ── Supabase (existing — used for tenant content, NOT auth) ────────────────── NEXT_PUBLIC_SUPABASE_URL= NEXT_PUBLIC_SUPABASE_ANON_KEY= diff --git a/apps/web/app/device/AuthorizeDeviceClient.tsx b/apps/web/app/device/AuthorizeDeviceClient.tsx index f6ee0cb..baa28e3 100644 --- a/apps/web/app/device/AuthorizeDeviceClient.tsx +++ b/apps/web/app/device/AuthorizeDeviceClient.tsx @@ -23,12 +23,24 @@ type Phase = | { kind: "denied" } | { kind: "error"; message: string } -export function AuthorizeDeviceClient({ initialUserCode }: { initialUserCode: string }) { +export interface ProviderAvailability { + google: boolean + github: boolean +} + +export function AuthorizeDeviceClient({ + initialUserCode, + providers, +}: { + initialUserCode: string + providers: ProviderAvailability +}) { const { data: session, isPending: sessionPending } = authClient.useSession() const [userCode, setUserCode] = useState(initialUserCode) const [phase, setPhase] = useState({ kind: "loading" }) const [email, setEmail] = useState("") const [submitting, setSubmitting] = useState(false) + const [socialSubmitting, setSocialSubmitting] = useState<"google" | "github" | null>(null) // Derive the UI phase from session state + URL-provided user_code. useEffect(() => { @@ -53,6 +65,28 @@ export function AuthorizeDeviceClient({ initialUserCode }: { initialUserCode: st }) }, [sessionPending, session, userCode]) + async function handleSocialSignIn(provider: "google" | "github") { + setSocialSubmitting(provider) + try { + // callbackURL preserves user_code so the OAuth round-trip lands + // back on the same device-authorize surface. Better Auth drops the + // rest of the query string, so anything not in callbackURL is lost. + const callback = userCode + ? `/device?user_code=${encodeURIComponent(userCode)}` + : "/device" + await authClient.signIn.social({ provider, callbackURL: callback }) + // On success, the browser is already redirecting to the OAuth + // provider; nothing to do here. If signIn.social rejects, we fall + // through to the catch. + } catch (err) { + setSocialSubmitting(null) + setPhase({ + kind: "error", + message: err instanceof Error ? err.message : String(err), + }) + } + } + async function handleMagicLink(event: FormEvent) { event.preventDefault() if (!email) return @@ -123,8 +157,10 @@ export function AuthorizeDeviceClient({ initialUserCode }: { initialUserCode: st } if (phase.kind === "signed-out") { + const anySocial = providers.google || providers.github + const socialBusy = socialSubmitting !== null return ( -
+
{userCode && (
@@ -135,27 +171,60 @@ export function AuthorizeDeviceClient({ initialUserCode }: { initialUserCode: st
)} - - setEmail(e.target.value)} - placeholder="you@company.com" - className="w-full rounded-md border border-neutral-300 bg-white px-3 py-2 text-sm dark:border-neutral-700 dark:bg-neutral-950" - /> - + {anySocial && ( +
+ {providers.google && ( + + )} + {providers.github && ( + + )} +
+
+ or +
+
+
+ )} + + + setEmail(e.target.value)} + placeholder="you@company.com" + className="w-full rounded-md border border-neutral-300 bg-white px-3 py-2 text-sm dark:border-neutral-700 dark:bg-neutral-950" + /> + +

- Don't have an account? Signing in with a new email creates one + Don't have an account? Signing in with a new {anySocial ? "provider or email" : "email"} creates one automatically.

- +
) } @@ -262,3 +331,34 @@ export function AuthorizeDeviceClient({ initialUserCode }: { initialUserCode: st
) } + +function GoogleIcon() { + return ( + + ) +} + +function GitHubIcon() { + return ( + + ) +} diff --git a/apps/web/app/device/page.tsx b/apps/web/app/device/page.tsx index ba2a7eb..ff98e63 100644 --- a/apps/web/app/device/page.tsx +++ b/apps/web/app/device/page.tsx @@ -1,4 +1,5 @@ import type { Metadata } from "next" +import { authProviders } from "@/lib/auth" import { AuthorizeDeviceClient } from "./AuthorizeDeviceClient" export const metadata: Metadata = { @@ -17,6 +18,14 @@ export default async function DevicePage({ const params = await searchParams const userCode = params.user_code?.trim() ?? "" + // Social-provider availability is computed server-side (env vars live + // on the server), then handed to the client so buttons only render for + // providers that are actually configured. + const providers = { + google: authProviders.google, + github: authProviders.github, + } + return (
@@ -26,7 +35,7 @@ export default async function DevicePage({ A CLI session on your device is waiting for approval.

- +
Compare the code above to what your terminal shows. If they don't match, close this tab and ignore the link. diff --git a/apps/web/lib/auth.ts b/apps/web/lib/auth.ts index 35c8a63..0642074 100644 --- a/apps/web/lib/auth.ts +++ b/apps/web/lib/auth.ts @@ -9,6 +9,39 @@ const DATABASE_URL = process.env.DATABASE_URL const RESEND_API_KEY = process.env.RESEND_API_KEY const FROM_EMAIL = process.env.RESEND_FROM_EMAIL ?? "helpbase " +// Social providers are opt-in: the config block is only added when BOTH +// the client id and secret are present in env. Lets us deploy the code +// without the OAuth apps being created yet, and lets teammates run +// locally without setting up their own OAuth clients. +const GOOGLE_CLIENT_ID = process.env.GOOGLE_CLIENT_ID +const GOOGLE_CLIENT_SECRET = process.env.GOOGLE_CLIENT_SECRET +const GITHUB_CLIENT_ID = process.env.GITHUB_CLIENT_ID +const GITHUB_CLIENT_SECRET = process.env.GITHUB_CLIENT_SECRET + +export const authProviders = { + google: Boolean(GOOGLE_CLIENT_ID && GOOGLE_CLIENT_SECRET), + github: Boolean(GITHUB_CLIENT_ID && GITHUB_CLIENT_SECRET), +} as const + +type SocialProvidersConfig = NonNullable[0]["socialProviders"]> + +function buildSocialProviders(): SocialProvidersConfig { + const providers: SocialProvidersConfig = {} + if (authProviders.google) { + providers.google = { + clientId: GOOGLE_CLIENT_ID!, + clientSecret: GOOGLE_CLIENT_SECRET!, + } + } + if (authProviders.github) { + providers.github = { + clientId: GITHUB_CLIENT_ID!, + clientSecret: GITHUB_CLIENT_SECRET!, + } + } + return providers +} + // Guards fire at runtime (server-side request handling), not during // `next build` static page-data collection. NEXT_PHASE is set to // 'phase-production-build' by Next.js during build so we can distinguish @@ -88,6 +121,7 @@ async function deliverMagicLink(email: string, url: string): Promise { export const auth = betterAuth({ database: new Pool({ connectionString: DATABASE_URL }), + socialProviders: buildSocialProviders(), plugins: [ bearer(), magicLink({