Advertisement
Guest User

Untitled

a guest
Jun 15th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.90 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using Firebase.Auth;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. using UnityEngine.SceneManagement;
  7.  
  8. public class Auth : MonoBehaviour
  9. {
  10.     private FirebaseAuth auth;
  11.     private FirebaseUser user;
  12.     public InputField Email, Pwd;
  13.  
  14.     void Start()
  15.     {
  16.         InitializeFirebase();
  17.     }
  18.  
  19.     void InitializeFirebase()
  20.     {
  21.         auth = FirebaseAuth.DefaultInstance;
  22.         auth.SignOut();
  23.         auth.StateChanged += AuthStateChanged;
  24.         AuthStateChanged(this, null);
  25.     }
  26.  
  27.     public void OnClickLogin()
  28.     {
  29.         auth.SignInWithEmailAndPasswordAsync(Email.text, Pwd.text).ContinueWith(task =>
  30.         {
  31.             if (task.IsCanceled)
  32.             {
  33.                 Debug.Log("SignInWithEmailAndPasswordAsync was canceled.");
  34.                 return;
  35.             }
  36.             if (task.IsFaulted)
  37.             {
  38.                 Debug.Log("SignInWithEmailAndPasswordAsync encountered an error: " + task.Exception);
  39.                 return;
  40.             }
  41.  
  42.             FirebaseUser newUser = task.Result;
  43.             Debug.LogFormat("User signed in successfully: {0} ({1})",
  44.                 newUser.DisplayName, newUser.UserId);
  45.         });
  46.     }
  47.  
  48.     void AuthStateChanged(object sender, System.EventArgs eventArgs)
  49.     {
  50.         if (auth.CurrentUser != user)
  51.         {
  52.             bool signedIn = user != auth.CurrentUser && auth.CurrentUser != null;
  53.             if (!signedIn && user != null)
  54.             {
  55.                 Debug.Log("Signed out " + user.UserId);
  56.             }
  57.             user = auth.CurrentUser;
  58.             if (signedIn)
  59.             {
  60.                 Debug.Log("Signed in " + user.UserId);
  61.                 Debug.Log(user.Email);
  62.  
  63.                 SceneManager.LoadScene("MercedesAR");
  64.             }
  65.         }
  66.     }
  67.  
  68.     void OnApplicationQuit()
  69.     {
  70.         auth.SignOut();
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement