Firebase Auth and Unity

Asked

Viewed 172 times

0

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

1 answer

1

Friend follows there the dodigo for all. Email(Register and Login), 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);
    });
}

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.