GoogleClientPlugin icon indicating copy to clipboard operation
GoogleClientPlugin copied to clipboard

onLogin event is never fired (Android)

Open sancilla opened this issue 4 years ago • 0 comments

Downloaded the sample, it works...

Trying to move to Xamarin.Forms, google.json is OK, the Debug SHA-1 key is added.

According to the SocialMediaAuthenticationSample, I have my userLoginDelegate, the _googleService.LoginAsync() is triggered but nothing happens.

I am almost sure I have missed something... Anyone has a hint for me?

Here is my ViewModel class:

public class LoginViewModel
{
	public ICommand OnLoginCommand { get; set; }

	IGoogleClientManager _googleService = CrossGoogleClient.Current;

	public readonly bool _Debug = true;

	public ObservableCollection<AuthNetwork> AuthenticationNetworks { get; set; } = new ObservableCollection<AuthNetwork>()
	{
		new AuthNetwork()
		{
			Name = "Google",
			Icon = "ic_google",
			Foreground = "#000000",
			Background = "#C0C0C0"
		}
	};

	public LoginViewModel()
	{
		OnLoginCommand = new Command<AuthNetwork>(async (data) => await LoginAsync(data));
	}

	async Task LoginAsync(AuthNetwork authNetwork)
	{
		switch (authNetwork.Name)
		{
			case "Google":
				await LoginGoogleAsync(authNetwork);
				break;
		}
	}

	async Task LoginGoogleAsync(AuthNetwork authNetwork)
	{
		try
		{
			if (_googleService.IsLoggedIn)
			{
				_googleService.Logout();
			}

			EventHandler<GoogleClientResultEventArgs<GoogleUser>> userLoginDelegate = null;
			userLoginDelegate = async (object sender, GoogleClientResultEventArgs<GoogleUser> e) =>
			{
				switch (e.Status)
				{
					case GoogleActionStatus.Completed:
						await App.Current.MainPage.DisplayAlert("Login Google", "Success!", "Ok");
						if (_Debug)
						{
							var googleUserString = JsonConvert.SerializeObject(e.Data);
							Debug.WriteLine($"Google Logged in succesfully: {googleUserString}");
						}

						var socialLoginData = new NetworkAuthData
						{
							Id = e.Data.Id,
							Logo = authNetwork.Icon,
							Foreground = authNetwork.Foreground,
							Background = authNetwork.Background,
							Picture = e.Data.Picture.AbsoluteUri,
							Name = e.Data.Name
						};

						// navigate to content


						await App.Current.MainPage.DisplayAlert("Google Auth", "Success", "Ok");

						break;

					case GoogleActionStatus.Canceled:
						await App.Current.MainPage.DisplayAlert("Google Auth", "Canceled", "Ok");
						break;

					case GoogleActionStatus.Error:
						await App.Current.MainPage.DisplayAlert("Google Auth", "Error", "Ok");
						break;

					case GoogleActionStatus.Unauthorized:
						await App.Current.MainPage.DisplayAlert("Google Auth", "Unauthorized", "Ok");
						break;

					default:
						await App.Current.MainPage.DisplayAlert("Google Auth", "Unknown state!", "Ok");
						break;
				}

				_googleService.OnLogin -= userLoginDelegate;
			};

			_googleService.OnLogin += userLoginDelegate;

			await _googleService.LoginAsync();

		}
		catch (Exception ex)
		{
			Debug.WriteLine("*** SWX *** EXCEPTION\r\n");
			Debug.WriteLine(ex.ToString());
		}
	}

}

sancilla avatar Jan 12 '22 13:01 sancilla