I have a nodejs/expressjs application that uses @azure/msal-node
library for user-login. For this, I followed the official Microsoft Documentation.
User login works like a charm.
But now I have to access the user's profile, and most important, the user's name. I tried many solutions, but nothing works.
For example, in server.js
, I tried this.app.getUser();
as well as this.app.getAccount();
as suggested here. But this does not work and in the Thread I linked here, someone says it only works with version "^0.2.4". Tried this, but npm could not find this version.
And as suggested in this answer, I also set family_name
and general_name
in Azure AD for my application, but I do not know how to get the information from the Token. Because in Chrome Developer Tools, there is no Token sent that contains these information.
Here is what I have so far:
// server.js
const path = require('path');
const express = require("express");
const msal = require('@azure/msal-node');
const SERVER_PORT = process.env.PORT || 3000;
const REDIRECT_URI = "http://localhost:3000";
const config = {
auth: {
clientId: "xyz",
authority: "xyz",
clientSecret: "xyz"
},
system: {
loggerOptions: {
loggerCallback(loglevel, message, containsPii) {
console.log(message);
},
piiLoggingEnabled: false,
logLevel: msal.LogLevel.Verbose,
}
}
};
// Create msal application object
const pca = new msal.ConfidentialClientApplication(config);
// Create Express App and Routes
const app = express();
app.use(express.static(path.join(__dirname, './build')));
app.get('/login', (req, res) => {
const authCodeUrlParameters = {
scopes: ["user.read"],
redirectUri: REDIRECT_URI,
};
// get url to sign user in and consent to scopes needed for application
pca.getAuthCodeUrl(authCodeUrlParameters).then((response) => {
console.log("my response: ", response);
// Here I tried pca.getUser(); and pca.getAccount();
res.redirect(response);
}).catch((error) => console.log(JSON.stringify(error)));
});
app.listen(SERVER_PORT, () => console.log(`Msal Node Auth Code Sample app listening on port ${SERVER_PORT}!`))
Since I works with express.js
and React as frontend: Should I better user msal-react? I wanted to avoid this because this is not officially supported by Microsoft.