Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

onLogin event is never fired (Android) #84

Open
sancilla opened this issue Jan 12, 2022 · 0 comments
Open

onLogin event is never fired (Android) #84

sancilla opened this issue Jan 12, 2022 · 0 comments

Comments

@sancilla
Copy link

sancilla commented Jan 12, 2022

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());
		}
	}

}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant