Avg. Rating 4.0

Problem

In one of my AIR based chat applications, which is used internally, I need to get the user name of the user currently logged in to the application.

Solution

In the application, our requirement is to make a chat application to be used internally. Since each user requires a unique login name, we'll use the company user name for each individual as those are unique for each internal user.

Detailed explanation

In the application, we use the File class to get the user's information, using this class we manipulate the userDirectory nativepath; using that object  it's easy to get the user name out of the system.

//////////////////////////////////////////// Application /////////////////////////////////////////////////////////////////////

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
creationComplete="onCreationComplete(event)">

<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.events.FlexEvent;

private function onCreationComplete(event:FlexEvent):void
{
Alert.show( getSystemUser() );
}

private function getSystemUser():String
{
                var userDir:String = File.userDirectory.nativePath;
                var userName:String = userDir.substr(userDir.lastIndexOf(File.separator) + 1);
                return userName;
}
]]>
</mx:Script>

</mx:WindowedApplication>
//////////////////////////////////////////// Application /////////////////////////////////////////////////////////////////////
 
This helps us reduce the code of the unique user sign up module, and it's easily done.

+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes