Skip to content

Commit

Permalink
chore(sentry): active les sessions utilisateurs dans l'app graphql
Browse files Browse the repository at this point in the history
  • Loading branch information
thom4parisot committed Nov 21, 2024
1 parent e64fbcd commit 8e8e2d6
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion graphql/helpers/token.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const jwt = require('jsonwebtoken')
const User = require('../models/user')
const Sentry = require('@sentry/node')

module.exports.createJWTToken = async function createJWTToken ({ email, jwtSecret}) {
const user = await User.findOne({ email })
Expand Down Expand Up @@ -28,14 +29,18 @@ module.exports.populateUserFromJWT = function populateUserFromJWT ({ jwtSecret }
try {
req.token = jwt.verify(jwtToken, jwtSecret)
} catch (error) {
Sentry.setUser(null)

res.status(400)
return res.json(error)
}

// 2. Fetch associated user, only if not populated by Passport Session before
if (req.token._id && !req.user) {
req.user = await User.findById(req.token._id)
// question: should we throw an error is the user does not exist?
req.user = await User.findById(req.token._id)

Sentry.setUser(req.user ? { id: req.user?._id } : null)
}

return next()
Expand Down

0 comments on commit 8e8e2d6

Please sign in to comment.