Firebase Auth and Unity

0

Does anyone have an example or would you know how to verify email authentication within unity using firebase

    
asked by anonymous 29.01.2018 / 13:37

1 answer

0

Friend follows the code for everyone. Email (Sign in and Sign in), Facebook and Google. using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement; using Firebase;

public class Login: MonoBehaviour {

private string email;
private string password;

public Text _btninformativoDeAguarde;
public Text LabelInfo;
public InputField inputEmail, inputPassword;

//private string googleIdToken, googleAccessToken, accessToken;

Firebase.Auth.FirebaseAuth auth;


private void Awake()
{

    auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
    VerificaCompatibilidade();
}

// Use this for initialization
void VerificaCompatibilidade()
{
    Firebase.FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task =>
    {
        var dependencyStatus = task.Result;
        if (dependencyStatus == Firebase.DependencyStatus.Available)
        {
            // Set a flag here indiciating that Firebase is ready to use by your
            // application.

// LabelInfo.text="Compatible";             }             else             {                 UnityEngine.Debug.LogError (System.String.Format (                   "Could not resolve all Firebase dependencies: {0}", dependencyStatus));                 // Firebase Unity SDK is not safe to use here.                 LabelInfo.text="Not compatible";             }         });     }

public void GoogleLogin(string googleIdToken, string googleAccessToken)
{
    Firebase.Auth.Credential credential = Firebase.Auth.GoogleAuthProvider.GetCredential(googleIdToken, googleAccessToken);
    auth.SignInWithCredentialAsync(credential).ContinueWith(task => {
        if (task.IsCanceled)
        {
            Debug.LogError("SignInWithCredentialAsync was canceled.");
            return;
        }
        if (task.IsFaulted)
        {
            Debug.LogError("SignInWithCredentialAsync encountered an error: " + task.Exception);
            return;
        }
        Firebase.Auth.FirebaseUser newUser = task.Result;
        Debug.LogFormat("User signed in successfully: {0} ({1})",
        newUser.DisplayName, newUser.UserId);
    });
}
// Configure Google Sign In

public void FacebookLogin(string accessToken)
{

    //https:// (end do firebase).firebaseapp.com/__/auth/handler

    Firebase.Auth.Credential credential = Firebase.Auth.FacebookAuthProvider.GetCredential(accessToken);
    auth.SignInWithCredentialAsync(credential).ContinueWith(task =>
    {
        if (task.IsCanceled)
        {
            Debug.LogError("SignInWithCredentialAsync was canceled.");
            LabelInfo.text = " SignInWithCredentialAsync was canceled. ";
            return;
        }
        if (task.IsFaulted)
        {
            Debug.LogError("SignInWithCredentialAsync encountered an error: " + task.Exception);
            LabelInfo.text = "SignInWithCredentialAsync encountered an error: " + task.Exception;
            return;
        }

        Firebase.Auth.FirebaseUser newUser = task.Result;
        Debug.LogFormat("User signed in successfully: {0} ({1})",
            newUser.DisplayName, newUser.UserId);
    });

    Firebase.Auth.FirebaseUser user = auth.CurrentUser;
    if (user != null)
    {
        string name = user.DisplayName;
        string email = user.Email;
        System.Uri photo_url = user.PhotoUrl;
        // The user's Id, unique to the Firebase project.
        // Do NOT use this value to authenticate with your backend server, if you
        // have one; use User.TokenAsync() instead.
        string uid = user.UserId;
    }


}


public void CadastrarUsuario()
{
    email = inputEmail.text;
    password = inputPassword.text;

    auth.CreateUserWithEmailAndPasswordAsync(email, password).ContinueWith(task => {
        if (task.IsCanceled)
        {
            Debug.LogError("CreateUserWithEmailAndPasswordAsync was canceled.");
            LabelInfo.text = "CreateUserWithEmailAndPasswordAsync was canceled.";
            return;
        }
        if (task.IsFaulted)
        {
            Debug.LogError("Create User With Email And Password Async encountered an error: " + task.Exception);
            LabelInfo.text = "Create User With Email And Password Async encountered an error: " + task.Exception;
            return;
        }
        // Firebase user has been created.
        Firebase.Auth.FirebaseUser newUser = task.Result;
        Debug.LogFormat("Usuário Cadastrado: {0} ({1})",newUser.DisplayName, newUser.UserId);
        LabelInfo.text = string.Format("Usuário Cadastrado: {0} ({1})", newUser.DisplayName, newUser.UserId);

        //Send Emaisl Verification
        newUser.SendEmailVerificationAsync().ContinueWith(t => {
        Debug.Log("Verifique o seu e-mail");

         PlayerPrefs.SetString("UserId", newUser.UserId);
        });

    });
}
public void LoginUser()
{
    email = inputEmail.text;
    password = inputPassword.text;


    auth.SignInWithEmailAndPasswordAsync(email, password).ContinueWith(task => {


        if (task.IsCanceled)
        {
            Debug.LogError("SignInWithEmailAndPasswordAsync was canceled.");
            LabelInfo.text = "Erro de login e senha";
            return;
        }
        if (task.IsFaulted)
        {
            Debug.LogError("SignInWithEmailAndPasswordAsync encountered an error: " + task.Exception);
            LabelInfo.text = "não encontrado usuário" + task.Exception;
            return;
        }

        //FireBase user has been created.
        Firebase.Auth.FirebaseUser newUser = task.Result;
        Debug.LogFormat("User signed in successfully: {0} ({1})", newUser.DisplayName, newUser.UserId);
        //
        LabelInfo.text = string.Format("Usuário Logado: {0} ({1})", newUser.DisplayName, newUser.UserId);
        _btninformativoDeAguarde.text = "aguarde...";

        PlayerPrefs.SetString("UserId", newUser.UserId);

        SceneManager.LoadSceneAsync(1);
    });
}

}

    
09.11.2018 / 20:47