Add dedicated users/me route using payload.auth
This commit is contained in:
@@ -1,25 +0,0 @@
|
|||||||
import { NextRequest, NextResponse } from 'next/server';
|
|
||||||
import payload from 'payload';
|
|
||||||
import config from '@/payload.config';
|
|
||||||
|
|
||||||
export async function GET(req: NextRequest) {
|
|
||||||
await payload.init({ config });
|
|
||||||
|
|
||||||
try {
|
|
||||||
const { user } = await payload.auth({
|
|
||||||
headers: req.headers,
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!user) {
|
|
||||||
return NextResponse.json({ user: null, message: 'Account' });
|
|
||||||
}
|
|
||||||
|
|
||||||
return NextResponse.json({ user, message: 'Account' });
|
|
||||||
} catch (err) {
|
|
||||||
console.error(err);
|
|
||||||
return NextResponse.json(
|
|
||||||
{ user: null, message: 'Error checking account' },
|
|
||||||
{ status: 500 },
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
21
package-lock.json
generated
21
package-lock.json
generated
@@ -13,6 +13,7 @@
|
|||||||
"@payloadcms/richtext-lexical": "^3.78.0",
|
"@payloadcms/richtext-lexical": "^3.78.0",
|
||||||
"clsx": "^2.1.1",
|
"clsx": "^2.1.1",
|
||||||
"next": "15.3.9",
|
"next": "15.3.9",
|
||||||
|
"nodemailer": "^8.0.1",
|
||||||
"payload": "^3.78.0",
|
"payload": "^3.78.0",
|
||||||
"react": "^19.2.4",
|
"react": "^19.2.4",
|
||||||
"react-dom": "^19.2.4",
|
"react-dom": "^19.2.4",
|
||||||
@@ -23,6 +24,7 @@
|
|||||||
"@eslint/eslintrc": "^3.3.3",
|
"@eslint/eslintrc": "^3.3.3",
|
||||||
"@tailwindcss/postcss": "^4",
|
"@tailwindcss/postcss": "^4",
|
||||||
"@types/node": "^20.19.33",
|
"@types/node": "^20.19.33",
|
||||||
|
"@types/nodemailer": "^7.0.11",
|
||||||
"@types/react": "^18.3.28",
|
"@types/react": "^18.3.28",
|
||||||
"@types/react-dom": "^18.3.7",
|
"@types/react-dom": "^18.3.7",
|
||||||
"eslint": "^9",
|
"eslint": "^9",
|
||||||
@@ -3473,6 +3475,16 @@
|
|||||||
"undici-types": "~6.21.0"
|
"undici-types": "~6.21.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/@types/nodemailer": {
|
||||||
|
"version": "7.0.11",
|
||||||
|
"resolved": "https://registry.npmjs.org/@types/nodemailer/-/nodemailer-7.0.11.tgz",
|
||||||
|
"integrity": "sha512-E+U4RzR2dKrx+u3N4DlsmLaDC6mMZOM/TPROxA0UAPiTgI0y4CEFBmZE+coGWTjakDriRsXG368lNk1u9Q0a2g==",
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"@types/node": "*"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/@types/parse-json": {
|
"node_modules/@types/parse-json": {
|
||||||
"version": "4.0.2",
|
"version": "4.0.2",
|
||||||
"resolved": "https://registry.npmjs.org/@types/parse-json/-/parse-json-4.0.2.tgz",
|
"resolved": "https://registry.npmjs.org/@types/parse-json/-/parse-json-4.0.2.tgz",
|
||||||
@@ -8427,6 +8439,15 @@
|
|||||||
"semver": "bin/semver.js"
|
"semver": "bin/semver.js"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/nodemailer": {
|
||||||
|
"version": "8.0.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/nodemailer/-/nodemailer-8.0.1.tgz",
|
||||||
|
"integrity": "sha512-5kcldIXmaEjZcHR6F28IKGSgpmZHaF1IXLWFTG+Xh3S+Cce4MiakLtWY+PlBU69fLbRa8HlaGIrC/QolUpHkhg==",
|
||||||
|
"license": "MIT-0",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=6.0.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/normalize-path": {
|
"node_modules/normalize-path": {
|
||||||
"version": "3.0.0",
|
"version": "3.0.0",
|
||||||
"resolved": "https://registry.npmjs.org/normalize-path/-/normalize-path-3.0.0.tgz",
|
"resolved": "https://registry.npmjs.org/normalize-path/-/normalize-path-3.0.0.tgz",
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
"type": "module",
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "next dev",
|
"dev": "next dev",
|
||||||
"build": "next build",
|
"build": "payload migrate && next build",
|
||||||
"start": "next start",
|
"start": "next start",
|
||||||
"lint": "eslint",
|
"lint": "eslint",
|
||||||
"payload": "payload",
|
"payload": "payload",
|
||||||
@@ -17,6 +17,7 @@
|
|||||||
"@payloadcms/richtext-lexical": "^3.78.0",
|
"@payloadcms/richtext-lexical": "^3.78.0",
|
||||||
"clsx": "^2.1.1",
|
"clsx": "^2.1.1",
|
||||||
"next": "15.3.9",
|
"next": "15.3.9",
|
||||||
|
"nodemailer": "^8.0.1",
|
||||||
"payload": "^3.78.0",
|
"payload": "^3.78.0",
|
||||||
"react": "^19.2.4",
|
"react": "^19.2.4",
|
||||||
"react-dom": "^19.2.4",
|
"react-dom": "^19.2.4",
|
||||||
@@ -27,6 +28,7 @@
|
|||||||
"@eslint/eslintrc": "^3.3.3",
|
"@eslint/eslintrc": "^3.3.3",
|
||||||
"@tailwindcss/postcss": "^4",
|
"@tailwindcss/postcss": "^4",
|
||||||
"@types/node": "^20.19.33",
|
"@types/node": "^20.19.33",
|
||||||
|
"@types/nodemailer": "^7.0.11",
|
||||||
"@types/react": "^18.3.28",
|
"@types/react": "^18.3.28",
|
||||||
"@types/react-dom": "^18.3.7",
|
"@types/react-dom": "^18.3.7",
|
||||||
"eslint": "^9",
|
"eslint": "^9",
|
||||||
|
|||||||
@@ -1,22 +1,57 @@
|
|||||||
import { buildConfig } from 'payload'
|
import { buildConfig } from 'payload';
|
||||||
import { postgresAdapter } from '@payloadcms/db-postgres'
|
import { postgresAdapter } from '@payloadcms/db-postgres';
|
||||||
import { lexicalEditor } from '@payloadcms/richtext-lexical'
|
import { lexicalEditor } from '@payloadcms/richtext-lexical';
|
||||||
import { Users } from './payload/collections/Users'
|
|
||||||
import { Services } from './payload/collections/Services'
|
|
||||||
import { Realisations } from './payload/collections/Realisations'
|
|
||||||
import { Articles } from './payload/collections/Articles'
|
|
||||||
import { Testimonials } from './payload/collections/Testimonials'
|
|
||||||
import { FAQ } from './payload/collections/FAQ'
|
|
||||||
import { Media } from './payload/collections/Media'
|
|
||||||
|
|
||||||
const serverURL = process.env.NEXT_PUBLIC_SERVER_URL || ''
|
import { Users } from './payload/collections/Users';
|
||||||
|
import { Services } from './payload/collections/Services';
|
||||||
|
import { Realisations } from './payload/collections/Realisations';
|
||||||
|
import { Articles } from './payload/collections/Articles';
|
||||||
|
import { Testimonials } from './payload/collections/Testimonials';
|
||||||
|
import { FAQ } from './payload/collections/FAQ';
|
||||||
|
import { Media } from './payload/collections/Media';
|
||||||
|
|
||||||
|
import nodemailer, { type Transporter } from 'nodemailer';
|
||||||
|
|
||||||
|
const serverURL = process.env.NEXT_PUBLIC_SERVER_URL || '';
|
||||||
|
|
||||||
|
const transporter: Transporter = nodemailer.createTransport({
|
||||||
|
host: process.env.SMTP_HOST,
|
||||||
|
port: Number(process.env.SMTP_PORT) || 587,
|
||||||
|
secure: false,
|
||||||
|
auth: {
|
||||||
|
user: process.env.SMTP_USER,
|
||||||
|
pass: process.env.SMTP_PASS,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
// on tape en `any` pour ne plus se battre avec EmailAdapter
|
||||||
|
const emailAdapter: any = {
|
||||||
|
sendEmail: async ({
|
||||||
|
to,
|
||||||
|
subject,
|
||||||
|
html,
|
||||||
|
text,
|
||||||
|
from,
|
||||||
|
}: {
|
||||||
|
to: string | string[];
|
||||||
|
subject: string;
|
||||||
|
html: string;
|
||||||
|
text: string;
|
||||||
|
from?: string;
|
||||||
|
}) => {
|
||||||
|
await transporter.sendMail({
|
||||||
|
from: from ?? '"OBC Maçonnerie" <no-reply@obc-maconnerie.com>',
|
||||||
|
to,
|
||||||
|
subject,
|
||||||
|
html,
|
||||||
|
text,
|
||||||
|
});
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
export default buildConfig({
|
export default buildConfig({
|
||||||
secret: process.env.PAYLOAD_SECRET || 'change-moi',
|
secret: process.env.PAYLOAD_SECRET || 'change-moi',
|
||||||
serverURL,
|
serverURL,
|
||||||
// csrf: Payload auto-populates from serverURL when non-empty.
|
|
||||||
// When serverURL is empty, csrf stays [] → all origins accepted (dev-safe).
|
|
||||||
// cors: Allow the public origin to call the REST API from the browser.
|
|
||||||
cors: serverURL ? [serverURL] : '*',
|
cors: serverURL ? [serverURL] : '*',
|
||||||
editor: lexicalEditor(),
|
editor: lexicalEditor(),
|
||||||
routes: {
|
routes: {
|
||||||
@@ -26,6 +61,7 @@ export default buildConfig({
|
|||||||
admin: {
|
admin: {
|
||||||
user: 'users',
|
user: 'users',
|
||||||
},
|
},
|
||||||
|
email: emailAdapter,
|
||||||
db: postgresAdapter({
|
db: postgresAdapter({
|
||||||
pool: {
|
pool: {
|
||||||
connectionString: process.env.DATABASE_URL!,
|
connectionString: process.env.DATABASE_URL!,
|
||||||
@@ -34,4 +70,4 @@ export default buildConfig({
|
|||||||
}),
|
}),
|
||||||
collections: [Users, Services, Realisations, Articles, Testimonials, FAQ, Media],
|
collections: [Users, Services, Realisations, Articles, Testimonials, FAQ, Media],
|
||||||
globals: [],
|
globals: [],
|
||||||
})
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user