Commit df17858c authored by nk161690's avatar nk161690

login

parent 0ebba712
......@@ -4,33 +4,54 @@ using Photon.Pun;
using Photon.Realtime;
using System;
using Facebook.Unity;
using System.Threading;
using System.Collections.Generic;
public class FacebookLogin : MonoBehaviour
{
private readonly string[] facebookPermissions = { "public_profile", "email" };
private Log.LoginSession session = new Log.LoginSession();
public string Token;
public string Error;
private void Awake()
{
if (!FB.IsInitialized)
{
FB.Init(OnInitComplete);
// Initialize the Facebook SDK
FB.Init(InitCallback, OnHideUnity);
}
else
{
OnInitComplete();
// Already initialized, signal an app activation App Event
FB.ActivateApp();
}
}
private void OnInitComplete()
void InitCallback()
{
if (FB.IsInitialized)
{
// Signal an app activation App Event
FB.ActivateApp();
// Continue with Facebook SDK
}
else
{
Debug.LogError("Failed to Initialize the Facebook SDK");
Debug.Log("Failed to Initialize the Facebook SDK");
}
}
void OnHideUnity(bool isGameShown)
{
if (!isGameShown)
{
// Pause the game - we will need to hide
Time.timeScale = 0;
}
else
{
// Resume the game - we're getting focus again
Time.timeScale = 1;
}
}
......@@ -42,21 +63,15 @@ public class FacebookLogin : MonoBehaviour
public void OnFacebookLoginButtonClicked()
{
if (FB.IsLoggedIn)
{
HandleLoggedIn();
}
else
{
FB.LogInWithReadPermissions(callback: OnLoginComplete);
}
FB.Mobile.LoginWithTrackingPreference(LoginTracking.LIMITED, callback:OnLoginComplete);
}
private void OnLoginComplete(ILoginResult result)
{
if (result.Error == null && !result.Cancelled)
if (FB.IsLoggedIn)
{
string accessToken = AccessToken.CurrentAccessToken.TokenString;
Token = accessToken;
Debug.Log("Facebook Login Successful! Access Token: " + accessToken);
// Continue with Photon authentication
......@@ -72,31 +87,6 @@ public class FacebookLogin : MonoBehaviour
}
}
private void HandleLoggedIn()
{
string accessToken = AccessToken.CurrentAccessToken.TokenString;
string userID = AccessToken.CurrentAccessToken.UserId;
// Request user's Facebook information
FB.API("/me?fields=id,name,email", HttpMethod.GET, OnFacebookGraphAPICallback);
}
private void OnFacebookGraphAPICallback(IGraphResult result)
{
if (string.IsNullOrEmpty(result.Error))
{
// Handle user's Facebook data
string userName = result.ResultDictionary["name"].ToString();
string userEmail = result.ResultDictionary["email"].ToString();
Debug.Log($"User Name: {userName}, Email: {userEmail}");
}
else
{
Debug.LogError($"Facebook Graph API request failed: {result.Error}");
}
}
private void OnFacebookLoggedIn()
{
string aToken = AccessToken.CurrentAccessToken.TokenString;
......
......@@ -3,26 +3,62 @@ using GooglePlayGames.BasicApi;
using System;
using TMPro;
using UnityEngine;
using Unity.Services.Core;
using Unity.Services.Authentication;
using System.Threading.Tasks;
public class GgPlayLogin : MonoBehaviour
{
[SerializeField] private TextMeshProUGUI message;
public void OnGgPlayLoginButtonClicked()
public string GooglePlayToken;
public string GooglePlayError;
public async Task Authenticate()
{
PlayGamesPlatform.Activate();
PlayGamesPlatform.Instance.Authenticate(ProcessAuthentication);
}
await UnityServices.InitializeAsync();
internal void ProcessAuthentication(SignInStatus status)
PlayGamesPlatform.Instance.Authenticate((success) =>
{
if (status == SignInStatus.Success)
if (success == SignInStatus.Success)
{
message.text = "Connected";
Debug.Log("Login with Google was successful.");
message.text = "Login with Google was successful.";
PlayGamesPlatform.Instance.RequestServerSideAccess(true, code =>
{
Debug.Log($"Auth code is {code}");
GooglePlayToken = code;
});
}
else
{
GooglePlayError = "Failed to retrieve GPG auth code";
Debug.LogError("Login Unsuccessful");
message.text = "Failed";
}
});
await AuthenticateWithUnity();
}
private async Task AuthenticateWithUnity()
{
try
{
await AuthenticationService.Instance.SignInWithGoogleAsync(GooglePlayToken);
}
catch (AuthenticationException ex)
{
Debug.LogException(ex);
message.text = "Failed";
throw;
}
catch (RequestFailedException ex)
{
Debug.LogException(ex);
message.text = "Failed";
throw;
}
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment