try fixing nextcrud
This commit is contained in:
@ -3,14 +3,20 @@ import { Prisma } from "@prisma/client";
|
|||||||
import { NextApiRequest, NextApiResponse } from "next";
|
import { NextApiRequest, NextApiResponse } from "next";
|
||||||
import { getServerSession } from "next-auth/next";
|
import { getServerSession } from "next-auth/next";
|
||||||
import { authOptions } from "../auth/[...nextauth]";
|
import { authOptions } from "../auth/[...nextauth]";
|
||||||
// import { getToken } from "next-auth/jwt";
|
import { JWT } from "next-auth/jwt";
|
||||||
// import { getSession } from "next-auth/client";
|
|
||||||
const common = require("../../../src/helpers/common");
|
const common = require("../../../src/helpers/common");
|
||||||
import jwt from 'jsonwebtoken';
|
import jwt from 'jsonwebtoken';
|
||||||
import { decode } from 'next-auth/jwt';
|
|
||||||
const logger = require('../../../src/logger');
|
const logger = require('../../../src/logger');
|
||||||
|
|
||||||
// import { getToken } from "next-auth/jwt";
|
interface SessionUser {
|
||||||
|
email?: string;
|
||||||
|
name?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface Session {
|
||||||
|
user?: SessionUser;
|
||||||
|
expires: string;
|
||||||
|
}
|
||||||
|
|
||||||
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
||||||
const prismaClient = common.getPrismaClient();
|
const prismaClient = common.getPrismaClient();
|
||||||
@ -19,24 +25,31 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
|||||||
adapter: new PrismaAdapter({ prismaClient }),
|
adapter: new PrismaAdapter({ prismaClient }),
|
||||||
models: {
|
models: {
|
||||||
[Prisma.ModelName.CartEvent]: { name: "cartevents" },
|
[Prisma.ModelName.CartEvent]: { name: "cartevents" },
|
||||||
|
[Prisma.ModelName.Publisher]: { name: "publishers" },
|
||||||
|
[Prisma.ModelName.Availability]: { name: "availabilities" },
|
||||||
|
[Prisma.ModelName.Location]: { name: "locations" },
|
||||||
|
[Prisma.ModelName.Shift]: { name: "shifts" },
|
||||||
|
[Prisma.ModelName.Assignment]: { name: "assignments" },
|
||||||
|
[Prisma.ModelName.Report]: { name: "reports" },
|
||||||
|
[Prisma.ModelName.Message]: { name: "messages" },
|
||||||
|
[Prisma.ModelName.Survey]: { name: "surveys" },
|
||||||
|
[Prisma.ModelName.EventLog]: { name: "eventlogs" },
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
//1: check session
|
//1: check session
|
||||||
const session = await getServerSession(req, res, authOptions);
|
const session = (await getServerSession(req, res, authOptions)) as Session | null;
|
||||||
//console.log("Session:", session); // Log the session
|
|
||||||
const authHeader = req.headers.authorization || '';
|
const authHeader = req.headers.authorization || '';
|
||||||
//console.log('authHeader', authHeader);
|
|
||||||
if (session) {
|
if (session && req.query.nextcrud) {
|
||||||
//get target table
|
//get target table
|
||||||
const targetTable = req.query.nextcrud[0];
|
const targetTable = req.query.nextcrud[0];
|
||||||
//get target action
|
//get target action
|
||||||
if (req.method === 'DELETE') {
|
if (req.method === 'DELETE') {
|
||||||
switch (targetTable) {
|
switch (targetTable) {
|
||||||
// case 'publishers':
|
|
||||||
// case 'availabilities':
|
|
||||||
default:
|
default:
|
||||||
const targetId = req.query.nextcrud[1];
|
const targetId = req.query.nextcrud[1];
|
||||||
logger.info('[nextCrud] ' + targetTable + ': ' + targetId + ' DELETED by ' + session.user.email);
|
logger.info('[nextCrud] ' + targetTable + ': ' + targetId + ' DELETED by ' + session.user?.email);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -50,21 +63,18 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
|||||||
//2: check jwt
|
//2: check jwt
|
||||||
const secret = process.env.NEXTAUTH_SECRET;
|
const secret = process.env.NEXTAUTH_SECRET;
|
||||||
const bearerHeader = req.headers['authorization'];
|
const bearerHeader = req.headers['authorization'];
|
||||||
if (bearerHeader) {
|
if (bearerHeader && secret) {
|
||||||
const token = bearerHeader.split(' ')[1]; // Assuming "Bearer <token>"
|
const token = bearerHeader.split(' ')[1]; // Assuming "Bearer <token>"
|
||||||
try {
|
// try {
|
||||||
const decoded = await decode({
|
// const decodedToken = await getToken({ req, secret });
|
||||||
token: token,
|
// if (decodedToken) {
|
||||||
secret: process.env.NEXTAUTH_SECRET,
|
// return nextCrudHandler(req, res);
|
||||||
});
|
// }
|
||||||
//console.log('Decoded JWT:');
|
// } catch (err) {
|
||||||
} catch (err) {
|
// console.error('[nextCrud]: Error decoding token:', err);
|
||||||
console.error('[nextCrud]: Error decoding token:', err);
|
// }
|
||||||
}
|
|
||||||
try {
|
try {
|
||||||
const verified = jwt.verify(token, secret);
|
const verified = jwt.verify(token, secret);
|
||||||
//console.log('Verified JWT:');
|
|
||||||
|
|
||||||
return nextCrudHandler(req, res);
|
return nextCrudHandler(req, res);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('[nextCrud]: Invalid token:', err);
|
console.error('[nextCrud]: Invalid token:', err);
|
||||||
@ -77,7 +87,6 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
|||||||
return nextCrudHandler(req, res);
|
return nextCrudHandler(req, res);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return res.status(401).json({ message: '[nextCrud]: Unauthorized' });
|
return res.status(401).json({ message: '[nextCrud]: Unauthorized' });
|
||||||
};
|
};
|
||||||
|
|
||||||
|
BIN
public/content/permits/12- Разрешително за Декември 24г..pdf
Normal file
BIN
public/content/permits/12- Разрешително за Декември 24г..pdf
Normal file
Binary file not shown.
Reference in New Issue
Block a user