Facebook Provider
Configure Facebook OAuth for authentication in your Next.js application using NextAuth.js.
Documentation
https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow/
Configuration
https://developers.facebook.com/apps/
Facebook Developer Setup
Follow these steps to configure Facebook OAuth:
Step 1: Create a Facebook App
- Go to Facebook Developers
- Click Create App
- Select Consumer or None (Build something else)
- Enter your app display name and contact email
- Click Create App
Step 2: Add Facebook Login Product
- In your app dashboard, find Facebook Login and click Set Up
- Select Web as the platform
- Enter your site URL (e.g., https://your-domain.com)
- Click Save and continue through the quickstart
Step 3: Configure OAuth Settings
- Navigate to Facebook Login → Settings in the left sidebar
- Add your Valid OAuth Redirect URIs:
| Environment | Redirect URI |
|---|---|
| Development | http://localhost:3000/api/auth/callback/facebook |
| Production | https://YOUR_DOMAIN/api/auth/callback/facebook |
Step 4: Get App ID and Secret
- Go to Settings → Basic in the left sidebar
- Copy the App ID and App Secret
- Add them to your
.env.localfile:
# .env.local
FACEBOOK_CLIENT_ID=your-app-id
FACEBOOK_CLIENT_SECRET=your-app-secretStep 5: Configure App Domains
- In Settings → Basic, scroll down to App Domains
- Add your domain (e.g., your-domain.com)
- Add Privacy Policy URL and Terms of Service URL (required for public apps)
- Click Save Changes
Step 6: Switch to Live Mode
- At the top of the dashboard, toggle App Mode from Development to Live
- Complete any required verification steps
- Note: You need a Privacy Policy URL to switch to Live mode
Options
The Facebook Provider comes with a set of default options. You can override any of the options to suit your own use case.
Example
// src/core/lib/auth.ts
import FacebookProvider from "next-auth/providers/facebook";
export const authOptions: NextAuthOptions = {
providers: [
FacebookProvider({
clientId: process.env.FACEBOOK_CLIENT_ID!,
clientSecret: process.env.FACEBOOK_CLIENT_SECRET!,
}),
],
};Tip: Development App
Production applications cannot use localhost URLs to sign in with Facebook. You need to use a dedicated development application in Facebook to use localhost callback URLs.
Tip: Mobile Email Addresses
Email address may not be returned for accounts created on mobile.
Advanced Configuration
Request Additional Permissions
You can request additional permissions (scopes) from Facebook:
// src/core/lib/auth.ts
import FacebookProvider from "next-auth/providers/facebook";
export const authOptions: NextAuthOptions = {
providers: [
FacebookProvider({
clientId: process.env.FACEBOOK_CLIENT_ID!,
clientSecret: process.env.FACEBOOK_CLIENT_SECRET!,
authorization: {
params: {
scope: "email,public_profile,user_birthday",
},
},
}),
],
};Custom Profile Fields
Request specific user profile fields:
// src/core/lib/auth.ts
import FacebookProvider from "next-auth/providers/facebook";
export const authOptions: NextAuthOptions = {
providers: [
FacebookProvider({
clientId: process.env.FACEBOOK_CLIENT_ID!,
clientSecret: process.env.FACEBOOK_CLIENT_SECRET!,
userinfo: {
params: {
fields: "id,name,email,picture",
},
},
}),
],
};Troubleshooting
Common Errors
| Error | Solution |
|---|---|
| URL Blocked | Add your redirect URI to Valid OAuth Redirect URIs in Facebook Login Settings |
| App Not Set Up | Make sure Facebook Login product is added and configured |
| Invalid App ID | Verify FACEBOOK_CLIENT_ID is correctly set in .env.local |
| Cannot use localhost | Create a separate development app for localhost testing |
| Email not returned | User may have signed up via phone; handle missing email in your app |