_server.loginUser()

Availability:

SmartFoxServer PRO 1.2.1

Usage:

_server.loginUser(nick, pass, channel)

Description:

Logs a user in, creating a User object (it.gotoandplay.smartfoxserver.data.User) for the client.
Each client connected with SmartFoxServer has its own Chanel object representing the socket connection. After successfully logging in, the client becomes a "real" user and its User object is created.

This method is usually used when you receive an event of type "loginRequest"

Properties:

nick   the user nickname
pass   the user password
channel   the Channel object

Returns:

An object with two properties:

success   True, if login was successfull
error   The error message of the server if login failed

Example:

/*
* This method handles internal events
* Internal events are dispactched by the Zone or Room where the extension is attached to
* 
* the (evt) object
*/
function handleInternalEvent(evt)
{	
	if (evt.name == "loginRequest")
	{
		var error = ""
		
		var nick = evt["nick"]
		var pass = evt["pass"]
		var chan = evt["chan"]
		
		if (pass != "The Password")
		{
			error = "Authentication failed"
		}
		else
		{
			var obj = _server.loginUser(nick, pass, chan)
			
			if (obj.success == false)
				error = obj.error
			else
			{
				var u = _server.instance.getUserByChannel(chan)
				trace("User " + u.getName() + " logged in")
			}
		}
		
		// Send response to client
		var response = new Object()
		
		if (error == "")
		{
			response._cmd = "logOK"
		}
		else
		{
			response._cmd = "logKO"
			response.err = error
		}
		
		/*
		* NOTE:
		* usually the 4th parameter of the sendResponse is a list of users object that will
		* receive this message.
		* 
		* Only when handling a login request you can pass a channel instead of a list of Users
		*/
		
		_server.sendResponse(response, -1, null, chan)

	}
}

In this case the user password is hardcoded in the Actionscript which is usually the worst way to handle this. We've used it in this example for the sake of simplicity. You may usually want to test the client's credentials against a database before logging the user in.

See also: