Authentication
The server has built-in authentication so clients can only send requests and connect if they have the server key. The default server key is defaultkey
but it is very important to set a unique value. This value should be embedded within client code.
Additionally, the server directly embed Universal Account (UA) system. When authentication is successful a client can create a session as a user. With UA, User account can directly submit onchain transaction to L1 in a seamless way.
The UA system aims to support console authentication for all major platforms, including Nintendo Switch, Xbox One and Series X|S, and PlayStation 4/5.
When authentication is successful a client can create a session as a user.
Every user account is created from one of the options used to authenticate. We call each of these options a “link” because it’s a way to access the user’s account. You can add more than one link to each account which is useful to enable users to login in multiple ways across different devices.
Authenticate
Before you interact with the server, you must obtain a session token by authenticating with the system. The authentication system is very flexible. You could register a user with an email address, link their Facebook account, and use it to login from another device.
By default the system will create a user automatically if the identifier used to authenticate did not previously exist in the system. This pattern is shown in the device section.
For full examples on the best way to handle registering and login in each of the clients have a look at their guides.
Email
UA integration is coming soon for email.
Users can be registered with an email and password. The password is hashed before it’s stored in the database server and cannot be read or “recovered” by administrators. This protects a user’s privacy.
You can choose a custom username when creating the account. To do this, set username
to a custom name. If you want to only authenticate without implicitly creating a user account, set create
to false.
An email address must be valid as defined by RFC-5322 and passwords must be at least 8 characters.
Social providers
The server supports a lot of different social services with register and login. With each provider the user account will be fetched from the social service and used to setup the user. In some cases a user’s friends will also be fetched and added to their friends list.
To register or login as a user with any of the providers an OAuth, OTP or access token must be obtained from that social service.
UA is currently only supported for Telegram.
Telegram
Telegram login follows the OTP flow. To receive OTP, user need to start a chat with a Telegram bot (https://t.me/layerg_bot).
To achieve embedded telegram user, append this code in the <head>
tag of your index.html
file
<script src="https://telegram.org/js/telegram-web-app.js?56"></script>
createUnityInstance(document.querySelector('#unity-canvas'), {
dataUrl: 'build/build.data.unityweb',
frameworkUrl: 'build/build.framework.js.unityweb',
codeUrl: 'build/build.wasm.unityweb',
streamingAssetsUrl: 'https://streamingasset.example.com',
companyName: 'LayerG',
productName: 'Zombie Clash',
productVersion: '0.1.0'
matchWebGLToCanvasSize: false,
devicePixelRatio: 1,
}).then((instance) => {
console.log('Unity instance', instance)
window.unityInstance = instance
if (window.Telegram && Telegram.WebApp) {
console.log('Telegram user', Telegram.WebApp.initDataUnsafe?.user)
const user = Telegram.WebApp.initDataUnsafe?.user
if (user) {
sendTelegramUserIdToUnity(JSON.stringify(user))
} else {
console.error('Telegram User ID not available')
}
}
function sendTelegramUserIdToUnity(user) {
if (window.unityInstance) {
window.unityInstance.SendMessage('LayerG_Client', 'ReceiveTelegramUser', user)
} else {
console.error('Unity is not ready!')
}
}
// }
}).catch(err => {
console.error('Error creating unity instance', err)
})
</script>
And in your client, write a handler function to receive the telegram user emitted by Telegram Browser
public void ReceiveTelegramUser(string json) {
Debug.Log($"Receive telegram user from html:{json}");
telegramUser = JsonConvert.DeserializeObject<TelegramUser>(json);
client.SendTelegramOTPAsync(telegramUser.id);
}
Next, user must get the OTP from the bot
And authenticate to get the session information
Session
When an authentication call succeeds, the server responds with a session object. The session object contains at least the following:
- The user ID
- The username
- A set of variables cached in the token
- The creation time
- The expiration time
Once the client obtains the session object, you can utilize LayerG’s real-time features such as multiplayer, notifications and status updates, passing stream data or real-time chat.