I am having issues while doing some complex stuff inside middleware. I know i should be getting this done in a route or some small function but I had no choice.
This is the entire middleware:
app.use(async function addReqUser(req,res,next) {
if (req.cookies) {
let token = req.cookies['session-token'];
let user = {};
async function verify() {
//google auth function
const ticket = await client.verifyIdToken({
idToken: token,
audience: CLIENT_ID,
});
const payload = ticket.getPayload();
user.name = payload.name;
user.email = payload.email;
user.picture = payload.picture;
}
verify()
.then(async ()=>{
req.user = user;
const user = await User.find({ email: req.user.email }).select("_id").exec();
req.user.id = user[0]._id
res.locals.user = req.user //populate the user with an id
next();
})
.catch(err=>{
console.log(err)
return res.redirect('/')
})
}
next()
})
The error just says [ERR HTTP_HEADERS_SENT] Cannot set headers after they are sent back to the client
Help me with this.
Your fast response in appreciated.
If req.cookies is set and the verify() call is successful, next() will be executed twice: once after the asynchronous invocation of verify() and once more at the end of the .then function.
On the other hand, if verify() fails, the res.redirect call in the .catch function sets a header after the first execution of next().
Both can lead to the observed error. A middleware should either
next() (once!) orres.send), but not call next().You have not used async and await for verify function, you can modify you app.use to the following code
app.use(async function addReqUser(req, res, next) {
if (req.cookies) {
let token = req.cookies['session-token'];
let user;
async function verify() {
//google auth function
return client.verifyIdToken({
idToken: token,
audience: CLIENT_ID,
});
}
const response = await verify()
const payload = response.getPayload()
if (payload.email) {
user.name = payload.name;
user.email = payload.email;
user.picture = payload.picture;
const userDoc = await User.find({ email: req.user.email }).select("_id").exec();
req.user = userDoc;
req.user.id = userDoc[0]._id;
res.locals.user = req.user //populate the user with an id
return next()
} else {
return res.redirect('/')
}
}
next()
})
While using the .then on verify, the code is not waiting for verify() Promise to complete & next() after the if block is executed first
The req.cookies was being called and the next() was being executed twice. After some suggestions and coding here's the solution:
app.use(function addReqUser(req, res, next) {
if (Object.hasOwnProperty.bind(req.cookies)('session-token')) { //has a session-token
let user = {};
async function verify() {
let token = req.cookies['session-token'];
//google auth function
const ticket = await client.verifyIdToken({
idToken: token,
audience: CLIENT_ID,
});
const payload = ticket.getPayload()
if (payload) {
user.name = payload.name;
user.email = payload.email;
user.picture = payload.picture;
req.user = user;
const userDoc = await User.find({ email: req.user.email }).select("_id").exec();
req.user.id = userDoc[0]._id;
res.locals.user = req.user //populate the user with an id
}
}
verify()
.then(()=>{
next();
})
.catch(err=>{
console.log(err)
res.redirect('/')
})
} else {
next();
}
})