Advertisement
Guest User

Confusion

a guest
Nov 28th, 2015
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.45 KB | None | 0 0
  1. using System;
  2. using UnityEngine;
  3. using Facebook.Unity;
  4. using GameSparks.Api.Requests;
  5.  
  6. public class AuthManagerFB {
  7.     static bool loggedIn;
  8.      
  9.     /// <summary>
  10.     /// Pass all actions that require authenticated user as a callback.
  11.     /// </summary>
  12.     /// <param name="onSuccess">Called if/once user has authenticated</param>
  13.     /// <param name="onError">Called on failure</param>
  14.     /// <param name="showDialog">Whether or not to show the Facebook-login dialog if user has not authenticated</param>
  15.     /// <example>
  16.     /// <code>
  17.     /// ifAuthenticated(() => { /* User authenticated, do something */ },
  18.     ///   onError: msg => Debug.Log("Error: " + msg),
  19.     ///   showDialog: true);
  20.     /// </code>
  21.     /// </example>
  22.     public void ifAuthenticated(Action onSuccess, Action<string> onError = null, bool showDialog = false) {
  23.         if (loggedIn) {
  24.             onSuccess();
  25.             return;
  26.         }
  27.         if (onError == null) onError = s => {};
  28.          
  29.         Action doGameSparksLogin = () => {
  30.             new FacebookConnectRequest().SetAccessToken(AccessToken.CurrentAccessToken.TokenString).Send(response => {
  31.                 if (response.HasErrors) {
  32.                     onError("GameSpark login failed");
  33.                 } else {
  34.                     Debug.Log("Logged in as " + response.UserId);
  35.                     loggedIn = true;
  36.                      
  37.                     onSuccess();
  38.                 }
  39.             });
  40.         };
  41.          
  42.         InitDelegate doFacebookLogin = () => {
  43.             if (FB.IsLoggedIn)
  44.                 doGameSparksLogin();
  45.             else if (showDialog) {
  46.                 FB.LogInWithReadPermissions(new string[] { "public_profile", "email", "user_friends" }, loginResult => {
  47.                     if (FB.IsLoggedIn)
  48.                         doGameSparksLogin();
  49.                     else {
  50.                         Debug.Log("Facebook login error: " + loginResult.Error);
  51.                         onError("Facebook login failed");
  52.                     }
  53.                 });
  54.             } else
  55.                 onError("Not logged in");
  56.         };
  57.          
  58.         if (FB.IsInitialized)
  59.             doFacebookLogin();
  60.         else {
  61.             try {
  62.                 FB.Init(doFacebookLogin);
  63.             } catch (Exception e) {
  64.                 Debug.LogWarning("Facebook ERROR: " + e.Message);
  65.                 onError("Facebook login failed");
  66.             }
  67.         }
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement