Commit df17858c authored by nk161690's avatar nk161690

login

parent 0ebba712
...@@ -4,33 +4,54 @@ using Photon.Pun; ...@@ -4,33 +4,54 @@ using Photon.Pun;
using Photon.Realtime; using Photon.Realtime;
using System; using System;
using Facebook.Unity; using Facebook.Unity;
using System.Threading;
using System.Collections.Generic;
public class FacebookLogin : MonoBehaviour public class FacebookLogin : MonoBehaviour
{ {
private readonly string[] facebookPermissions = { "public_profile", "email" };
private Log.LoginSession session = new Log.LoginSession(); private Log.LoginSession session = new Log.LoginSession();
public string Token;
public string Error;
private void Awake() private void Awake()
{ {
if (!FB.IsInitialized) if (!FB.IsInitialized)
{ {
FB.Init(OnInitComplete); // Initialize the Facebook SDK
FB.Init(InitCallback, OnHideUnity);
} }
else else
{ {
OnInitComplete(); // Already initialized, signal an app activation App Event
FB.ActivateApp();
} }
} }
private void OnInitComplete() void InitCallback()
{ {
if (FB.IsInitialized) if (FB.IsInitialized)
{ {
// Signal an app activation App Event
FB.ActivateApp(); FB.ActivateApp();
// Continue with Facebook SDK
} }
else 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 ...@@ -42,21 +63,15 @@ public class FacebookLogin : MonoBehaviour
public void OnFacebookLoginButtonClicked() public void OnFacebookLoginButtonClicked()
{ {
if (FB.IsLoggedIn) FB.Mobile.LoginWithTrackingPreference(LoginTracking.LIMITED, callback:OnLoginComplete);
{
HandleLoggedIn();
}
else
{
FB.LogInWithReadPermissions(callback: OnLoginComplete);
}
} }
private void OnLoginComplete(ILoginResult result) private void OnLoginComplete(ILoginResult result)
{ {
if (result.Error == null && !result.Cancelled) if (FB.IsLoggedIn)
{ {
string accessToken = AccessToken.CurrentAccessToken.TokenString; string accessToken = AccessToken.CurrentAccessToken.TokenString;
Token = accessToken;
Debug.Log("Facebook Login Successful! Access Token: " + accessToken); Debug.Log("Facebook Login Successful! Access Token: " + accessToken);
// Continue with Photon authentication // Continue with Photon authentication
...@@ -72,31 +87,6 @@ public class FacebookLogin : MonoBehaviour ...@@ -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() private void OnFacebookLoggedIn()
{ {
string aToken = AccessToken.CurrentAccessToken.TokenString; string aToken = AccessToken.CurrentAccessToken.TokenString;
......
...@@ -3,26 +3,62 @@ using GooglePlayGames.BasicApi; ...@@ -3,26 +3,62 @@ using GooglePlayGames.BasicApi;
using System; using System;
using TMPro; using TMPro;
using UnityEngine; using UnityEngine;
using Unity.Services.Core;
using Unity.Services.Authentication;
using System.Threading.Tasks;
public class GgPlayLogin : MonoBehaviour public class GgPlayLogin : MonoBehaviour
{ {
[SerializeField] private TextMeshProUGUI message; [SerializeField] private TextMeshProUGUI message;
public void OnGgPlayLoginButtonClicked() public string GooglePlayToken;
public string GooglePlayError;
public async Task Authenticate()
{ {
PlayGamesPlatform.Activate(); PlayGamesPlatform.Activate();
PlayGamesPlatform.Instance.Authenticate(ProcessAuthentication); await UnityServices.InitializeAsync();
PlayGamesPlatform.Instance.Authenticate((success) =>
{
if (success == SignInStatus.Success)
{
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();
} }
internal void ProcessAuthentication(SignInStatus status) private async Task AuthenticateWithUnity()
{ {
if (status == SignInStatus.Success) try
{ {
message.text = "Connected"; await AuthenticationService.Instance.SignInWithGoogleAsync(GooglePlayToken);
}
catch (AuthenticationException ex)
{
Debug.LogException(ex);
message.text = "Failed";
throw;
} }
else catch (RequestFailedException ex)
{ {
Debug.LogException(ex);
message.text = "Failed"; 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