payload-oauth2
TypeScript icon, indicating that this package has built-in type declarations

0.0.9 • Public • Published

Payload OAuth2 Plugin

Software License Issues NPM

Features

  • ✅ Compatible with Payload v3
  • 🔐 Configures OAuth2 with any providers
  • ✨ Zero dependencies
  • ⚙ Highly customizable

Installation

npm install payload-oauth2
yarn install payload-oauth2

Example Usage

Integrating Google OAuth2 to users collection.

export default buildConfig({
  // ...
  plugins: [
    OAuth2Plugin({
      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)
    }),
  ],
  // ...
});

Contributing

Contributions and feedback are very welcome.

To get it running:

  1. Clone the project.
  2. pnpm install
  3. pnpm dev

License

The MIT License (MIT). Please see License File for more information.

Credits

This package was inspired by Payload Plugin OAuth

Package Sidebar

Install

npm i payload-oauth2

Weekly Downloads

192

Version

0.0.9

License

MIT

Unpacked Size

28.5 kB

Total Files

21

Last publish

Collaborators

  • wilsonle