/// <summary> /// Gets (from Facebook API) and sets (in to CurrentUser) info about the user /// (i.e Name and FB User ID) from a Facebook API call /// </summary> /// <param name="callbackResult">Result of a FB API Call for ("/me")</param> private static void UserInfoCallback(IGraphResult callbackResult) { //Create a dictonary of the result that we got back from the API Call Dictionary <string, object> callbackResultsDictionary = (Dictionary <string, object>)Facebook.MiniJSON.Json.Deserialize(callbackResult.RawResult); //Check that we have got the right call back (i.e it contains a key for name and user ID) if (!callbackResultsDictionary.ContainsKey("name") || !callbackResultsDictionary.ContainsKey("id")) { Debug.LogWarning("UserInfoCallback was called with the wrong call back info"); //Trigger Event for unsuccessful login and log out the current user FBLoginEvent?.Invoke(LoginEventResult.LoginFailed); FacebookLogout(); return; } //We get the name and userID back from our API call. (Name: "", userID: "") //we need to put this in our user info class CurrentUser = new FBUserInfo((string)callbackResultsDictionary["name"], Convert.ToInt64(callbackResultsDictionary["id"])); //Trigger event for successful logon FBLoginEvent?.Invoke(LoginEventResult.LoginSuccess); }
/// <summary> /// Log out of Facebook account /// </summary> public static void FacebookLogout() { //Don't allow logout with uninitalised facebook if (!FB.IsInitialized || !IsUserLoggedIn) { return; } //Null out logged in user info CurrentUser = null; //Logout of facebook and call event FB.LogOut(); FBLoginEvent?.Invoke(LoginEventResult.Logout); }
/// <summary> /// Login in to Facebook account /// </summary> public static void FacebookLogin() { //Don't allow login with uninitalised facebook if (!FB.IsInitialized) { Debug.LogError("Could not login to facebook as it has not been initalised"); FBLoginEvent?.Invoke(LoginEventResult.LoginFailed); return; } if (!IsUserLoggedIn) { //Login to facebook with permissions FB.LogInWithReadPermissions(FBPermissions, LoginCallback); } }
/// <summary> /// Callback function for login from facebook /// </summary> /// <param name="result">Result of the login</param> private static void LoginCallback(ILoginResult result) { //If login is cancelled or there was an error then don't save info if (result.Cancelled || result.Error != null) { CurrentUser = null; Debug.LogWarning("Did not login to facebook"); FBLoginEvent?.Invoke(LoginEventResult.LoginFailed); //Log out incase there is a cached user logged in FacebookLogout(); return; } //Get the info about the logged in user, that is name and FB user ID FB.API("/me", HttpMethod.GET, UserInfoCallback); }