예제 #1
0
        /// <summary>
        /// Logs in or registers a user whose email has not already been registered.
        /// If successful, calls the UserLoggedIn event handler and sets the user in ApiHelper
        /// </summary>
        /// <param name="info">Information needed by the api to log a user in</param>
        /// <returns></returns>
        public static async void LoginOrRegisterAsync()
        {
            AuthenticationResult result = null;

            try
            {
                PublicClientApplication pca = App.AuthClient;
                result = await pca.AcquireTokenAsync(Globals.DefaultScopes);

                ApiHelper.setToken(result.AccessToken);

                LoginInitiated?.Invoke(DateTime.Now, null);

                Models.User user = await ApiHelper.GetAsync <Models.User>("User");

                if (user == null || user.Email == null)
                {
                    user = await ApiHelper.PostAsync <Models.User>("User");
                }



                if (user != null && !String.IsNullOrWhiteSpace(user.Email))
                {
                    UserLoggedIn?.Invoke(DateTime.Now, user);
                }
                else
                {
                    AuthError?.Invoke(DateTime.Now, "No connection to server.");
                }
            }
            catch (MsalException ex)
            {
                if (ex.ErrorCode != "authentication_canceled")
                {
                    string message = ex.Message;
                    if (ex.InnerException != null)
                    {
                        message += "\nInner Exception: " + ex.InnerException.Message;
                    }

                    AuthError?.Invoke(DateTime.Now, message);
                }
            }
            catch (System.Net.Http.HttpRequestException ex)
            {
                // Launch warning popup
                ContentDialog notLoggedInDialog = new ContentDialog()
                {
                    Title             = "Could not login",
                    Content           = "Please check your wifi signal.",
                    PrimaryButtonText = "Ok"
                };
                await ContentDialogHelper.CreateContentDialogAsync(notLoggedInDialog, true);
            }
        }
예제 #2
0
        private async void LoginButton_Click(object sender, RoutedEventArgs e)
        {
            if (this.UserDetails != null)
            {
                if (FlyoutBase.GetAttachedFlyout(_loginButton) is FlyoutBase flyout)
                {
                    flyout.ShowAt(_loginButton);
                }
            }
            else
            {
                var cargs = new CancelEventArgs();
                LoginInitiated?.Invoke(this, cargs);

                if (!cargs.Cancel)
                {
                    await LoginAsync();
                }
            }
        }