From 6fe0faad2abfed326cc1f86187c18aec12380d94 Mon Sep 17 00:00:00 2001 From: niftylettuce Date: Mon, 2 Dec 2019 16:02:53 -0600 Subject: [PATCH] fix: fixed per bug found with parsing user object --- src/index.js | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index b61459d..73a24be 100644 --- a/src/index.js +++ b/src/index.js @@ -365,8 +365,27 @@ const parseRequest = (config = {}) => { // default to the user object let user = {}; - if (ctx && isObject(ctx.state.user)) user = clone(ctx.state.user); - else if (req && isObject(req.user)) user = clone(req.user); + let parsedUser; + if (ctx && isObject(ctx.state.user)) parsedUser = ctx.state.user; + else if (req && isObject(req.user)) parsedUser = req.user; + + if (parsedUser) { + try { + user = + typeof parsedUser.toJSON === 'function' + ? parsedUser.toJSON() + : typeof parsedUser.toObject === 'function' + ? parsedUser.toObject() + : clone(parsedUser); + } catch (err) { + debug(err); + try { + user = JSON.parse(safeStringify(parsedUser)); + } catch (err) { + debug(err); + } + } + } const ip = ctx ? ctx.ip : req ? req.ip : null;