A SvelteKit template with Better Auth for OAuth social login, featuring multiple providers, protected routes, and a modern UI.
src/
├── lib/
│ ├── server/auth.ts # Better Auth server configuration
│ ├── auth-client.ts # Client-side auth utilities (signIn, signOut)
│ └── components/ # Reusable UI components
├── routes/
│ ├── +page.svelte # Home page
│ ├── dashboard/ # Protected dashboard page
│ └── api/auth/[...all]/ # Better Auth API routes
├── hooks.server.ts # Auth middleware & route protection
└── app.css # Global styles with CSS variables
git clone <repository-url>
cd sveltekit-better-auth-template
npm install
Copy the example file and fill in your credentials:
cp .env.example .env
npm run dev
Visit http://localhost:5173 to see your app.
| Variable | Description |
|---|---|
BETTER_AUTH_SECRET |
Secret key for encryption (min 32 chars). Generate with: openssl rand -base64 32 |
Configure one or more providers. Only providers with both CLIENT_ID and CLIENT_SECRET will be displayed.
| Provider | Variables | Description |
|---|---|---|
| GitHub | GITHUB_CLIENT_IDGITHUB_CLIENT_SECRET |
OAuth credentials from GitHub OAuth App |
GOOGLE_CLIENT_IDGOOGLE_CLIENT_SECRET |
OAuth credentials from Google Cloud Console | |
| Discord | DISCORD_CLIENT_IDDISCORD_CLIENT_SECRET |
OAuth credentials from Discord Developer Portal |
| Slack | SLACK_CLIENT_IDSLACK_CLIENT_SECRET |
OAuth credentials from Slack API |
| Vercel | VERCEL_CLIENT_IDVERCEL_CLIENT_SECRET |
OAuth credentials from Vercel Integrations |
Note: Better Auth supports 30+ OAuth providers including Apple, Microsoft, Twitter, LinkedIn, and more. The 5 providers above are just examples. Refer to the Better Auth documentation for integration guides.
Set the callback URL in each OAuth provider's settings:
http://localhost:5173/api/auth/callback/{provider}
Replace {provider} with: github, google, discord, slack, or vercel.
For production, replace http://localhost:5173 with your domain.
Add environment variables to .env:
NEWPROVIDER_CLIENT_ID=xxx
NEWPROVIDER_CLIENT_SECRET=xxx
Add provider config in src/lib/server/auth.ts:
socialProviders: {
// ...existing providers
newprovider: {
clientId: env.NEWPROVIDER_CLIENT_ID as string,
clientSecret: env.NEWPROVIDER_CLIENT_SECRET as string,
}
}
Add provider detection in src/routes/+page.server.ts:
{
id: 'newprovider',
name: 'New Provider',
enabled: !!(env.NEWPROVIDER_CLIENT_ID && env.NEWPROVIDER_CLIENT_SECRET)
}
Create icon component in src/lib/components/icons/NewProviderIcon.svelte
Register icon in src/lib/components/AuthProviderButton.svelte
MIT