I have working nextjs app using prisma as ORM. I have this page: /somecats which connect to nextjs backend (/pages/api/categories). When I execute http://localhost:3000/api/requests), it not require authorize. I want this endpoint should be authorized. How can I do this in NextJS especially with next-connect?
I’ve tried this tutorial but it’s not working, still not authorized.
This is /pages/api/categories/index.js now and still not authorized:
import { getCategories, postCategory } from '../../../database/controller';
import { verify } from 'jsonwebtoken';
const handler = nc({
onError: (err, req, res, next) => {
console.error(err.stack);
return res.status(500).end(err);
},
onNoMatch: (req, res, next) => {
return res.status(404).end("Page is not found");
},
}).use((req, res, next) => {
req.userId = null;
req.username = null;
const { authorization } = req.headers;
if (!authorization) {
next();
} else {
verify(authorization, 'khidir-secret', (error, decoded) => {
if (!error && decoded) {
req.userId = decoded.userId;
req.username = decoded.name;
}
next();
});
}
})
.get(async (req, res) => getCategories(req, res))
.post(async (req, res) => postCategory(req, res))
export default handler;
And also did this too:
//this is /lib/handler.ts
import { verify } from 'jsonwebtoken';
import { NextApiRequest, NextApiResponse } from 'next';
import nextConnect from 'next-connect';
export interface NextApiRequestExtended extends NextApiRequest {
userId: number | null;
username: string | null;
}
export default function getHandler() {
return nextConnect<NextApiRequestExtended, NextApiResponse>({
onError(error, req, res) {
res.status(501).json({ error: `Sorry something Happened! ${error.message}` });
},
onNoMatch(req, res) {
res.status(405).json({ error: `Method ${req.method} Not Allowed` });
},
}).use((req, res, next) => {
req.userId = null;
req.username = null;
const { authorization } = req.headers;
if (!authorization) {
next();
} else {
verify(authorization, 'khidir-secret', (error: any, decoded: any) => {
if (!error && decoded) {
req.userId = decoded.userId;
req.username = decoded.name;
}
next();
});
}
})};
And call that handler with this:
// this is version2 of /pages/api/categories.js
import { getCategories, postCategory } from '../../../database/controller';
import getHandler from '../../../lib/handler';
export default getHandler()
.get(async (req, res) => getCategories(req, res))
.post(async (req, res) => postCategory(req, res));
Those codes are both working, BUT with NO Authorization. What should I do?