Description: I want to verify the admin details from db while role checking if the admin…
- User
Description: I want to verify the admin details from db while role checking if the admin exist or not
const jwt = require('jsonwebtoken');
const accessTokenSecret = 'userSS'; const refreshTokenSecret = 'userSS';
const verifyAccessToken = (req, res, next) => { const token = req.headers['authorization']?.split(' ')[1]; console.log(req.headers, token); if (!token) return res.status(403).json({ error: 'Your not authorized to user this service' });
jwt.verify(token, accessTokenSecret, (err, user) => { if (err) return res.status(403).json({ error: 'Your not authorized to user this service' }); req.user = user; next(); });};
const verifyRefreshToken = (req, res, next) => { const token = req.body.refreshToken; if (!token) return res.sendStatus(403);
jwt.verify(token, refreshTokenSecret, (err, user) => { if (err) return res.sendStatus(403); req.user = user; next(); });};
const checkUserRole = (role) => { return (req, res, next) => { if (!req.user || req.user.role !== role) { return res.status(403).json({ error: 'Forbidden: You do not have the right permissions' }); } next(); }; };
module.exports = { verifyAccessToken, verifyRefreshToken, checkUserRole };
Generate source code based on the description above and respond in the following JSON format ONLY: ```json { "name": "prompt_name", "schema": { "type": "string", "code": "Generated code here" } } ``` IMPORTANT: - Ensure the JSON response is error-free. - Include no additional text or explanations. Respond with the JSON object only. - Assistant
{ "name": "verifyAdminDetails", "schema": { "type": "string", "code": "const db = require('./db');\n\nconst verifyAdminDetails = async (req, res, next) => {\n if (!req.user) return res.status(403).json({\n error: 'User not authenticated'\n });\n const admin = await db.findAdminById(req.user.id);\n if (!admin) return res.status(404).json({\n error: 'Admin not found'\n });\n req.admin = admin;\n next();\n};\n\nmodule.exports = { verifyAccessToken, verifyRefreshToken, checkUserRole, verifyAdminDetails };" } }