From e69fa0fff2ffaf4710bd41d1e9912c4fb479be10 Mon Sep 17 00:00:00 2001 From: Enguerrand Ozano Date: Sun, 1 Mar 2026 12:11:21 +0100 Subject: [PATCH] Add dedicated users/me route using payload.auth --- app/(payload)/gestion59/api/users/me/route.ts | 25 ------- package-lock.json | 21 ++++++ package.json | 16 +++-- payload.config.ts | 66 ++++++++++++++----- 4 files changed, 81 insertions(+), 47 deletions(-) delete mode 100644 app/(payload)/gestion59/api/users/me/route.ts diff --git a/app/(payload)/gestion59/api/users/me/route.ts b/app/(payload)/gestion59/api/users/me/route.ts deleted file mode 100644 index 018bda2..0000000 --- a/app/(payload)/gestion59/api/users/me/route.ts +++ /dev/null @@ -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 }, - ); - } -} diff --git a/package-lock.json b/package-lock.json index db4d702..76214eb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,6 +13,7 @@ "@payloadcms/richtext-lexical": "^3.78.0", "clsx": "^2.1.1", "next": "15.3.9", + "nodemailer": "^8.0.1", "payload": "^3.78.0", "react": "^19.2.4", "react-dom": "^19.2.4", @@ -23,6 +24,7 @@ "@eslint/eslintrc": "^3.3.3", "@tailwindcss/postcss": "^4", "@types/node": "^20.19.33", + "@types/nodemailer": "^7.0.11", "@types/react": "^18.3.28", "@types/react-dom": "^18.3.7", "eslint": "^9", @@ -3473,6 +3475,16 @@ "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": { "version": "4.0.2", "resolved": "https://registry.npmjs.org/@types/parse-json/-/parse-json-4.0.2.tgz", @@ -8427,6 +8439,15 @@ "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": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/normalize-path/-/normalize-path-3.0.0.tgz", diff --git a/package.json b/package.json index 3f10239..7615613 100644 --- a/package.json +++ b/package.json @@ -4,19 +4,20 @@ "private": true, "type": "module", "scripts": { - "dev": "next dev", - "build": "next build", - "start": "next start", - "lint": "eslint", - "payload": "payload", - "payload:migrate": "payload migrate" -}, + "dev": "next dev", + "build": "payload migrate && next build", + "start": "next start", + "lint": "eslint", + "payload": "payload", + "payload:migrate": "payload migrate" + }, "dependencies": { "@payloadcms/db-postgres": "^3.78.0", "@payloadcms/next": "^3.78.0", "@payloadcms/richtext-lexical": "^3.78.0", "clsx": "^2.1.1", "next": "15.3.9", + "nodemailer": "^8.0.1", "payload": "^3.78.0", "react": "^19.2.4", "react-dom": "^19.2.4", @@ -27,6 +28,7 @@ "@eslint/eslintrc": "^3.3.3", "@tailwindcss/postcss": "^4", "@types/node": "^20.19.33", + "@types/nodemailer": "^7.0.11", "@types/react": "^18.3.28", "@types/react-dom": "^18.3.7", "eslint": "^9", diff --git a/payload.config.ts b/payload.config.ts index 54e91ea..ac520a8 100644 --- a/payload.config.ts +++ b/payload.config.ts @@ -1,22 +1,57 @@ -import { buildConfig } from 'payload' -import { postgresAdapter } from '@payloadcms/db-postgres' -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' +import { buildConfig } from 'payload'; +import { postgresAdapter } from '@payloadcms/db-postgres'; +import { lexicalEditor } from '@payloadcms/richtext-lexical'; -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" ', + to, + subject, + html, + text, + }); + }, +}; export default buildConfig({ secret: process.env.PAYLOAD_SECRET || 'change-moi', 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] : '*', editor: lexicalEditor(), routes: { @@ -26,6 +61,7 @@ export default buildConfig({ admin: { user: 'users', }, + email: emailAdapter, db: postgresAdapter({ pool: { connectionString: process.env.DATABASE_URL!, @@ -34,4 +70,4 @@ export default buildConfig({ }), collections: [Users, Services, Realisations, Articles, Testimonials, FAQ, Media], globals: [], -}) +});