In a node.js cli app running on a windows machine, you can get the current local user using:
// method 1
const os = require('os')
console.log(os.userInfo())
// method 2
console.log(`${process.env.USERDOMAIN}\\${process.env.USERNAME}`)
Which outputs:
# METHOD 1
{
uid: -1,
gid: -1,
username: 'adam',
homedir: 'C:\\Users\\adam',
shell: null
}
# METHOD 2
MYPC\adam
However, in modern Windows OS (e.g. Windows 10, 11), many users opt to sign-in with a Microsoft account. Apparently, this means there are 2 mirrored accounts on the machine:
{USERDOMAIN}\{USERNAME}whereUSERDOMAINcould be the local machine name or the enterprise network domain nameMicrosoftAccount\{EmailAddress}whereEmailAddressis the Microsoft account username
How can I retrieve the 2nd variant of the username in node.js?