169 lines
5.6 KiB
C#
169 lines
5.6 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using Google;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class LoginManager : MonoBehaviour
|
|
{
|
|
public Text txtChangeLogin;
|
|
public Text txtLogin;
|
|
public InputField usernameInput,passwordInput;
|
|
public GameObject loadingPanel;
|
|
void Awake(){
|
|
loadingPanel.SetActive(true);
|
|
|
|
// PlayGamesPlatform.Activate();
|
|
// LoginGooglePlayGames();
|
|
configuration = new GoogleSignInConfiguration {
|
|
WebClientId = "143863534373-b4f6l0esj5jr7kl2m66bkd0je21fsp7s.apps.googleusercontent.com",
|
|
RequestIdToken = false,
|
|
RequestEmail=true,
|
|
};
|
|
}
|
|
// public void LoginGooglePlayGames()
|
|
// {
|
|
// PlayGamesPlatform.Instance.Authenticate((success) =>
|
|
// {
|
|
// if (success == SignInStatus.Success)
|
|
// {
|
|
// Debug.Log("Login with Google Play games successful.");
|
|
// PlayGamesPlatform.Instance.Get
|
|
// PlayGamesPlatform.Instance.RequestServerSideAccess(true, code =>
|
|
// {
|
|
// Debug.Log("Authorization code: " + code);
|
|
// // Token = code;
|
|
// StartCoroutine(DoneLogin(task.Result.Email));
|
|
|
|
// // This token serves as an example to be used for SignInWithGooglePlayGames
|
|
// });
|
|
// }
|
|
// else
|
|
// {
|
|
// MessageBox.ShowMessage("Failed to retrieve Google play games authorization code", "Error Auth");
|
|
// Debug.Log("Login Unsuccessful");
|
|
// }
|
|
// });
|
|
// }
|
|
|
|
async void Start(){
|
|
// DataManager.GoogleLogin("sewmina7d@gmail.com");
|
|
// return;
|
|
if(PlayerPrefs.HasKey("username") && PlayerPrefs.HasKey("password")){
|
|
int result = await DataManager.Login(PlayerPrefs.GetString("username"), PlayerPrefs.GetString("password"));
|
|
if(result == 0){return;}
|
|
}
|
|
|
|
loadingPanel.SetActive(false);
|
|
}
|
|
|
|
public async void Login(){
|
|
|
|
if(usernameInput.text.Length < 4 || passwordInput.text.Length < 4){
|
|
MessageBox.ShowMessage("Username and Password must be longer than 4 characters","Invalid Input");
|
|
return;
|
|
}
|
|
loadingPanel.SetActive(true);
|
|
|
|
int result = await DataManager.Login(usernameInput.text, passwordInput.text);
|
|
if(result == 0){return;}
|
|
|
|
loadingPanel.SetActive(false);
|
|
}
|
|
|
|
public async void Register(){
|
|
loadingPanel.SetActive(true);
|
|
|
|
int result = await DataManager.Login(usernameInput.text, passwordInput.text);
|
|
if(result == 0){return;}
|
|
|
|
loadingPanel.SetActive(false);
|
|
}
|
|
|
|
private GoogleSignInConfiguration configuration;
|
|
|
|
public void OnSignIn() {
|
|
GoogleSignIn.Configuration = configuration;
|
|
GoogleSignIn.Configuration.UseGameSignIn = false;
|
|
GoogleSignIn.Configuration.RequestIdToken = true;
|
|
// AddStatusText("Calling SignIn");
|
|
|
|
GoogleSignIn.DefaultInstance.SignIn().ContinueWith(
|
|
OnAuthenticationFinished);
|
|
}
|
|
|
|
public void OnSignOut() {
|
|
// AddStatusText("Calling SignOut");
|
|
GoogleSignIn.DefaultInstance.SignOut();
|
|
}
|
|
|
|
public void OnDisconnect() {
|
|
// AddStatusText("Calling Disconnect");
|
|
GoogleSignIn.DefaultInstance.Disconnect();
|
|
}
|
|
|
|
internal void OnAuthenticationFinished(Task<GoogleSignInUser> task) {
|
|
if (task.IsFaulted) {
|
|
using (IEnumerator<System.Exception> enumerator =
|
|
task.Exception.InnerExceptions.GetEnumerator()) {
|
|
if (enumerator.MoveNext()) {
|
|
GoogleSignIn.SignInException error =
|
|
(GoogleSignIn.SignInException)enumerator.Current;
|
|
// AddStatusText("Got Error: " + error.Status + " " + error.Message);
|
|
MessageBox.ShowMessage("Got Error: " + error.Status + " " + error.Message);
|
|
} else {
|
|
// AddStatusText("Got Unexpected Exception?!?" + task.Exception);
|
|
MessageBox.ShowMessage("Got Unexpected Exception?!?" + task.Exception);
|
|
}
|
|
}
|
|
} else if(task.IsCanceled) {
|
|
// AddStatusText("Canceled");
|
|
|
|
} else {
|
|
// AddStatusText("Welcome: " + task.Result.DisplayName + "!");
|
|
// MessageBox.ShowMessage($"email: {task.Result.Email}\nDisplay:{task.Result.DisplayName}\ntoken:{task.Result.IdToken}");
|
|
StartCoroutine(DoneLogin(task.Result.Email));
|
|
}
|
|
}
|
|
|
|
IEnumerator DoneLogin(string username){
|
|
yield return new WaitForSeconds(0.5f);
|
|
|
|
DataManager.GoogleLogin(username);
|
|
}
|
|
|
|
public void OnSignInSilently() {
|
|
GoogleSignIn.Configuration = configuration;
|
|
GoogleSignIn.Configuration.UseGameSignIn = false;
|
|
GoogleSignIn.Configuration.RequestIdToken = true;
|
|
// AddStatusText("Calling SignIn Silently");
|
|
|
|
GoogleSignIn.DefaultInstance.SignInSilently()
|
|
.ContinueWith(OnAuthenticationFinished);
|
|
}
|
|
|
|
|
|
public void OnGamesSignIn() {
|
|
GoogleSignIn.Configuration = configuration;
|
|
GoogleSignIn.Configuration.UseGameSignIn = true;
|
|
GoogleSignIn.Configuration.RequestIdToken = false;
|
|
|
|
// AddStatusText("Calling Games SignIn");
|
|
|
|
GoogleSignIn.DefaultInstance.SignIn().ContinueWith(
|
|
OnAuthenticationFinished);
|
|
}
|
|
|
|
public bool isLogin => txtLogin.text == "Login";
|
|
public void ToggleLoginPage(){
|
|
if(txtLogin.text == "Login"){
|
|
txtLogin.text = "Register";
|
|
txtChangeLogin.text = "Have an account?";
|
|
}else{
|
|
txtLogin.text = "Login";
|
|
txtChangeLogin.text = "Don't have an account?";
|
|
}
|
|
}
|
|
}
|