Closed Adityapanther closed 3 years ago
Can you create a reproducible example?
server.js
const fastify = require('fastify')({logger: true});
// importing custom routes
// const authRoute = require('./routers/authentication');
const questionRoute = require('./routers/question');
// importing decorator
const jwtVerify = require('./decorate/jwtAuthVerify');
// importing schema
const dotEnvSchema = require('./fastify-schema/dotEnvSchema');
// dot env option written
const dotenvOpts = {
schema: dotEnvSchema,
dotenv: {
path: `${__dirname}/../.env`,
// debug: true
}
}
async function launch(){
// registering fastify plugin
await fastify.register(require('fastify-env'), dotenvOpts);
await fastify.register(require('fastify-jwt'), {
secret: fastify.config.JWT
});
await fastify.register(require('fastify-formbody'));
await fastify.register(require('./database/mongoDbConnector'));
// registering custom decorator
await fastify.register(jwtVerify);
// registering custom user routes
//await fastify.register(authRoute, {
// prefix: "/user"
// });
await fastify.register(questionRoute, {
prefix: "/user"
});
fastify.listen(fastify.config.ENV === "development" ? '3000' : fastify.config.PORT, (err, addr) => {
if(err){
fastify.log.error(err);
process.exit(1);
}
fastify.log.info(`server listenning on ${addr}`)
})
}
launch()
jwtAuthVerify.js
const fastifyPlugin = require('fastify-plugin');
async function jwtAuthVerify(fastify, opts){
fastify.decorate('jwtVerify', async(req, rep, next)=>{
const accessToken = req.headers['x-auth-access-token'];
try{
const decoded = fastify.jwt.verify(accessToken);
if(!decoded) return {status: "error", msg: "access token is invalid or not verified"};
console.log(decoded);
next();
}catch(err){
if(err){
rep.send({status: "error", msg: err.message})
}
}
})
}
module.exports = fastifyPlugin(jwtAuthVerify);
question.js routes
async function question(fastify, opts){
const userQuestionDb = fastify.mongo.db.collection('userQuestions');
fastify.post('/question', {preValidation: [fastify.jwtVerify]}, async(req, rep)=>{
const {postType, postTitle, postContent} = req.body;
const xAuthAccessToken = await req.headers['x-auth-access-token'];
const decoded = await fastify.jwt.decode(xAuthAccessToken);
const {_uId, role} = decoded;
switch(postType){
case '[text]':
const postData = {
_uId,
postTitle,
postDate: Date(),
postUpdateDate: null,
postContent: null,
postType,
}
const questionAdded = await userQuestionDb.insertOne(postData);
if(!questionAdded) return {status: "error", msg: "post does not added"};
const {insertedId} = questionAdded;
rep.send({insertedId});
break;
case '[text,images]':
break;
default:
console.log("defaults");
break;
}
})
fastify.get('/questions', async(req, rep)=>{
})
}
module.exports = question;
You are mixing callbacks with async in jwtVerify. Remove next()
and you'd be good.
Prefer returning an object to using reply.send(). It's simpler and less error prone. Moroever, always return an object in your handler.
@mcollina as your response we don't have to use next()
callback in preValidation like express middlewere
You either use promises or callbacks.
ok thanks @mcollina
ok thanks @mcollina
I am getting error that reply already sent
error written below
{"level":40,"time":1620399967136,"pid":2596,"hostname":"HP","reqId":"req-1","err":{"type":"FastifyError","message":"Reply was already sent.","stack":"FastifyError: Reply was already sent.\n at _Reply.Reply.send (C:\\Users\\Aditya_kumar\\Desktop\\Project Live\\internet\\sarkarirace.com\\backend\\node_modules\\fastify\\lib\\reply.js:118:26)\n at Object.<anonymous> (C:\\Users\\Aditya_kumar\\Desktop\\Project Live\\internet\\sarkarirace.com\\backend\\app\\routers\\question.js:27:21)\n at processTicksAndRejections (internal/process/task_queues.js:93:5)","name":"FastifyError","code":"FST_ERR_REP_ALREADY_SENT","statusCode":500},"msg":"Reply already sent"}