34

How would I find the username that the computer owner is using currently (while logged in), using NodeJS?

I have searched around a bit, but haven't found anything...

76

I am not sure why, but someone added an answer and then deleted it quickly after... I was fast enough to catch it though, and after checking, it is the shortest and most effective way of doing what I asked before:

require("os").userInfo().username

The only problem is, in Windows 10, it returns the first name of the owner account that has been used (just a heads up). Everything else works completely fine!

| improve this answer | |
  • 1
    I did but only because it only is available in v7 of nodejs. its a new feature that electron probably cant use. if it works then good! – corn3lius Nov 4 '16 at 14:00
  • It's not correct, node is executed on server side, so it will give you info about your server, not about the client. – Kevin Grosgojat Nov 4 '16 at 14:02
  • 1
    where does he mention he needs a clients log-in? node.js is server side code? – corn3lius Nov 4 '16 at 14:04
  • "How would I find the username that the computer owner is using currently" – Kevin Grosgojat Nov 4 '16 at 14:07
  • 1
    Ok, I understand, didn't seen the electron tag. We are not in web context, but in an heavy client context. That's ok. – Kevin Grosgojat Nov 4 '16 at 14:13
13

This one object you will get username:

let os = require('os')
console.log(os.userInfo());
| improve this answer | |
  • 3
    It will give you the hostname of server, not client. – Kevin Grosgojat Nov 4 '16 at 13:52
  • 1
    Actually you should use const os = require('os'); as os is an inmutable variable. – Griffin Jun 18 '19 at 20:59
8

process.env.USER Should work also at least in MAC or Linux. e.g.

let user = process.env.USER || ""
| improve this answer | |
3

Definitely, the easiest way to do it is using username

Install:

$ npm install username

Then:

const username = require('username');

(async () => {
    console.log(await username());
    //=> 'current_username'
})();
| improve this answer | |
  • Hai @carlos, I have implemented the same and tried to add this code in one GET rest api. I am trying to attach the response of username() function to response.send() method but its not returning. Please find the code i am trying below: var user = (async () => { await username(); })(); res.status(200).send({ success: 'true', message: user }); – Kishore Konangi Nov 6 '18 at 5:10
  • Please ingnore my above comment, it got resolved . Hai @carlos, When i tried to move the same code to linux server and using GET rest api i am hitting from client machine. I am getting the linux server user but not the client user who is hitting the url. The same worked in my windows machine as i am running the application locally. Any help regarding this.!? – Kishore Konangi Nov 6 '18 at 5:40
  • @kishoreaoe this module will basically return to you the logged in user(in this case the server user), you will need to work around that to find out how does this module can help you get the client user, probably not because it will get you the root or logged in username. – abranhe Nov 8 '18 at 5:47
-1

If it doesn't need to be cross operating systems (just *nix based), one way you could do (keep in mind that exec could be potentially risky to use if you parameterize it):

const Promise = require( 'bluebird' ),
      exec    = Promise.promisify( require( 'child_process' ).exec );

exec( 'id -un' ).then( ( username )=> {
 // do something about it
});

If you want to use bluebird for promises, don't forget about: npm install bluebird --save

| improve this answer | |
-7

If you want to get information about the client witch call a route on your server, you have to parse client useragent.

https://msdn.microsoft.com/en-us/library/ms537503(v=vs.85).aspx

You can get client user agent with node using those examples :

How to handle user-agent in nodejs environment?

| improve this answer | |
  • 4
    This is completely unrelated to what the OP is asking. – Harold Sep 15 '17 at 19:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.