Joueurs et connexion
Des comptes invités qui survivent aux redémarrages, des noms affichés et la liaison des invités à un e-mail ou à Google.
Les commentaires du code et les noms d'API restent en anglais pour correspondre aux SDK.
Connexion invité#
Appelez cette méthode une fois au démarrage. Elle restaure la session enregistrée (en la rafraîchissant si besoin) ou crée un nouvel invité : le même joueur conserve ainsi sa progression.
var signIn = await Majacloud.Auth.SignInAnonymouslyAsync();
if (signIn.Ok)
{
var player = signIn.Value;
Debug.Log(player.PlayerId + " " + player.DisplayName + " guest: " + player.IsGuest);
}var signed_in := await Majacloud.auth.sign_in_anonymously()
if signed_in.ok:
var player: Dictionary = signed_in.value
print(player.player_id, " ", player.display_name, " ", player.auth_type)const signIn = await mc.auth.signInAnonymously();
if (signIn.ok) {
const player = signIn.value;
console.log(player.player_id, player.display_name, player.auth_type);
}curl -X POST https://majacloud.majanite.com/v1/auth/guest -H "X-Project-ID: $PROJECT_ID" -H "X-Client-Key: $CLIENT_KEY" \
-H "Content-Type: application/json" -d '{"device_id": "install-7f2c"}'
# Before the access token expires (1 hour), rotate the session:
curl -X POST https://majacloud.majanite.com/v1/auth/refresh -H "X-Project-ID: $PROJECT_ID" -H "X-Client-Key: $CLIENT_KEY" \
-H "Content-Type: application/json" -d '{"refresh_token": "<refresh_token>"}'Noms affichés#
Les invités reçoivent un nom généré comme Guest 4821. Les joueurs peuvent choisir le leur ; c'est celui qu'affichent les classements.
var renamed = await Majacloud.Auth.SetDisplayNameAsync("Nova");
if (!renamed.Ok) Debug.Log(renamed.Error.Message); // e.g. Validation: too longvar renamed := await Majacloud.auth.set_display_name("Nova")
if not renamed.ok:
print(renamed.message) # e.g. validation: too longconst renamed = await mc.auth.setDisplayName('Nova');
if (!renamed.ok) console.log(renamed.error.message); // e.g. validation: too longcurl -X PATCH https://majacloud.majanite.com/v1/players/me -H "X-Project-ID: $PROJECT_ID" -H "X-Client-Key: $CLIENT_KEY" \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" -d '{"display_name": "Nova"}'Lier à un e-mail ou à Google#
La liaison ouvre une page web sécurisée sur votre serveur MAJACLOUD, où le joueur se connecte ou s'inscrit avec un e-mail et un mot de passe ou avec Google. La progression de l'invité (scores, sauvegardes, portefeuille) est transférée sur ce compte, et le jeu reçoit la nouvelle session — sur n'importe quel appareil.
// Opens the sign-in page, waits for the player, then switches the session
var linked = await Majacloud.Auth.LinkAccountAsync(new MajacloudLinkOptions { ReturnUrl = "mygame://auth" });
if (linked.Ok) Debug.Log("Signed in as " + linked.Value.DisplayName);
// WebGL: browsers only open tabs from a click, so start first and open inside the handler
var flow = await Majacloud.Auth.StartLinkAsync(new MajacloudLinkOptions { OpenBrowser = false });
linkButton.onClick.AddListener(() => flow.Value.OpenInBrowser());
var result = await flow.Value.WaitForCompletionAsync();# Opens the sign-in page, waits for the player, then switches the session
var linked := await Majacloud.auth.link_account("mygame://auth")
if linked.ok:
print("Signed in as ", linked.value.display_name)
# Or step by step (e.g. to open the page from a button)
var started := await Majacloud.auth.start_link()
var flow: MajacloudLinkFlow = started.value
flow.open_in_browser()
var result := await flow.wait_for_completion()// Call it from a click handler: the browser only allows the new tab after a user gesture
document.querySelector('#link').addEventListener('click', async () => {
const linked = await mc.auth.linkAccount();
if (linked.ok) console.log('Signed in as ' + linked.value.display_name);
});# 1. Start (send the guest's Bearer token so its progress is merged)
curl -X POST https://majacloud.majanite.com/v1/auth/link/start -H "X-Project-ID: $PROJECT_ID" -H "X-Client-Key: $CLIENT_KEY" \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" -d '{"code_challenge": "<base64url(sha256(verifier))>", "code_challenge_method": "S256"}'
# 2. Open auth_url for the player, then poll until status is "completed"
curl "https://majacloud.majanite.com/v1/auth/link/status?flow_id=<flow_id>&flow_token=<flow_token>" -H "X-Project-ID: $PROJECT_ID" -H "X-Client-Key: $CLIENT_KEY"
# 3. Exchange for the new session
curl -X POST https://majacloud.majanite.com/v1/auth/link/exchange -H "X-Project-ID: $PROJECT_ID" -H "X-Client-Key: $CLIENT_KEY" \
-H "Content-Type: application/json" -d '{"flow_id": "<flow_id>", "exchange_code": "<exchange_code>", "code_verifier": "<verifier>"}'Fonctionnement de la liaison#
- Le SDK crée un secret à usage unique (PKCE) et démarre un flux de connexion.
- Le joueur termine sur la page web ; le SDK interroge le serveur jusqu'à ce que le flux soit terminé.
- Le SDK échange le flux contre une session à l'aide du secret : un lien volé est donc inutilisable.
- Les flux expirent au bout de 15 minutes (délai prolongé tant que la page est ouverte).
Retour au jeu#
Sans URL de retour, la page invite les joueurs à revenir au jeu. Pour les y renvoyer automatiquement, ajoutez votre schéma d'URL personnalisé (par exemple mygame) dans Clés API ▸ URL de retour après connexion du joueur et passez mygame://auth comme URL de retour.
Événements#
Réagissez quand le joueur change, se déconnecte ou quand la session ne peut plus être rafraîchie :
Majacloud.Auth.PlayerChanged += player => nameLabel.text = player?.DisplayName;
Majacloud.Auth.SignedOut += () => ShowTitleScreen();
Majacloud.Auth.SessionExpired += () => ShowSignIn();Majacloud.auth.player_changed.connect(func(player): $NameLabel.text = player.get("display_name", ""))
Majacloud.auth.signed_out.connect(show_title_screen)
Majacloud.auth.session_expired.connect(show_sign_in)mc.auth.on('playerChanged', (player) => render(player));
mc.auth.on('signedOut', () => showTitleScreen());
mc.auth.on('sessionExpired', () => showSignIn());Déconnexion#
La déconnexion révoque la session sur le serveur et l'oublie sur l'appareil. La connexion suivante crée un nouvel invité.
await Majacloud.Auth.SignOutAsync(); // revokes the session on the server
Majacloud.Auth.ClearLocalSession(); // or only forget it on this deviceawait Majacloud.auth.sign_out() # revokes the session on the server
Majacloud.auth.clear_local_session() # or only forget it on this deviceawait mc.auth.signOut(); // revokes the session on the server
mc.auth.clearLocalSession(); // or only forget it on this devicecurl -X POST https://majacloud.majanite.com/v1/auth/logout -H "X-Project-ID: $PROJECT_ID" -H "X-Client-Key: $CLIENT_KEY" \
-H "Content-Type: application/json" -d '{"refresh_token": "<refresh_token>"}'Lire les joueurs depuis votre serveur#
Avec la clé secrète, votre serveur peut lire n'importe quel joueur, y compris son e-mail et les soldes de son portefeuille :
// Node.js, created with secretKey
const found = await admin.server.getPlayer('player_3f9c…');
if (found.ok) console.log(found.value.player.email, found.value.player.wallets);curl https://majacloud.majanite.com/v1/players/player_3f9c… \
-H "X-Project-ID: $PROJECT_ID" -H "X-Secret-Key: $SECRET_KEY"Dans le tableau de bord#
La page Joueurs liste tous les joueurs avec leur méthode de connexion, leurs scores, leurs sauvegardes et leur portefeuille. Vous pouvez y renommer ou désactiver un joueur, le déconnecter partout, lui envoyer un lien de réinitialisation du mot de passe ou le supprimer.