Skip to main content

User Session

This tutorial guides you through creating a new user session using email authentication on your Nakama server. With a user session, a logged-in user can securely interact with the server.

Prerequisites

  • Access: Ensure you have access to your Nakama server.
  • Credentials: A valid email and password. If this is a new user, setting "create": true will automatically create a new account.
  • Client/Tool: Have a tool like curl or one of the Nakama client libraries ready.

Establishing a New User Session with cURL

You can create a user session by sending a POST request to Nakama's email authentication endpoint.

  1. Prepare Your User Credentials:

    Replace [email protected] with your email and a-strong-password with your desired password.

  2. Make the Authentication Request:

    curl -X POST http://your-nakama-server-host:7350/v2/account/authenticate/email \
    -H "Content-Type: application/json" \
    -d '{"email": "[email protected]", "password": "a-strong-password", "create": true}'

    This request returns a JSON response containing a session token. Use this token for authenticated requests to the Nakama server.

Establishing a New User Session with a Client Library

If you're using one of Nakama's client libraries, here's an example using JavaScript:

// Example using the Nakama JavaScript client
const client = new Nakama.Client("defaultkey", "your-nakama-server-host", "7350");
client.authenticateEmail("[email protected]", "a-strong-password", true)
.then(session => {
console.log("User session established:", session);
})
.catch(error => {
console.error("Error establishing user session:", error);
});

Make sure to replace [email protected], a-strong-password, and your-nakama-server-host with your actual email, password, and Nakama server host.

Additional Notes

  • Session Token Usage: The session token received in the response should be included with each subsequent request to authenticate your user.
  • Session Renewal: If the session expires, you will need to re-authenticate or utilize a refresh mechanism provided by your client library.

Additional Resources