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

0.0.11 • 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

Dependencies (0)

    Dev Dependencies (30)

    Package Sidebar

    Install

    npm i payload-oauth2

    Weekly Downloads

    97

    Version

    0.0.11

    License

    MIT

    Unpacked Size

    28.6 kB

    Total Files

    21

    Last publish

    Collaborators

    • wilsonle