Device Session
This tutorial guides you through the process of creating a new device session using device authentication on your Nakama server. With a device session, your device can securely interact with the server.
Prerequisites
- Access: Ensure you have access to your Nakama server.
- Unique Device ID: Use a unique identifier (e.g., UUID) for your device. If this is a new device, 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 Device Session with cURL
You can create a device session by sending a POST request to Nakama's device authentication endpoint.
-
Prepare Your Device ID:
Replace
your-unique-device-id
with your device's unique identifier. -
Make the Authentication Request:
curl -X POST http://your-nakama-server-host:7350/v2/account/authenticate/device \
-H "Content-Type: application/json" \
-d '{"id": "your-unique-device-id", "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 Device 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.authenticateDevice("your-unique-device-id", true)
.then(session => {
console.log("Device session established:", session);
})
.catch(error => {
console.error("Error establishing device session:", error);
});
Make sure to replace "your-unique-device-id"
and "your-nakama-server-host"
with your actual device identifier 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 device.
- Session Renewal: If the session expires, you will need to re-authenticate or utilize a refresh mechanism provided by your client library.