Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
95 lines (82 sloc) 2.98 KB
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Firebase.Auth;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System;
public class EmailPassword : MonoBehaviour
{
private FirebaseAuth auth;
public InputField UserNameInput, PasswordInput;
public Button SignupButton, LoginButton;
public Text ErrorText;
void Start()
{
auth = FirebaseAuth.DefaultInstance;
//Just an example to save typing in the login form
UserNameInput.text = "demofirebase@gmail.com";
PasswordInput.text = "abcdefgh";
SignupButton.onClick.AddListener(() => Signup(UserNameInput.text, PasswordInput.text));
LoginButton.onClick.AddListener(() => Login(UserNameInput.text, PasswordInput.text));
}
public void Signup(string email, string password)
{
if (string.IsNullOrEmpty(email) || string.IsNullOrEmpty(password))
{
//Error handling
return;
}
auth.CreateUserWithEmailAndPasswordAsync(email, password).ContinueWith(task =>
{
if (task.IsCanceled)
{
Debug.LogError("CreateUserWithEmailAndPasswordAsync was canceled.");
return;
}
if (task.IsFaulted)
{
Debug.LogError("CreateUserWithEmailAndPasswordAsync error: " + task.Exception);
if (task.Exception.InnerExceptions.Count > 0)
UpdateErrorMessage(task.Exception.InnerExceptions[0].Message);
return;
}
FirebaseUser newUser = task.Result; // Firebase user has been created.
Debug.LogFormat("Firebase user created successfully: {0} ({1})",
newUser.DisplayName, newUser.UserId);
UpdateErrorMessage("Signup Success");
});
}
private void UpdateErrorMessage(string message)
{
ErrorText.text = message;
Invoke("ClearErrorMessage", 3);
}
void ClearErrorMessage()
{
ErrorText.text = "";
}
public void Login(string email, string password)
{
auth.SignInWithEmailAndPasswordAsync(email, password).ContinueWith(task =>
{
if (task.IsCanceled)
{
Debug.LogError("SignInWithEmailAndPasswordAsync canceled.");
return;
}
if (task.IsFaulted)
{
Debug.LogError("SignInWithEmailAndPasswordAsync error: " + task.Exception);
if (task.Exception.InnerExceptions.Count > 0)
UpdateErrorMessage(task.Exception.InnerExceptions[0].Message);
return;
}
FirebaseUser user = task.Result;
Debug.LogFormat("User signed in successfully: {0} ({1})",
user.DisplayName, user.UserId);
PlayerPrefs.SetString("LoginUser", user != null ? user.Email : "Unknown");
SceneManager.LoadScene("LoginResults");
});
}
}
You can’t perform that action at this time.