Skip to content
← All posts

·

How to add Google and GitHub login to your web app

Add email, Google and GitHub sign-in to your app: OAuth apps, callback URLs, sessions and protected pages. The manual steps, and what Launchfast does.

Short answer: pick an auth library, create one OAuth app per provider, register the exact callback URL for each environment, store the client ID and secret in env vars, and check the session on the server before rendering protected pages. Launchfast already runs Better Auth with email, Google and GitHub login. Each social provider switches on once its two env vars are set, and pages call requireUser() to protect themselves.

What goes wrong when you add social login?

Sign-in looks like a one-hour job and then fails in small, confusing ways. These are the symptoms people search for:

  • redirect_uri_mismatch from Google, because the callback URL in the console differs from the one your app sends. A trailing slash, http vs https, or a different port is enough.
  • GitHub's "The redirect_uri is not associated with this application" page.
  • Login works on localhost and breaks on the deployed site, because the OAuth app only knows the local URL.
  • A user signs up with a password, later clicks "Continue with Google" with the same email, and ends up with two accounts or an error.
  • Pages that are only hidden in the UI but still render for anyone who opens the URL.

How do you do it by hand?

  1. Choose a library. Writing the OAuth flow yourself means handling state, PKCE, token exchange and session cookies. Most people use a library such as Better Auth, Auth.js or a hosted service.
  2. Create the database tables. Users, sessions, linked accounts and verification tokens. The library's docs list the exact columns.
  3. Create a Google OAuth client. In Google Cloud Console, open APIs & Services, configure the consent screen, then create an OAuth client ID of type Web application. Add the authorized redirect URI for local development.
  4. Create a GitHub OAuth app. In GitHub's developer settings, create a new OAuth App and set the authorization callback URL. A GitHub OAuth app takes one callback URL, so people often keep a second app for production.
  5. Find the callback path your library expects. With Better Auth it is /api/auth/callback/<provider>, for example http://localhost:3000/api/auth/callback/google.
  6. Put the keys in env vars. GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET, GITHUB_CLIENT_ID and GITHUB_CLIENT_SECRET, plus a random auth secret. Keep the secrets on the server only.
  7. Mount the auth route. In the App Router that is a catch-all route handler under app/api/auth/[...all]/route.ts.
  8. Build the sign-in and sign-up pages. Buttons that start the provider flow, an email and password form, and a place to show errors when the provider says no.
  9. Decide what happens when an email already exists. If you link accounts automatically, only do it when the provider has verified the email. Otherwise someone can register your email first with a password and keep a way into your account.
  10. Protect pages on the server. Read the session in the page or layout and redirect when it is missing. A cookie check in middleware is a shortcut for speed, not a security check.
  11. Repeat for production. New callback URLs for your real domain in both consoles, the same env vars on your host, and the app URL set correctly so the library builds the right redirect.

The traps are in steps 5, 9, 10 and 11. Each one fails quietly until a real user hits it.

What does Launchfast already do?

Launchfast uses Better Auth with Drizzle. The pieces are in the repository.

The auth route and tables exist. src/app/api/auth/[...all]/route.ts mounts Better Auth, and src/db/schema.ts has the user, session, account and verification tables with the field names Better Auth expects.

Google and GitHub switch on by themselves. src/lib/auth-providers.ts adds a provider only when both of its env vars exist:

export const socialProviders = {
  ...(process.env.GOOGLE_CLIENT_ID &&
    process.env.GOOGLE_CLIENT_SECRET && {
      google: {
        clientId: process.env.GOOGLE_CLIENT_ID,
        clientSecret: process.env.GOOGLE_CLIENT_SECRET,
      },
    }),
  // GitHub is the same shape with GITHUB_CLIENT_ID and GITHUB_CLIENT_SECRET
};

The sign-in and sign-up pages read enabledSocialProviders from the same file, so a button only appears for a provider you configured. .env.example lists the four variables and reminds you of the callback URLs: <NEXT_PUBLIC_APP_URL>/api/auth/callback/google and .../github.

npm run setup walks you through it. The setup script can't create OAuth apps for you, because Google and GitHub don't offer an API for it. Instead, the "Social sign-in" step asks whether you want Google and GitHub, prints where to click (Google Cloud Console credentials, or GitHub's developer settings) and the exact callback URL to paste, then saves the client ID and secret to .env.local. When you deploy, it copies the same keys to Vercel.

Account linking is handled. In src/lib/auth.ts, accountLinking lets a Google or GitHub login sign into an existing account whose email was never confirmed. A database hook in the same file then deletes the unproven password on that account, so whoever registered the email first can't keep a way in. They can set a new password with "Forgot password".

Email and password work alongside. Sign-up requires a confirmed email (requireEmailVerification: true), with a minimum password length of 8. Better Auth's rate limiting stores its counters in Postgres (rateLimit: { storage: "database" }), so limits hold across serverless instances.

Pages protect themselves. src/lib/session.ts exposes the only two functions pages need:

export async function getSession() {
  return auth.api.getSession({ headers: await headers() });
}

export async function requireUser() {
  const session = await getSession();
  if (!session) redirect("/sign-in");
  return session.user;
}

Because cacheComponents is on in Next.js 16, reading headers must happen inside a component wrapped in <Suspense>, so a signed-in page calls requireUser() from an inner component. src/proxy.ts also redirects visitors with no session cookie away from /admin, but it's only a speed-up. The admin pages check the session again on the server.

What should you know before relying on this?

You still create the Google and GitHub OAuth apps yourself, and you need a second callback URL for production. If you use one GitHub OAuth app for both, GitHub accepts a single callback URL, so use separate apps for local and live. Google's consent screen may also need verification before strangers outside your test users can sign in. Check Google's current rules for that.

How much does this save?

You skip the schema, the route handler, the sign-in pages, the account-linking edge case and the session helpers. What is left is two short forms in the Google and GitHub consoles.

Launchfast is the starter this post describes. For the next pieces of a launch, read how to set up a Postgres database for your web app, how to accept payments in your SaaS and the checklist I run before launching a SaaS.