What would be the best approach to retrieve O365/AzureAD user's details such as username who consumes an Azure Function on Node?
Only Online authenticated users are allowed to consume the Azure Function
All the examples I see are related to Azure Functions on C#.
If you're using Azure AD and node.js, the easiest way is to look at the X-MS-CLIENT-PRINCIPAL-NAME HTTP request header. Here is my test code for your reference.
module.exports = async function (context, req) {
context.log('JavaScript HTTP trigger function processed a request.');
if (req.query.name || (req.body && req.body.name)) {
context.res = {
// status: 200, /* Defaults to 200 */
body: "Hello " + (req.query.name || req.body.name)+". CurrentUser:"+req.headers['x-ms-client-principal-name']
};
}
else {
context.res = {
status: 400,
body: "Please pass a name on the query string or in the request body"
};
}
};

Reference:
Advanced usage of authentication and authorization in Azure App Service
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With