Skip to content

Commit b0ff26a

Browse files
committed
examples(multiple-auth-collections): Add example for using multiple authentication collections
1 parent 0a5e859 commit b0ff26a

File tree

33 files changed

+1514
-629
lines changed

33 files changed

+1514
-629
lines changed

examples/basic/.env.example

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
DATABASE_URI=postgres://postgres:<password>@127.0.0.1:5432/your-database-name
33
PAYLOAD_SECRET=YOUR_SECRET_HERE
44

5-
NEXT_PUBLIC_SERVER_URL=http://localhost:3000
6-
75
# Auth.js
86
AUTH_SECRET=YOUR_SECRET_HERE
97
AUTH_GITHUB_ID=YOUR_GITHUB_ID
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Payload
2+
DATABASE_URI=postgres://postgres:<password>@127.0.0.1:5432/your-database-name
3+
PAYLOAD_SECRET=YOUR_SECRET_HERE
4+
5+
# Auth.js
6+
AUTH_SECRET=YOUR_SECRET_HERE
7+
AUTH_ADMINS_GITHUB_ID=YOUR_GITHUB_ID
8+
AUTH_ADMINS_GITHUB_SECRET=YOUR_GITHUB_SECRET
9+
AUTH_CUSTOMERS_GITHUB_ID=YOUR_GITHUB_ID
10+
AUTH_CUSTOMERS_GITHUB_SECRET=YOUR_GITHUB_SECRET
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
.yarn/install-state.gz
8+
9+
/.idea/*
10+
!/.idea/runConfigurations
11+
12+
# testing
13+
/coverage
14+
15+
# next.js
16+
/.next/
17+
/out/
18+
19+
# production
20+
/build
21+
22+
# misc
23+
.DS_Store
24+
*.pem
25+
26+
# debug
27+
npm-debug.log*
28+
yarn-debug.log*
29+
yarn-error.log*
30+
31+
# local env files
32+
.env*.local
33+
34+
# vercel
35+
.vercel
36+
37+
# typescript
38+
*.tsbuildinfo
39+
next-env.d.ts
40+
41+
.env
42+
43+
/media
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Multiple Auth Collections Example
2+
3+
This example demonstrates how to use multiple authentication collections in a single application.
4+
5+
## Overview
6+
7+
This project leverages two authentication collections:
8+
9+
- **customers**: For general users
10+
- **admins**: For administrative users
11+
12+
Each collection has its own configuration, endpoints, and auth workflows.
13+
14+
## Configuration
15+
16+
- Customers authentication configuration is located in `src/auth.customers.config.ts` and its handlers are setup in `src/auth.customers.ts`.
17+
- Admins authentication configuration is located in `src/auth.admins.config.ts` and its handlers are setup in `src/auth.admins.ts`.
18+
- In the Payload CMS configuration (`src/payload.config.ts`) we have two separate `payload-authjs` plugins, one for each collection.
19+
20+
## Setup
21+
22+
1. Rename the `.env.example` file to `.env` and update the values with your secrets
23+
2. Install dependencies: `pnpm install`
24+
3. Start the development server: `pnpm run dev`
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { FlatCompat } from "@eslint/eslintrc";
2+
import { dirname } from "path";
3+
import { fileURLToPath } from "url";
4+
5+
const __filename = fileURLToPath(import.meta.url);
6+
const __dirname = dirname(__filename);
7+
8+
const compat = new FlatCompat({
9+
baseDirectory: __dirname,
10+
});
11+
12+
const eslintConfig = [
13+
...compat.extends("next/core-web-vitals", "next/typescript"),
14+
{
15+
ignores: ["src/app/(payload)/"],
16+
},
17+
{
18+
rules: {
19+
"@typescript-eslint/ban-ts-comment": "warn",
20+
"@typescript-eslint/no-empty-object-type": "warn",
21+
"@typescript-eslint/no-explicit-any": "warn",
22+
"@typescript-eslint/no-unused-vars": [
23+
"warn",
24+
{
25+
vars: "all",
26+
args: "after-used",
27+
ignoreRestSiblings: false,
28+
argsIgnorePattern: "^_",
29+
varsIgnorePattern: "^_",
30+
destructuredArrayIgnorePattern: "^_",
31+
caughtErrorsIgnorePattern: "^(_|ignore)",
32+
},
33+
],
34+
},
35+
},
36+
];
37+
38+
export default eslintConfig;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { withPayload } from "@payloadcms/next/withPayload";
2+
3+
/** @type {import('next').NextConfig} */
4+
const nextConfig = {};
5+
6+
export default withPayload(nextConfig);
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"name": "multiple-auth-collections-example",
3+
"version": "1.0.0",
4+
"description": "An example of Payload CMS using payload-authjs plugin with multiple auth collections",
5+
"private": true,
6+
"type": "module",
7+
"scripts": {
8+
"build": "cross-env NODE_OPTIONS=--no-deprecation next build",
9+
"dev": "cross-env NODE_OPTIONS=--no-deprecation next dev",
10+
"devsafe": "rm -rf .next && cross-env NODE_OPTIONS=--no-deprecation next dev",
11+
"generate:importmap": "cross-env NODE_OPTIONS=--no-deprecation payload generate:importmap",
12+
"generate:types": "cross-env NODE_OPTIONS=--no-deprecation payload generate:types",
13+
"lint": "cross-env NODE_OPTIONS=--no-deprecation next lint",
14+
"payload": "cross-env NODE_OPTIONS=--no-deprecation payload",
15+
"start": "cross-env NODE_OPTIONS=--no-deprecation next start"
16+
},
17+
"dependencies": {
18+
"@payloadcms/db-postgres": "^3.24.0",
19+
"@payloadcms/next": "^3.24.0",
20+
"@payloadcms/richtext-lexical": "^3.24.0",
21+
"cross-env": "^7.0.3",
22+
"graphql": "^16.10.0",
23+
"next": "15.1.7",
24+
"next-auth": "5.0.0-beta.25",
25+
"payload": "^3.24.0",
26+
"payload-authjs": "workspace:*",
27+
"react": "19.0.0",
28+
"react-dom": "19.0.0",
29+
"sharp": "0.33.5"
30+
},
31+
"devDependencies": {
32+
"@eslint/eslintrc": "^3.2.0",
33+
"@next/eslint-plugin-next": "^15.1.6",
34+
"@types/node": "^22.13.5",
35+
"@types/react": "19.0.10",
36+
"@types/react-dom": "19.0.4",
37+
"eslint": "^9.20.0",
38+
"eslint-config-next": "15.1.6",
39+
"eslint-plugin-react-hooks": "^5.1.0",
40+
"typescript": "5.7.3"
41+
},
42+
"engines": {
43+
"node": "^18.20.2 || >=20.9.0"
44+
}
45+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"$schema": "../../node_modules/nx/schemas/project-schema.json",
3+
"sourceRoot": "examples/multiple-auth-collections/src",
4+
"projectType": "application",
5+
"targets": {}
6+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { signIn } from "@/auth.customers";
2+
3+
export function SignInButton() {
4+
return (
5+
<form
6+
action={async () => {
7+
"use server";
8+
await signIn();
9+
}}
10+
>
11+
<button type="submit">Sign In (Customer)</button>
12+
</form>
13+
);
14+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"use client";
2+
3+
export function SignOutButton() {
4+
return (
5+
<button
6+
type="button"
7+
onClick={async () => {
8+
await fetch(`/api/customers/logout`, {
9+
method: "POST",
10+
headers: {
11+
"Content-Type": "application/json",
12+
},
13+
});
14+
window.location.reload();
15+
}}
16+
>
17+
Sign Out (Customer)
18+
</button>
19+
);
20+
}

0 commit comments

Comments
 (0)