/// <summary> /// Private helper that signs the into Google Play Games and performs /// a Firebase account operation. /// </summary> private static Task <FirebaseUser> SignIntoGooglePlayServices( Action <Firebase.Auth.Credential, TaskCompletionSource <FirebaseUser> > operation) { TaskCompletionSource <FirebaseUser> taskCompletionSource = new TaskCompletionSource <FirebaseUser>(); UnityEngine.Social.localUser.Authenticate((bool success) => { if (success) { String authCode = PlayGamesPlatform.Instance.GetServerAuthCode(); if (String.IsNullOrEmpty(authCode)) { Debug.LogError(@"Signed into Play Games Services but failed to get the server auth code, will not be able to sign into Firebase"); taskCompletionSource.SetException(new AggregateException( new[] { new ApplicationException() })); return; } Credential credential = PlayGamesAuthProvider.GetCredential(authCode); operation(credential, taskCompletionSource); } else { Debug.LogError("Failed to sign into Play Games Services"); taskCompletionSource.SetException(new AggregateException( new[] { new ApplicationException() })); } }); return(taskCompletionSource.Task); }
public static void Auth(string authCode, Action <Task> callback) { auth = FirebaseAuth.DefaultInstance; Credential credential = PlayGamesAuthProvider.GetCredential(authCode); auth.SignInWithCredentialAsync(credential).ContinueWithOnMainThread(task => { if (task.IsCanceled) { UnityEngine.Debug.LogError("Firebase - SignInWithCredentialAsync was canceled!"); } else if (task.IsFaulted) { UnityEngine.Debug.LogError("Firebase - SignInWithCredentialAsync was failed with error: " + task.Exception); } else { UnityEngine.Debug.Log("Firebase - SignInWithCredentialAsync was completed!"); } callback?.Invoke(task); }); }
public IAuthCredential GetCredential(string serverAuthCode) { var credential = PlayGamesAuthProvider.GetCredential(serverAuthCode); return(new AuthCredentialWrapper(credential)); }
public async Task <(IUserAuth, Exception)> Login(ICloudAuthOptions options) { try { if (!_inited) { await new WaitUntil(() => _inited); } if (!_currentUser.IsNull()) { return(_currentUser, null); } var authtype = options.GetOptions <FirebaseCloudAuthType>(FirebaseCloudAuthOptions.LOGIN_TYPE); if (authtype.IsNull() || authtype == FirebaseCloudAuthType.None) { return(null, new ArgumentNullException(nameof(authtype))); } switch (authtype) { case FirebaseCloudAuthType.EmailPassword: { var email = options.GetOptions <string>(FirebaseCloudAuthOptions.EMAIL); var password = options.GetOptions <string>(FirebaseCloudAuthOptions.PASSWORD); if (email.IsNullOrEmpty()) { return(null, new ArgumentNullException(FirebaseCloudAuthOptions.EMAIL, "Email is Empty")); } if (password.IsNullOrEmpty()) { return(null, new ArgumentNullException(FirebaseCloudAuthOptions.PASSWORD, "Password is Empty")); } //Check email registered var providers = await FirebaseAuth.DefaultInstance.FetchProvidersForEmailAsync(email); if (providers.IsNullOrEmpty()) { return(null, new FirebaseException(401, "Not Registered")); } var firebaseUser = await FirebaseAuth.DefaultInstance.SignInWithEmailAndPasswordAsync(email, password); var accessToken = await firebaseUser.TokenAsync(false); _currentUser = new CloudAuthUser() { id = firebaseUser.UserId, authType = CloudAuthType.Firebase, imageUrl = firebaseUser.PhotoUrl?.AbsoluteUri, name = firebaseUser.DisplayName, email = firebaseUser.Email, token = accessToken }; return(_currentUser, null); } #if GOOGLE_AUTH case FirebaseCloudAuthType.GoogleSignIn: { var accessToken = options.GetOptions <string>(FirebaseCloudAuthOptions.TOKEN); var credential = GoogleAuthProvider.GetCredential(accessToken, null); var firebaseUser = await FirebaseAuth.DefaultInstance.SignInWithCredentialAsync(credential); accessToken = await firebaseUser.TokenAsync(false); _currentUser = new CloudAuthUser() { id = firebaseUser.UserId, authType = CloudAuthType.Firebase, imageUrl = firebaseUser.PhotoUrl?.AbsoluteUri, name = firebaseUser.DisplayName, email = firebaseUser.Email, token = accessToken }; return(_currentUser, null); } #endif #if FACEBOOK_AUTH case FirebaseCloudAuthType.Facebook: { var accessToken = options.GetOptions <string>(FirebaseCloudAuthOptions.TOKEN); var credential = FacebookAuthProvider.GetCredential(accessToken); var firebaseUser = await FirebaseAuth.DefaultInstance.SignInWithCredentialAsync(credential); accessToken = await firebaseUser.TokenAsync(false); _currentUser = new CloudAuthUser() { id = firebaseUser.UserId, authType = CloudAuthType.Firebase, imageUrl = firebaseUser.PhotoUrl?.AbsoluteUri, name = firebaseUser.DisplayName, token = accessToken, email = firebaseUser.Email }; return(_currentUser, null); } #endif #if PLAYSERVICE_AUTH case FirebaseCloudAuthType.GooglePlayService: { var accessToken = options.GetOptions <string>(FirebaseCloudAuthOptions.TOKEN); var credential = PlayGamesAuthProvider.GetCredential(accessToken); var firebaseUser = await FirebaseAuth.DefaultInstance.SignInWithCredentialAsync(credential); accessToken = await firebaseUser.TokenAsync(false); _currentUser = new CloudAuthUser() { id = firebaseUser.UserId, authType = CloudAuthType.Firebase, imageUrl = firebaseUser.PhotoUrl?.AbsoluteUri, name = firebaseUser.DisplayName, token = accessToken, email = firebaseUser.Email }; return(_currentUser, null); } #endif default: { return(null, new Exception("Auth Type Currently Not Supported")); } } } catch (FirebaseException ex) { return(null, ex); } }
private void AuthenticationWithGooglePlay() { Credential credential = PlayGamesAuthProvider.GetCredential(credentialUser.code); SignInWithCredentialAsync(credential); }