Account Management

Account Management

uLobby's account management features provide support for creating persistent user accounts stored in a database, logging in and out of them and performing different queries. It is implemented in the AccountManager class.

Registering Accounts

New accounts are registered lobby-side with the AccountManager.Master.RegisterAccount() method:
Request<Account> RegisterAccount(string name, byte[] passwordHash, byte[] data)
This is called lobby-side to register an account with the specified name and password hash. If the name is free, the account is registered and stored in the database.
The optional data argument allows storing arbitrary data with the account. This data will always be attached to the account instance when it is stored or sent over the network, so you should try to keep it small in order to minimise network and database traffic. It is best used for storing a small amount of static account information that is often needed when handling the account. If you need to store dynamic or larger amounts of information about accounts it is best to store them separately in the database.
If the request succeeds, the registered account is returned as its result and the OnAccountRegistered event is raised. If it was not successful, the request will contain the exception that occurred.
Clients can register accounts using the AccountManager.RegisterAccount() method:
void RegisterAccount(string name, string password)
This sends a request to the lobby to register an account with the specified name and password. The password is salted and hashed before sending it - plaintext passwords are never sent or stored anywhere in uLobby.
If the request succeeds, the OnAccountRegistered event will be raised in the peer who made it, as well as the lobby. If the name was already registered, the OnRegisterFailed event will be raised, containing the requested account name and an instance of the AccountError enum describing what went wrong.

Logging In and Out

The AccountManager.Master.LogIn() method is used for logging in to a previously registered account.
Request<Account> LogIn(LobbyPeer peer, string name, byte[] passwordHash)

Request<Account> LogIn(LobbyPeer peer, string name, string password)
This logs in the specified peer to the account with the given name and password. The password can be specified either as plaintext, in which case it will be salted and hashed internally, or as an already hashed byte array.
If the login was successful, the OnAccountLoggedIn event will be raised both in the lobby and the peer, containing the logged in account. The request object will also contain the logged in account. If anything went wrong, the request will contain an exception.
Clients can log in to accounts using the AccountManager.LogIn() method:
void LogIn(string name, string password)
The request is sent to the lobby, which logs in to the account with the specified name and password. If the login was successful, the effects will be the same as for AccountManager.Master.LogIn(). If anything went wrong, the OnLogInFailed event will be raised, containing the name of the account requested to log in to, and the error that occurred.
The AccountManager.Master.LogOut() method logs out a peer from its currently logged in account.
Request LogOut(LobbyPeer peer)
After logging out the account, the OnAccountLoggedOut event will be raised in the lobby and the peer that was logged out.
Clients can use the AccountManager.LogOut() method to log out:
void LogOut()
This requests the lobby to log out the client from its currently logged in account, and otherwise has the same effects as calling AccountManager.Master.LogOut().

Account IDs

An account is identified by its ID, which is generated automatically by uLobby upon registering the account. The Account.id property returns an AccountID instance containing this ID, and this can be used for referring to the account. Many of uLobby's methods that handle accounts take instances of the IAccount interface as parameters. This interface is implemented by both AccountID and Account, so you can pass either of them to such methods.

Query Methods

There are several methods available for querying the account manager for various information. This API is only available lobby-side and is accessed from the AccountManager.Master class.
  • bool IsLoggedIn(IAccount account)
    - Checks if an account is logged in.
  • bool IsLoggedIn(LobbyPeer peer)
    - Checks if a peer is logged in.
  • Account GetLoggedInAccount(LobbyPeer peer)
    - Returns the logged in account of a peer, or null if it is not logged in.
  • IEnumerable<Account> GetLoggedInAccounts()
    - Gets a list of all currently logged in accounts.
  • LobbyPeer GetLoggedInPeer(IAccount account)
    - Gets the peer of a currently logged in account, or null if it is not logged in.
  • Request<bool> AccountExists(IAccount account)
    - Checks whether an account exists in the database. This can be used for validating accounts.
  • Request<Account> GetAccount(AccountID accountID)
    - Gets an account by its ID. The request result is null if no account with that ID exists.
  • Request<Account> GetAccount(string name)
    - Gets an account by its name. The request result is null if no account with that name exists.