User Authentication

When building a multiplayer game you often need to authenticate users on the server. SandBoxd makes this process as simple as possible.

Let's assume you've built a multiplayer game, and the server is built with NodeJS. First you will want to install the SandBoxd library by running npm install sandboxd. Next locate your server's initialization code and include the following.

var sandboxd = require("sandboxd");
sandboxd.init(GAMEID, "APIKEY");

GAMEID and APIKEY are provided to you in the Manage Game page.

You now want to log in a user who has opened your game on SandBoxd.

SandBoxd provides a number of query parameters to your game when it first loads including:

uid
A unique identifier which represents this user on SandBoxd.
sid
A secret session identifier that is uniquely generated everytime a user opens your game.

There are other query parameters, but for the purposes of user authentication these are the only ones that matter.

On the client you will want to forward these two tokens to your game server after you connect. These will tell the server who you are. The way in which you send these tokens to your server is up to you.

Once the server receives these tokens, it needs to verify that the user is who they claim to be. This is achieved by using the sandboxd.verifyUser() function as follows.

var sandboxd = require("sandboxd");

function authenticateUser (uid, sid) {
	sandboxd.verifyUser(uid, sid, function (err, data) {
		if (err == null) {
			//Authentication successful
			//Add code in here to handle a successful login
			
			//Fetch the user's details
			sandboxd.getUser(uid, function (err, data) {
				var user = data;
				console.log(user.name);	//Print the user's name
			}
		} else {
			//Authentication failed
			//Check the value of 'err' to see the details of the error
		}
	});
}

After the user is successfully authenticated, you can fetch the user's details by issuing a sandboxd.getUser() query as shown above.