@vladanilicjs/payload-auth-plugin
v0.6.0
Published
Authentication plugin for Payload CMS with flexible collection relationships
Maintainers
Readme
payload-auth-plugin (Enhanced Fork)
This is an enhanced fork of the original payload-auth-plugin with added support for flexible collection relationships.
What's Different in This Fork?
This fork adds one significant enhancement to the original plugin:
Flexible Collection Relationships: Instead of only being able to link OAuth accounts to the admin users collection, you can now configure the plugin to link accounts to any collection in your Payload CMS.
This enables scenarios such as:
- Customer authentication instead of just admin authentication
- Different authentication flows for different user types
- More flexible application architecture
Installation
npm install @vladanilic/payload-auth-plugin
# or
yarn add @vladanilic/payload-auth-plugin
# or
pnpm add @vladanilic/payload-auth-pluginUsage
For basic usage instructions, please refer to the original documentation.
Using Flexible Collection Relationships
To link OAuth accounts to a collection other than the default users collection:
import { buildConfig } from "payload/config"
import { adminAuthPlugin } from "@vladanilic/payload-auth-plugin"
import { GoogleAuthProvider } from "@vladanilic/payload-auth-plugin/providers"
export default buildConfig({
serverURL: "http://localhost:3000",
// Your collections definition
collections: [
// Other collections...
{
slug: "customers",
fields: [
{
name: "email",
type: "email",
required: true,
unique: true,
},
// Other fields...
],
},
],
plugins: [
adminAuthPlugin({
providers: [
GoogleAuthProvider({
client_id: process.env.GOOGLE_CLIENT_ID,
client_secret: process.env.GOOGLE_CLIENT_SECRET,
}),
],
// Define a custom relation to the "customers" collection
relationConfig: {
fieldName: "customer", // Field name in accounts collection
relationTo: "customers", // Target collection
collectionField: "email", // Field to use for user matching
hasMany: false,
required: true,
label: "Customer",
},
// Allow creating new customers automatically if they don't exist
allowSignUp: true,
// Custom redirect path after login
successPath: "/admin/collections/customers",
}),
],
// Other Payload config...
})Default Behavior
If you don't specify a relationConfig, the plugin works exactly like the original, linking accounts to the admin users collection.
Status of This Fork
This fork has been submitted as a pull request to the original repository. If accepted, I recommend you switch back to the original package once the changes are published there.
For all other documentation, please refer to the official documentation.
Compatibility / Troubleshooting after Payload upgrades
This package lists payload as a peer dependency (^3.0.0), which means it can be installed with newer Payload v3 versions in your app (e.g. 3.69.0). The payload version in this repo is only used for developing the plugin.
If auth “suddenly stops working” after upgrading Payload in your project:
- Clear cookies: remove the
${cookiePrefix}-tokencookie for your domain and re-login. - Version alignment: make sure all Payload packages in your app resolve to the same version (e.g.
payload,@payloadcms/ui, DB adapters, storage plugins). - Isolate the plugin: temporarily disable
adminAuthPlugin({ enabled: false, ... }). If core Payload login starts working again, the issue is in the plugin auth flow. - Cookies in HTTPS: if you run behind HTTPS, cookies may need
Secure. The plugin setsSecureautomatically whenserverURLstarts withhttps://.
If you still get stuck, check your browser network tab for the OAuth callback response and confirm it returns a Set-Cookie header for the token cookie.
License
This project is licensed under the MIT License - see the LICENSE file for details.
