Documentation
OAuth Setup
Step-by-step Google and GitHub OAuth setup for Motoko Base — create provider apps, configure callback URLs, and fill in .env.
Google and GitHub sign-in are optional. Leave the OAuth env vars unset and the app boots normally — social buttons on /sign-in and /sign-up stay disabled.
Once configured, Better Auth handles the OAuth flow and stores linked accounts in the account table. Callback URLs must exactly match your BETTER_AUTH_URL.
Before you start
Make sure these auth env vars are already set:
# .env or .env.local
DATABASE_URL=postgresql://...
BETTER_AUTH_SECRET=your-generated-secret
BETTER_AUTH_URL=http://localhost:3000
NEXT_PUBLIC_APP_URL=http://localhost:3000For local development, use http://localhost:3000 for both URL variables. In production, set them to your public HTTPS origin (e.g. https://app.example.com).
Generate BETTER_AUTH_SECRET if you have not already:
openssl rand -base64 32Restart the dev server after changing any env var:
pnpm devGoogle OAuth
Step 1 — Create a Google Cloud project
- Open Google Cloud Console
- Create a new project (or select an existing one)
- Go to APIs & Services → OAuth consent screen
- Choose External (or Internal for Workspace-only apps)
- Fill in the app name, support email, and developer contact
- Add scopes:
email,profile, andopenid(defaults are usually enough) - Add test users if the app is in Testing mode
Step 2 — Create OAuth credentials
- Go to APIs & Services → Credentials
- Click Create Credentials → OAuth client ID
- Application type: Web application
- Name it (e.g.
Motoko Base local)
Step 3 — Set the redirect URI
Under Authorized redirect URIs, add:
{BETTER_AUTH_URL}/api/auth/callback/googleExamples:
| Environment | Redirect URI |
|---|---|
| Local | http://localhost:3000/api/auth/callback/google |
| Production | https://app.example.com/api/auth/callback/google |
The path must be exactly /api/auth/callback/google — Better Auth registers this route automatically.
Optionally add Authorized JavaScript origins for local dev:
http://localhost:3000Step 4 — Copy credentials to .env
After creating the client, Google shows a Client ID and Client secret. Add them to .env:
GOOGLE_CLIENT_ID=123456789-abcdef.apps.googleusercontent.com
GOOGLE_CLIENT_SECRET=GOCSPX-xxxxxxxxxxxxxxxxBoth values are server-only — never put them on NEXT_PUBLIC_*.
Step 5 — Verify locally
- Restart the dev server:
pnpm dev - Open http://localhost:3000/sign-in
- The Sign in with Google button should be enabled (not greyed out)
- Click it — you should be redirected to Google, then back to
/dashboard
If the button stays disabled, check that both GOOGLE_CLIENT_ID and GOOGLE_CLIENT_SECRET are set and non-empty.
Google troubleshooting
| Problem | Fix |
|---|---|
redirect_uri_mismatch | Redirect URI in Google Console must match {BETTER_AUTH_URL}/api/auth/callback/google exactly (scheme, host, port, path) |
| Button disabled | Both GOOGLE_* vars must be set; restart dev server |
access_denied in Testing mode | Add your Google account as a test user on the consent screen |
| Works locally, fails in production | Add production redirect URI and set BETTER_AUTH_URL to your live domain |
GitHub OAuth
Step 1 — Create a GitHub OAuth App
- Open GitHub Developer Settings → OAuth Apps
- Click New OAuth App
- Fill in:
- Application name — e.g.
Motoko Base - Homepage URL — your app URL (local:
http://localhost:3000) - Authorization callback URL — see Step 2
- Application name — e.g.
Step 2 — Set the callback URL
The Authorization callback URL must be:
{BETTER_AUTH_URL}/api/auth/callback/githubExamples:
| Environment | Callback URL |
|---|---|
| Local | http://localhost:3000/api/auth/callback/github |
| Production | https://app.example.com/api/auth/callback/github |
Click Register application.
Step 3 — Generate a client secret
- On the OAuth app page, click Generate a new client secret
- Copy the Client ID and Client secret immediately — the secret is shown only once
Step 4 — Copy credentials to .env
GITHUB_CLIENT_ID=Iv1.xxxxxxxxxxxxxxxx
GITHUB_CLIENT_SECRET=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxBoth values are server-only.
Step 5 — Verify locally
- Restart the dev server:
pnpm dev - Open http://localhost:3000/sign-in
- The Sign in with GitHub button should be enabled
- Click it — authorize the app, then land on
/dashboard
GitHub Apps vs OAuth Apps
Motoko Base uses a standard OAuth App (not a GitHub App). If you use a GitHub App instead, ensure Account permissions → Email addresses is set to Read-only so Better Auth can read the user's verified email.
GitHub troubleshooting
| Problem | Fix |
|---|---|
redirect_uri mismatch | Callback URL must be {BETTER_AUTH_URL}/api/auth/callback/github exactly |
| Button disabled | Both GITHUB_* vars must be set; restart dev server |
| Missing email / sign-in fails | For GitHub Apps, enable Email addresses read permission; OAuth Apps include email by default |
| Works locally, fails in production | Update callback URL in GitHub settings and set production BETTER_AUTH_URL |
Production checklist
Use this checklist when deploying OAuth to staging or production:
-
Set canonical URLs
BETTER_AUTH_URL=https://app.example.com NEXT_PUBLIC_APP_URL=https://app.example.com -
Add production callback URLs in Google Console and GitHub OAuth app settings (keep local URIs if you still develop locally)
-
Optional — extra origins for preview deployments:
BETTER_AUTH_TRUSTED_ORIGINS=https://staging.example.com,https://*.vercel.app -
Restart / redeploy after env changes
-
Test both providers on the live URL — sign in, sign out, and confirm the session persists
Where OAuth is wired in code
You usually only need env vars. To customize behavior, edit these files:
| File | Purpose |
|---|---|
src/lib/auth/auth.ts | Registers socialProviders.google and socialProviders.github |
src/lib/auth/social.ts | isGoogleAuthConfigured() / isGitHubAuthConfigured() |
src/app/sign-in/sign-in-form.tsx | Google/GitHub sign-in handlers |
src/app/sign-up/sign-up-form.tsx | Google/GitHub sign-up handlers |
src/app/sign-in/page.tsx | Passes enabled flags to the form |
OAuth secrets never belong in TypeScript config files — only in .env.
Next steps
Better Auth — Full auth overview, sessions, protected routes, and file map.
Environment Variables — All auth-related env vars in one place.