/// <summary> /// Function to handle successful Google Login /// </summary> /// <param name="sender"></param> /// <param name="e"></param> async void OnGoogleAuthCompleted(object sender, AuthenticatorCompletedEventArgs e) { var authenticator = sender as OAuth2Authenticator; if (authenticator != null) { authenticator.Completed -= OnGoogleAuthCompleted; authenticator.Error -= OnGoogleAuthError; } if (e.IsAuthenticated) { DialogsHelper.ProgressDialog.Show(); try { var google_provider_authCredential = Plugin.FirebaseAuth.CrossFirebaseAuth.Current.GoogleAuthProvider.GetCredential(e.Account.Properties["id_token"], e.Account.Properties["access_token"]); var result = await Plugin.FirebaseAuth.CrossFirebaseAuth.Current.Instance.SignInWithCredentialAsync(google_provider_authCredential); await SaveUser(result); } catch (Plugin.FirebaseAuth.FirebaseAuthException ex) { Debug.WriteLine(ex.Message); DialogsHelper.HandleDialogMessage(DialogsHelper.Errors.UndefinedError); } DialogsHelper.ProgressDialog.Hide(); } }
/// <summary> /// Event handler to capture Google Login error /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void OnGoogleAuthError(object sender, AuthenticatorErrorEventArgs e) { Debug.WriteLine("Authentication error: " + e.Message); DialogsHelper.ProgressDialog.Hide(); var authenticator = sender as OAuth2Authenticator; if (authenticator != null) { authenticator.Completed -= OnGoogleAuthCompleted; authenticator.Error -= OnGoogleAuthError; } DialogsHelper.HandleDialogMessage(DialogsHelper.Errors.UndefinedError); }
void LoginWithGoogle() { try { var store = AccountStore.Create(); var account = store.FindAccountsForService(Constants.AppName).FirstOrDefault(); var authenticator = new OAuth2Authenticator( GoogleClientId(), string.Empty, Constants.GoogleScope, new Uri(Constants.GoogleAuthorizeUrl), new Uri(GoogleRedirectUrl()), new Uri(Constants.GoogleAccessTokenUrl), null, true) { AllowCancel = true, Title = "Google Login" }; authenticator.Completed += OnGoogleAuthCompleted; authenticator.Error += OnGoogleAuthError; AuthenticationState.Authenticator = authenticator; var presenter = new Xamarin.Auth.Presenters.OAuthLoginPresenter(); presenter.Login(authenticator); } catch (Exception ex) { Debug.WriteLine(ex.Message); DialogsHelper.HandleDialogMessage(DialogsHelper.Errors.UndefinedError); } }