Provider configuration lets your Next.js app connect with services like Google or GitHub for login. It makes signing in easy and safe.
Provider configuration (OAuth, credentials) in NextJS
import NextAuth from "next-auth"; import GoogleProvider from "next-auth/providers/google"; export default NextAuth({ providers: [ GoogleProvider({ clientId: process.env.GOOGLE_CLIENT_ID, clientSecret: process.env.GOOGLE_CLIENT_SECRET }) ] });
Use environment variables to keep your client ID and secret safe.
Each provider needs its own client ID and secret from its developer console.
import NextAuth from "next-auth"; import CredentialsProvider from "next-auth/providers/credentials"; export default NextAuth({ providers: [ CredentialsProvider({ name: "Credentials", credentials: { username: { label: "Username", type: "text" }, password: { label: "Password", type: "password" } }, async authorize(credentials) { if (credentials.username === "user" && credentials.password === "pass") { return { id: 1, name: "User" }; } return null; } }) ] });
import NextAuth from "next-auth"; import GitHubProvider from "next-auth/providers/github"; export default NextAuth({ providers: [ GitHubProvider({ clientId: process.env.GITHUB_CLIENT_ID, clientSecret: process.env.GITHUB_CLIENT_SECRET }) ] });
This is a complete NextAuth configuration using Google OAuth. It uses environment variables for sensitive info. When users visit the sign-in page, they can choose Google to log in.
import NextAuth from "next-auth"; import GoogleProvider from "next-auth/providers/google"; export default NextAuth({ providers: [ GoogleProvider({ clientId: process.env.GOOGLE_CLIENT_ID, clientSecret: process.env.GOOGLE_CLIENT_SECRET }) ], secret: process.env.NEXTAUTH_SECRET });
Always keep your client secrets in environment variables, never in code.
OAuth providers require you to register your app and get client IDs and secrets.
Credentials provider needs you to write your own user check logic inside authorize.
Provider configuration connects your app to login services like Google or GitHub.
Use environment variables to keep secrets safe.
Choose OAuth providers for social login or credentials for username/password.