Hello,
As title says, I want to access current logged in user details inside my extended app router.
My goal is to insert the users email address in the req.body and pass it ahead:
var approuter = require('@sap/approuter')
require('@sap/xsenv').loadEnv()
var bodyParser = require('body-parser')
var jsonParser = bodyParser.json()
const jwtDecode = require('jwt-decode');
var convert = require('xml-js')
const axios = require('axios');
var ar = approuter()
const userApiMiddleware = require('@sap/approuter/lib/middleware/user-api-middleware'); // Tried to use
existing middleware but no luck - req.session.user returned undefined :-(
ar.beforeRequestHandler.use(userApiMiddleware)
ar.beforeRequestHandler.use(jsonParser);
ar.beforeRequestHandler.use("/save/", function myFunc(req, res, next) {
if(req.method === "POST") {
console.log("Current user - "+req.passport) // returns undefined
console.log("And current user - "+req.user) // returns undefined
console.log("Session --> "+JSON.stringify(req.session)) // returns no user data
decodedToken = jwtDecode(req.session.user.token.accessToken) // does not work
console.log("decodedToken = "+decodedToken) // does not work
let config = {
method: 'get',
maxBodyLength: Infinity,
url: 'http://localhost:8080/user-api/currentUser',
headers: req.headers,
};
console.log("Starting axios...")
axios.request(config).then((response) => {
console.log(JSON.stringify(response.data));
}).catch((error) => {
console.log(error);
});
console.log("Axios ended....")
console.log("req.body is below:")
req.body['UserID'] = "SAPUser1@demo.com" // <-- That's where the user's email has to get passed
}
next()
});<br>
I had 2 ideas:
1. Use the existing user-api-middleware from

If once would see, this is the middleware behind path /user-api/currentUser.
2. Try to call http://localhost:8080/user-api/currentUser but it returns the html code of the logic page where we select the IdP even after passing the user's headers to it as is.
Need help, thanks