Skip to content

Commit

Permalink
gaurd against missing cookie or req.user object
Browse files Browse the repository at this point in the history
  • Loading branch information
mcstover committed Aug 14, 2020
1 parent 7d2100d commit 93c9cb4
Showing 1 changed file with 24 additions and 16 deletions.
40 changes: 24 additions & 16 deletions server/vue-middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,25 +68,33 @@ module.exports = function createMiddleware({

const targetCookies = ['kvbskt', 'kvls', 'kvu', 'kv'];
let cookieMessage = 'Cookie info:';
targetCookies.forEach(cookieKey => {
if (typeof cookies[cookieKey] !== 'string') {
cookieMessage += ` missing ${cookieKey} cookie,`;
} else {
cookieMessage += ` ${cookieKey} cookie: ${cookies[cookieKey]},`;
}
});
console.info(cookieMessage);
if (typeof cookies === 'object') {
targetCookies.forEach(cookieKey => {
if (typeof cookies[cookieKey] !== 'string') {
cookieMessage += ` missing ${cookieKey} cookie,`;
} else {
cookieMessage += ` ${cookieKey} cookie: ${cookies[cookieKey]},`;
}
});
console.info(cookieMessage);
} else {
console.info('missing cookie object');
}

const targetReqUser = ['id', 'user_id', 'accessToken'];
let reqUserMessage = 'req.user info:';
targetReqUser.forEach(reqUserKey => {
if (typeof req.user[reqUserKey] !== 'string') {
reqUserMessage += ` missing req.user ${reqUserKey},`;
} else {
reqUserMessage += ` req.user ${reqUserKey}: ${req.user[reqUserKey].substring(0, 25)},`;
}
});
console.info(reqUserMessage);
if (req && req.user && typeof req.user === 'object') {
targetReqUser.forEach(reqUserKey => {
if (typeof req.user[reqUserKey] !== 'string') {
reqUserMessage += ` missing req.user ${reqUserKey},`;
} else {
reqUserMessage += ` req.user ${reqUserKey}: ${req.user[reqUserKey].substring(0, 25)},`;
}
});
console.info(reqUserMessage);
} else {
console.info('missing req.user object');
}

// set html response headers
res.setHeader('Content-Type', 'text/html');
Expand Down

0 comments on commit 93c9cb4

Please sign in to comment.