I am currently working on a Nuxt server side rendered application using the express framework for authentication with the openid-client package. My goal is to store the retrieved token in the express session, but I am facing an issue where the request model (req) is always undefined in the callback promise. I want to achieve this by assigning req.session.token = tokenSet.access_token. As a beginner in JavaScript, I believe I might be overlooking something obvious.
I have experimented with different approaches to pass variables into a JavaScript promise, but they all required defining the Promise manually, which is not applicable in my situation. I also attempted to wait for the promise and use it outside of the callback promise without success.
router.get('/api/oidc-callback', (req, res, params) => {
Issuer.discover('http://localhost:5000') // => Promise
.then(function(identityIssuer) {
const client = new identityIssuer.Client({
...
})
// REQUEST DEFINED HERE
console.log(req)
client
.callback('http://localhost:3000/api/oidc-callback', req.query, {
code_verifier
})
// => Promise
.then(function(tokenSet) {
// REQUEST BECOMES UNDEFINED HERE
console.log(req)
req.session.token = tokenSet.access_token
}, req)
.catch(error => {
console.log(error)
})
//I also tried using it outside
res.redirect('/oidc-callback')
})
})
Thank you in advance for any assistance!