Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions apps/web/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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 <login@auth.helpbase.dev>"

# ── 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=
Expand Down
140 changes: 120 additions & 20 deletions apps/web/app/device/AuthorizeDeviceClient.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<Phase>({ 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(() => {
Expand All @@ -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<HTMLFormElement>) {
event.preventDefault()
if (!email) return
Expand Down Expand Up @@ -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 (
<form onSubmit={handleMagicLink} className="space-y-4">
<div className="space-y-4">
{userCode && (
<div className="rounded border border-neutral-200 bg-neutral-50 p-3 text-center text-xs text-neutral-600 dark:border-neutral-800 dark:bg-neutral-950 dark:text-neutral-400">
<div className="uppercase tracking-wider text-neutral-500">
Expand All @@ -135,27 +171,60 @@ export function AuthorizeDeviceClient({ initialUserCode }: { initialUserCode: st
</div>
</div>
)}
<label className="block text-sm font-medium">Email</label>
<input
type="email"
required
value={email}
onChange={(e) => 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"
/>
<button
type="submit"
disabled={submitting}
className="w-full rounded-md bg-neutral-900 px-4 py-2 text-sm font-medium text-white hover:bg-neutral-800 disabled:opacity-50 dark:bg-white dark:text-neutral-900"
>
{submitting ? "Sending…" : "Send sign-in link"}
</button>
{anySocial && (
<div className="space-y-2">
{providers.google && (
<button
type="button"
onClick={() => handleSocialSignIn("google")}
disabled={socialBusy || submitting}
className="flex w-full items-center justify-center gap-2 rounded-md border border-neutral-300 bg-white px-4 py-2 text-sm font-medium text-neutral-900 hover:bg-neutral-50 disabled:opacity-50 dark:border-neutral-700 dark:bg-neutral-900 dark:text-neutral-100 dark:hover:bg-neutral-800"
>
<GoogleIcon />
{socialSubmitting === "google" ? "Redirecting…" : "Continue with Google"}
</button>
)}
{providers.github && (
<button
type="button"
onClick={() => handleSocialSignIn("github")}
disabled={socialBusy || submitting}
className="flex w-full items-center justify-center gap-2 rounded-md border border-neutral-300 bg-white px-4 py-2 text-sm font-medium text-neutral-900 hover:bg-neutral-50 disabled:opacity-50 dark:border-neutral-700 dark:bg-neutral-900 dark:text-neutral-100 dark:hover:bg-neutral-800"
>
<GitHubIcon />
{socialSubmitting === "github" ? "Redirecting…" : "Continue with GitHub"}
</button>
)}
<div className="flex items-center gap-2 py-1 text-xs uppercase tracking-wider text-neutral-400">
<div className="h-px flex-1 bg-neutral-200 dark:bg-neutral-800" />
<span>or</span>
<div className="h-px flex-1 bg-neutral-200 dark:bg-neutral-800" />
</div>
</div>
)}
<form onSubmit={handleMagicLink} className="space-y-4">
<label className="block text-sm font-medium">Email</label>
<input
type="email"
required
value={email}
onChange={(e) => 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"
/>
<button
type="submit"
disabled={submitting || socialBusy}
className="w-full rounded-md bg-neutral-900 px-4 py-2 text-sm font-medium text-white hover:bg-neutral-800 disabled:opacity-50 dark:bg-white dark:text-neutral-900"
>
{submitting ? "Sending…" : "Send sign-in link"}
</button>
</form>
<p className="text-center text-xs text-neutral-500">
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.
</p>
</form>
</div>
)
}

Expand Down Expand Up @@ -262,3 +331,34 @@ export function AuthorizeDeviceClient({ initialUserCode }: { initialUserCode: st
</div>
)
}

function GoogleIcon() {
return (
<svg width="16" height="16" viewBox="0 0 18 18" aria-hidden="true">
<path
fill="#4285F4"
d="M17.64 9.2c0-.637-.057-1.251-.164-1.84H9v3.481h4.844a4.14 4.14 0 0 1-1.796 2.716v2.258h2.908c1.702-1.567 2.684-3.874 2.684-6.615z"
/>
<path
fill="#34A853"
d="M9 18c2.43 0 4.467-.806 5.956-2.184l-2.908-2.259c-.806.54-1.837.86-3.048.86-2.344 0-4.328-1.584-5.036-3.711H.957v2.332A8.997 8.997 0 0 0 9 18z"
/>
<path
fill="#FBBC05"
d="M3.964 10.707a5.41 5.41 0 0 1-.282-1.707c0-.593.102-1.17.282-1.707V4.958H.957A8.996 8.996 0 0 0 0 9c0 1.452.348 2.827.957 4.042l3.007-2.335z"
/>
<path
fill="#EA4335"
d="M9 3.58c1.321 0 2.508.454 3.44 1.345l2.582-2.58C13.463.891 11.426 0 9 0A8.997 8.997 0 0 0 .957 4.958L3.964 7.29C4.672 5.163 6.656 3.58 9 3.58z"
/>
</svg>
)
}

function GitHubIcon() {
return (
<svg width="16" height="16" viewBox="0 0 16 16" fill="currentColor" aria-hidden="true">
<path d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.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.51-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.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.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.012 8.012 0 0 0 16 8c0-4.42-3.58-8-8-8z" />
</svg>
)
}
11 changes: 10 additions & 1 deletion apps/web/app/device/page.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Metadata } from "next"
import { authProviders } from "@/lib/auth"
import { AuthorizeDeviceClient } from "./AuthorizeDeviceClient"

export const metadata: Metadata = {
Expand All @@ -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 (
<main className="mx-auto flex min-h-screen max-w-md flex-col items-center justify-center px-6 py-12">
<div className="w-full rounded-lg border border-neutral-200 bg-white p-8 shadow-sm dark:border-neutral-800 dark:bg-neutral-900">
Expand All @@ -26,7 +35,7 @@ export default async function DevicePage({
A CLI session on your device is waiting for approval.
</p>
</div>
<AuthorizeDeviceClient initialUserCode={userCode} />
<AuthorizeDeviceClient initialUserCode={userCode} providers={providers} />
<div className="mt-6 border-t border-neutral-200 pt-4 text-center text-xs text-neutral-500 dark:border-neutral-800">
Compare the code above to what your terminal shows. If they don't
match, close this tab and ignore the link.
Expand Down
34 changes: 34 additions & 0 deletions apps/web/lib/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <login@helpbase.dev>"

// 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<Parameters<typeof betterAuth>[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
Expand Down Expand Up @@ -88,6 +121,7 @@ async function deliverMagicLink(email: string, url: string): Promise<void> {

export const auth = betterAuth({
database: new Pool({ connectionString: DATABASE_URL }),
socialProviders: buildSocialProviders(),
plugins: [
bearer(),
magicLink({
Expand Down
Loading