Exemplo n.º 1
0
        /// <summary>
        /// This is the go-to method to handle the authorization and sign-in OAuth flow.
        /// It tries to fetch the google auth token from cache, and if it can't it will try to
        /// fetch it using a refresh token, if that's not possible it will prompt the user to
        /// authorize the application resulting in a google auth token.
        /// It will then use this google auth token to try to sign into firebase, and if that
        /// doesn't work the first time it will retry a second time after having the using re-authorize
        /// the application. (It does this since the refresh token could have been revoked.)
        /// </summary>
        /// <returns><c>true</c> if sign in was successful, <c>false</c> otherwise.</returns>
        public async Task <bool> SignInPromptUserIfNecessary()
        {
            try
            {
                string googleToken;

                using (new DisposableLogger(() => AuthLog.GetAccessTokenBegin("Google"), (sw) => AuthLog.GetAccessTokenEnd(sw, "Google", !string.IsNullOrEmpty(GoogleClient.accessToken))))
                {
                    // Try to fetch google token without prompting the user (authorizing).
                    if (await GoogleClient.Client.GetAccessTokenWithoutAuthentication())
                    {
                        googleToken = GoogleClient.accessToken;
                    }
                    else // Prompt the user to authorize the application.
                    {
                        googleToken = await AuthModal.AuthorizeAndGetGoogleAccessTokenAsync();
                    }
                }

                var firebaseSignInSuccess = false;
                using (new DisposableLogger(() => AuthLog.GetAccessTokenBegin("Firebase"), (sw) => AuthLog.GetAccessTokenEnd(sw, "Firebase", firebaseSignInSuccess)))
                {
                    // Try to sign into firebase with the googleToken.
                    firebaseSignInSuccess = await SignInWithFirebaseAsync(googleToken);
                }
                if (!firebaseSignInSuccess)
                {
                    // Could not log into firebase. Could be caused by a refresh token revocation, try re-authenticating with Google.
                    googleToken = await AuthModal.AuthorizeAndGetGoogleAccessTokenAsync();

                    using (new DisposableLogger(() => AuthLog.GetAccessTokenBegin("Firebase"), (sw) => AuthLog.GetAccessTokenEnd(sw, "Firebase", firebaseSignInSuccess)))
                    {
                        // Retry firebase login.
                        firebaseSignInSuccess = await SignInWithFirebaseAsync(googleToken);

                        // Return result.
                        return(firebaseSignInSuccess);
                    }
                }

                return(firebaseSignInSuccess);
            }
            catch (Exception e)
            {
                // Log Exception.
                LifecycleLog.Exception(e);
                return(false);
            }
        }
        private async void Auth_Tapped(object sender, TappedRoutedEventArgs e)
        {
            SignInButton.IsEnabled = false;
            var signInAuthResult = false;

            // Time and log signing in and authentication.
            using (new DisposableLogger(() => AuthLog.GetAccessTokenBegin("Google"), (sw) => AuthLog.GetAccessTokenEnd(sw, "Google", signInAuthResult)))
            {
                signInAuthResult = await GoogleClient.Client.SignInAuthenticate();
            }

            if (signInAuthResult)
            {
                if (_authCompletionSource.TrySetResult(GoogleClient.accessToken))
                {
                    ShowAuth(false);
                }
            }
            SignInButton.IsEnabled = true;
        }