ผู้เล่นและการเข้าสู่ระบบ
บัญชีเกสต์ที่คงอยู่แม้รีสตาร์ตเกม ชื่อที่แสดง และการเชื่อมบัญชีเกสต์กับอีเมลหรือ Google
คอมเมนต์ในโค้ดและชื่อ API ยังคงเป็นภาษาอังกฤษเพื่อให้ตรงกับ SDK
เข้าสู่ระบบแบบเกสต์#
เรียกครั้งเดียวตอนเริ่มเกม ฟังก์ชันนี้จะกู้คืนเซสชันที่เก็บไว้ (และรีเฟรชเมื่อจำเป็น) หรือสร้างเกสต์ใหม่ ผู้เล่นคนเดิมจึงยังคงมีความคืบหน้าเดิม
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>"}'ชื่อที่แสดง#
เกสต์จะได้รับชื่อที่สร้างอัตโนมัติ เช่น Guest 4821 ผู้เล่นเลือกชื่อของตัวเองได้ และลีดเดอร์บอร์ดจะแสดงชื่อนั้น
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"}'เชื่อมกับอีเมลหรือ Google#
การเชื่อมบัญชีจะเปิดหน้าเว็บที่ปลอดภัยบนเซิร์ฟเวอร์ MAJACLOUD ของคุณ ซึ่งผู้เล่นใช้เข้าสู่ระบบหรือสมัครด้วยอีเมลและรหัสผ่านหรือ Google ความคืบหน้าของเกสต์ (คะแนน เซฟ กระเป๋าเงิน) จะย้ายไปยังบัญชีนั้น และเกมจะได้รับเซสชันใหม่ บนอุปกรณ์ใดก็ได้
// 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>"}'การเชื่อมบัญชีทำงานอย่างไร#
- SDK สร้างค่าลับแบบใช้ครั้งเดียว (PKCE) และเริ่มโฟลว์การเข้าสู่ระบบ
- ผู้เล่นทำขั้นตอนให้เสร็จบนหน้าเว็บ ระหว่างนั้น SDK จะโพลสถานะจนกว่าโฟลว์จะเสร็จสิ้น
- SDK แลกโฟลว์เป็นเซสชันโดยใช้ค่าลับนั้น ลิงก์ที่ถูกขโมยไปจึงใช้ประโยชน์ไม่ได้
- โฟลว์จะหมดอายุหลัง 15 นาที (ขยายเวลาให้ระหว่างที่หน้าเว็บยังเปิดอยู่)
การกลับเข้าเกม#
หากไม่มี return URL หน้าเว็บจะบอกให้ผู้เล่นกลับไปที่เกมเอง หากต้องการส่งผู้เล่นกลับอัตโนมัติ ให้เพิ่มสคีม URL แบบกำหนดเอง (เช่น mygame) ใน คีย์ API ▸ URL กลับเข้าเกมหลังเข้าสู่ระบบ แล้วส่ง mygame://auth เป็น return URL
อีเวนต์#
ตอบสนองเมื่อผู้เล่นเปลี่ยน ออกจากระบบ หรือเมื่อรีเฟรชเซสชันไม่ได้อีกต่อไป:
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());ออกจากระบบ#
การออกจากระบบจะเพิกถอนเซสชันบนเซิร์ฟเวอร์และลบออกจากอุปกรณ์ การเข้าสู่ระบบครั้งถัดไปจะสร้างเกสต์ใหม่
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>"}'อ่านข้อมูลผู้เล่นจากเซิร์ฟเวอร์#
เซิร์ฟเวอร์ของคุณอ่านข้อมูลผู้เล่นคนใดก็ได้ด้วยคีย์ลับ รวมถึงอีเมลและยอดในกระเป๋าเงิน:
// 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"ในแดชบอร์ด#
หน้า ผู้เล่น แสดงผู้เล่นทุกคนพร้อมวิธีเข้าสู่ระบบ คะแนน เซฟ และกระเป๋าเงิน จากหน้านี้คุณเปลี่ยนชื่อหรือปิดใช้งานผู้เล่น ให้ออกจากระบบทุกอุปกรณ์ ส่งลิงก์รีเซ็ตรหัสผ่าน หรือลบผู้เล่นได้