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",
|
||||
"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",
|
||||
|
||||
16
package.json
16
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",
|
||||
|
||||
@@ -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" <no-reply@obc-maconnerie.com>',
|
||||
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: [],
|
||||
})
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user