身份驗證 + 社交 (@colyseus/social
)¶
棄用通知
@colyseus/social
模組即將被完全棄用. 目前我們建議使用 Firebase, Auth0 或其他身份證驗服務.
在此介紹 @colyseus/social
的配置和用法.
@colyseus/social
是一個實驗性模組, 提供通用型後端服務, 以方便多人遊戲的開發. 歡迎為此 API 提供建議和改進方法.
Tip
如果想要實現自己的身份驗證方法,請參考 Room » onAuth().
安裝¶
-
安裝
@colyseus/social
模組.
npm install @colyseus/social
npm install express-jwt
- 匯入和公開
@colyseus/social
提供的 Express 入口
import express from "express";
import socialRoutes from "@colyseus/social/express"
const app = express();
app.use("/", socialRoutes);
app.listen(8080);
const express = require("express");
const socialRoutes = require("@colyseus/social/express").default;
const app = express();
app.use("/", socialRoutes);
app.listen(8080);
伺服器端配置¶
環境變數¶
MONGO_URI
: MongoDB 連線 URIJWT_SECRET
: 用於身份驗證的安全保密字符串FACEBOOK_APP_TOKEN
: Facebook 應用令牌 ("appid|appsecret"
)
伺服器端 API¶
@colyseus/social
裏提供了 MongoDB 模組,以及令牌驗證功能.
typescript import { User, FriendRequest, verifyToken } from "@colyseus/social";
實現 onAuth
以檢索當前用戶¶
import { User, verifyToken } from "@colyseus/social";
class MyRoom extends Room {
async onAuth(client, options) {
// 令牌身份驗證
const token = verifyToken(options.token);
// 以用戶 id 查找用戶
return await User.findById(token._id);
}
onJoin(client, options, user) {
console.log(user.username, "has joined the room!");
}
}
勾點¶
hooks.beforeAuthenticate
¶
在用戶登錄或註冊之前, 會觸發 beforeAuthenticate
.
import { hooks } from "@colyseus/social";
hooks.beforeAuthenticate((provider, $setOnInsert, $set) => {
// 註冊時賦予預設元數據
$setOnInsert.metadata = {
coins: 100,
trophies: 0
};
});
hooks.beforeUserUpdate
¶
在用戶 通過 save() 方法 更新自己的資訊之前, 會觸發 beforeUserUpdate
.
import Filter from "bad-words";
const filter = new Filter();
hooks.beforeUserUpdate((_id, fields) => {
if (fields['username'] && filter.isProfane(fields['username'])) {
throw new Error("no_swearing_allowed");
}
})
客戶端 API¶
登錄¶
匿名¶
await client.auth.login();
await client.Auth.Login();
client.auth:login(function(err, auth)
-- ...
end);
電子郵件 + 密碼¶
await client.auth.login({
email: "user@example.com",
password: "12345"
});
await client.Auth.Login("user@example.com", "12345");
client.auth:login({
email = "user@example.com",
password = "12345"
}, function(err, auth)
-- ...
end)
Facebook¶
//
// 請提前安裝和配置好 Facebook SDK
// - https://developers.facebook.com/docs/javascript/quickstart
// - https://developers.facebook.com/docs/facebook-login/web
//
FB.login(function(response) {
if (response.authResponse) {
client.auth.login({ accessToken: response.authResponse.accessToken });
}
}, { scope: 'public_profile,email,user_friends' });
//
// 請提前安裝和配置好 Facebook SDK
// - https://developers.facebook.com/docs/unity/gettingstarted
// - https://developers.facebook.com/docs/unity/examples#login
//
var perms = new List<string>(){"public_profile", "email", "user_friends"};
FB.LogInWithReadPermissions(perms, AuthCallback);
private void AuthCallback (ILoginResult result) {
if (FB.IsLoggedIn) {
client.Auth.Login(Facebook.Unity.AccessToken.CurrentAccessToken);
}
}
client.auth:facebook_login(function(err, auth)
pprint(auth)
end)
更新用戶數據¶
您可以在客戶端修改 username
, displayName
, avatarUrl
, lang
, location
和 timezone
,然後調用 save()
方法保存.
client.auth.username = "Hello world!"
await client.auth.save();
client.Auth.Username = "Hello world!";
await client.Auth.Save();
client.auth.username = "Hello world!"
client.auth:save()
登出¶
client.auth.logout();
client.Auth.Logout();
client.auth:logout();
獲取好友數據¶
const friends = await client.auth.getFriends();
friends.forEach(friend => {
console.log(friend.username);
});
var friends = await client.Auth.GetFriends();
for (var i=0; i<friends.Length; i++)
{
Debug.Log(friends[i].Username);
}
client.auth:get_friends(function(err, friends)
for i, friend in pairs(friends) do
print(friend.username)
end
end);
獲取在線好友¶
const friends = await client.auth.getOnlineFriends();
friends.forEach(friend => {
console.log(friend.username);
});
var friends = await client.Auth.GetOnlineFriends();
for (var i=0; i<friends.Length; i++)
{
Debug.Log(friends[i].Username);
}
client.auth:get_online_friends(function(err, friends)
for i, friend in pairs(friends) do
print(friend.username)
end
end);
獲取加好友請求¶
const friends = await client.auth.getFriendRequests();
friends.forEach(friend => {
console.log(friend.username);
});
var friends = await client.Auth.GetFriendRequests();
for (var i=0; i<friends.Length; i++)
{
Debug.Log(friends[i].Username);
}
client.auth:get_friend_requests(function(err, friends)
for i, friend in pairs(friends) do
print(friend.username)
end
end);
同意加好友請求¶
await client.auth.acceptFriendRequest(friendId);
await client.Auth.AcceptFriendRequest(friendId);
client.auth:accept_friend_request(friend_id)
拒絕加好友請求¶
await client.auth.declineFriendRequest(friendId);
await client.Auth.DeclineFriendRequest(friendId);
client.auth:decline_friend_request(friend_id)
發送加好友請求¶
await client.auth.sendFriendRequest(friendId);
await client.Auth.SendFriendRequest(friendId);
client.auth:send_friend_request(friend_id)
拉黑用戶¶
await client.auth.blockUser(friendId);
await client.Auth.BlockUser(friendId);
client.auth:block_user(friend_id)
拉黑恢復¶
await client.auth.unblockUser(friendId);
await client.Auth.UnblockUser(friendId);
client.auth:unblock_user(friend_id)