Skip to content

Instantly share code, notes, and snippets.

@novabyte
Created October 18, 2017 16:55
Show Gist options
  • Save novabyte/cd6afe28afa7949cde7d46545efd0f37 to your computer and use it in GitHub Desktop.
Save novabyte/cd6afe28afa7949cde7d46545efd0f37 to your computer and use it in GitHub Desktop.
A simple example on how to register/login a user via Facebook with Unity and Nakama server.
/**
* Copyright 2017 The Nakama Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using Facebook.Unity;
using Nakama;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FacebookManager : MonoBehaviour {
public bool IsLoggedIn {
get {
return FB.IsLoggedIn;
}
}
public string CurrentUserId {
get {
if (Facebook.Unity.AccessToken.CurrentAccessToken != null) {
return Facebook.Unity.AccessToken.CurrentAccessToken.UserId;
} else {
// Must login via Facebook for a valid access token.
return null;
}
}
}
void Awake() {
if (!FB.IsInitialized) {
FB.Init(SetInit, SetOnHide);
} else {
FB.ActivateApp();
}
}
void Update () {
if (Input.GetKeyDown(KeyCode.Space)) {
FBLoginButtonAction();
}
}
private void SetInit() {
if (FB.IsInitialized) {
FB.ActivateApp();
} else {
Debug.Log("Failed FB SDK init.");
}
}
private void SetOnHide(bool isGameShown) {
}
public void FBLoginButtonAction() {
FBLogin();
}
public void FBLogin() {
var permissions = new List<string> {
"public_profile",
"email",
"user_friends"
};
FB.LogInWithReadPermissions(permissions, AuthCallback);
}
void AuthCallback(ILoginResult result) {
if (result.Error != null) {
Debug.LogFormat("Facebook SDK error: {0}", result.Error);
} else if (FB.IsLoggedIn) {
var aToken = Facebook.Unity.AccessToken.CurrentAccessToken;
#if UNITY_EDITOR
foreach (string permission in aToken.Permissions) {
Debug.Log(permission);
}
#endif
ServerLoginOrRegister(aToken.TokenString);
} else {
Debug.Log("Facebook login was cancelled.");
}
}
public void ServerLoginOrRegister(string aToken) {
var client = NClient.Default("defaultkey");
var message = NAuthenticateMessage.Facebook(aToken);
Action<INError> errorHandler = delegate(INError err) {
Debug.LogErrorFormat("Error: code '{0}' with '{1}'.", err.Code, err.Message);
};
Action<INSession> sessionHandler = delegate(INSession session) {
Debug.LogFormat("Session: '{0}'.", session.Token);
client.Connect(session, (bool done) => {
FacebookLink(client, aToken);
});
};
client.Login(message, sessionHandler, (INError err) => {
if (err.Code == ErrorCode.UserNotFound) {
client.Register(message, sessionHandler, errorHandler);
} else {
errorHandler(err);
}
});
}
public void FacebookLink(INClient client, string aToken) {
var message = NSelfLinkMessage.Facebook(aToken);
client.Send(message, (bool done) => {
Debug.Log("Successfully linked device ID to current user.");
}, (INError err) => {
Debug.LogErrorFormat("Error: code '{0}' with '{1}'.", err.Code, err.Message);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment