forked from juice-shop/juice-shop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurrentUser.js
30 lines (28 loc) · 1.01 KB
/
currentUser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*
* Copyright (c) 2014-2020 Bjoern Kimminich.
* SPDX-License-Identifier: MIT
*/
const insecurity = require('../lib/insecurity')
const utils = require('../lib/utils')
const cache = require('../data/datacache')
const challenges = cache.challenges
module.exports = function retrieveLoggedInUser () {
return (req, res) => {
let user
try {
if (insecurity.verify(req.cookies.token)) {
user = insecurity.authenticatedUsers.get(req.cookies.token)
}
} catch (err) {
user = undefined
} finally {
const response = { user: { id: (user && user.data ? user.data.id : undefined), email: (user && user.data ? user.data.email : undefined), lastLoginIp: (user && user.data ? user.data.lastLoginIp : undefined), profileImage: (user && user.data ? user.data.profileImage : undefined) } }
if (req.query.callback === undefined) {
res.json(response)
} else {
utils.solveIf(challenges.emailLeakChallenge, () => { return true })
res.jsonp(response)
}
}
}
}