Social Providers

Configure OAuth providers for social login (Google, GitHub, etc.)

In addition to the email/password and magic link authentication methods, you can also enable OAuth providers for social login.

By default, we add the Google OAuth sign-in as a social provider. You can customize these as you see fit.

apps/web/.env.local

NEXT_PUBLIC_AUTH_OAUTH_PROVIDERS=google,github

In addition to adding the OAuth providers you want to use, you will need to add the required provider configuration in the better-auth package in packages/better-auth/src/plugins/social-providers.ts.

For Google, it looks like the following:

import { socialProviders } from 'better-auth/social-providers';
import * as z from 'zod';
function createGoogleProviderPlugin() {
const googleClientId = z
.string()
.min(1)
.optional()
.parse(process.env.GOOGLE_CLIENT_ID);
const googleClientSecret = z
.string()
.min(1)
.optional()
.parse(process.env.GOOGLE_CLIENT_SECRET);
if (!googleClientId || !googleClientSecret) {
return [] as never;
}
return [
socialProviders.google({
clientId: googleClientId,
clientSecret: googleClientSecret,
}),
];
}
/**
* @name createSocialProviderPlugin
* @description Create a social provider plugin
* @returns A BetterAuth plugin
*/
export const createSocialProviderPlugin = () => {
const googleSocialProviderPlugin = createGoogleProviderPlugin();
return [
...googleSocialProviderPlugin,
];
};

Redirect URL

Please make sure to set the redirect URL in the OAuth provider dashboard:

https://<your-site-url>/api/auth/callback/<provider-name>

Note: if you're using Github, you must replace <your-site-url> with your actual site URL and <provider-name> with github. Ex:

https://my-site.com/api/auth/callback/github

Please refer to Better Auth documentation for more information on how to set the redirect URL for each provider.

Adding new OAuth providers

To create a new OAuth provider, you can follow the same pattern and add the plugin to the createSocialProviderPlugin function.

For example, if you want to add GitHub, you can follow the Better Auth Github documentation to create the plugin.

While the Zod validation is optional, it is recommended to make sure you're supplying the required environment variables for the provider.


Next: Multi-Factor Authentication Configuration