Exemplo n.º 1
0
        public void SignIn()
        {
            verifyInit();
            Task.Run(async() =>
            {
                try
                {
                    var gsoBuilder = new GoogleSignInOptions.Builder(GoogleSignInOptions.DefaultSignIn)
                                     .RequestIdToken(_clientId)
                                     .RequestEmail();

                    var gso          = gsoBuilder.Build();
                    _googleApiClient = new GoogleApiClient.Builder(_context)
                                       .AddApi(Auth.GOOGLE_SIGN_IN_API, gso)
                                       .Build();
                    _googleApiClient.Connect();

                    if (signInResultSource != null && !signInResultSource.Task.IsCompleted)
                    {
                        signInResultSource.TrySetCanceled();
                    }

                    signInResultSource = new TaskCompletionSource <GoogleSignInResult>();

                    var signInIntent = Auth.GoogleSignInApi.GetSignInIntent(_googleApiClient);
                    _context.StartActivityForResult(signInIntent, RC_SIGN_IN);
                    var result = await signInResultSource.Task;
                    if (result == null || result.Status.IsCanceled || result.Status.IsInterrupted)
                    {
                        _callbackDelegate.OnConnectionFailed("Connection cancelled");
                        return;
                    }

                    if (!result.IsSuccess)
                    {
                        _callbackDelegate.OnConnectionFailed(result.Status.StatusMessage);
                        return;
                    }
                    _accountName = result.SignInAccount?.DisplayName;
                    _idToken     = result.SignInAccount?.IdToken;

                    _callbackDelegate.OnConnectionSucceeded();
                }
                catch (Exception ex)
                {
                    _callbackDelegate.OnConnectionFailed(ex.Message);
                }
            });
        }
Exemplo n.º 2
0
        public override void DidSignInForUser(GIDSignIn signIn, GIDGoogleUser user, NSError error)
        {
            if (error == null)
            {
                _idToken     = user.Authentication.IdToken;
                _accountName = user.Profile.Name;

                if (_callbackDelegate != null)
                {
                    _callbackDelegate.OnConnectionSucceeded();
                }
            }
            else
            {
                if (_callbackDelegate != null)
                {
                    _callbackDelegate.OnConnectionFailed(error.Description);
                }
            }
        }