- ✅ Compatible with Payload v3
- 🔐 Configures OAuth2 with any providers
- ✨ Zero dependencies
- ⚙ Highly customizable
npm install payload-oauth2
yarn install payload-oauth2
Integrating Google OAuth2 to users
collection.
export default buildConfig({
// ...
plugins: [
oAuthPlugin({
enabled: true,
serverURL: process.env.NEXT_PUBLIC_URL || "http://localhost:3000",
authCollection: "users", // assuming you already have a users collection with auth enabled
clientId: process.env.CLIENT_ID || "",
clientSecret: process.env.CLIENT_SECRET || "",
tokenEndpoint: "https://oauth2.googleapis.com/token",
scopes: [
"https://www.googleapis.com/auth/userinfo.email",
"https://www.googleapis.com/auth/userinfo.profile",
"openid",
],
providerAuthorizationUrl: "https://accounts.google.com/o/oauth2/v2/auth",
getUserInfo: async (accessToken: string) => {
const response = await fetch(
"https://www.googleapis.com/oauth2/v3/userinfo",
{ headers: { Authorization: `Bearer ${accessToken}` } }
);
const user = await response.json();
return { email: user.email, sub: user.sub };
},
successRedirect: () => "/admin",
failureRedirect: () => "/login",
OAuthLoginButton, // a simple link to authorization path (/api/users/oauth/authorize)
}),
],
// ...
});
Contributions and feedback are very welcome.
To get it running:
- Clone the project.
pnpm install
pnpm dev
The MIT License (MIT). Please see License File for more information.
This package was inspired by Payload Plugin OAuth