示例#1
0
        private async void LoginAsync(string facebookToken = null)
        {
            try
            {
                IsBusy = true;

                LoginResponseModel result;

                if (!string.IsNullOrEmpty(facebookToken))
                {
                    result = await authenticationService.LoginFacebookAsync(facebookToken);
                }
                else
                {
                    result = await authenticationService.LoginAsync(Email, Password);
                }

                if (result.IsSuccess)
                {
                    if (!string.IsNullOrEmpty(result.Jwt.AuthToken))
                    {
                        var token = result.Jwt.AuthToken;
                        requestProvider.InitWithAuthorizationToken(token);
                        Settings.Token    = token;
                        Settings.UserName = result.Jwt.UserName;
                        Settings.UserID   = result.Jwt.ID;

                        NavigationService.IsCompany = await authenticationService.GetIsCompany();
                    }

                    var userInfo = await userService.GetUserNameWithPhoto();

                    if (userInfo.IsSuccess)
                    {
                        Settings.UserProfileImage = userInfo.Photo;
                    }

                    IsBusy = false;

                    await NavigationService.NavigateToAsync <MainViewModel>();

                    NavigationService.ClearBackStack();
                }
                else
                {
                    await NavigationService.DisplayAlert("Error", result.Message, "Ok");
                }
            }
            catch (Exception ex)
            {
                await NavigationService.DisplayAlert("Error", ex.Message, "Cancel");
            }
            finally
            {
                IsBusy = false;
            }
        }
        private async void TryLogin(bool goToEmail)
        {
            try
            {
                if (userModel == null)
                {
                    return;
                }

                IsBusy = true;

                var result = await authenticationService.LoginAsync(userModel.Email, userModel.Password);

                if (result.IsSuccess)
                {
                    if (!string.IsNullOrWhiteSpace(result.Jwt.AuthToken))
                    {
                        Settings.ClearSettings();

                        Settings.Token            = result.Jwt.AuthToken;
                        Settings.UserName         = result.Jwt.UserName;
                        Settings.UserProfileImage = userModel.UserProfileImage;
                        Settings.UserID           = result.Jwt.ID;
                        requestProvider.InitWithAuthorizationToken(result.Jwt.AuthToken);
                        NavigationService.IsCompany = await authenticationService.GetIsCompany();
                    }

                    IsBusy = false;

                    await NavigationService.NavigateToAsync <MainViewModel>();

                    NavigationService.ClearBackStack();
                }
                else
                {
                    if (!goToEmail || !platformService.GoToInbox())
                    {
                        await NavigationService.DisplayAlert("Error", "Please check your email and confirm registration", "Ok");
                    }
                }
            }
            catch (Exception ex)
            {
                await NavigationService.DisplayAlert("Error", ex.Message, "Ok");
            }
            finally
            {
                IsBusy = false;
            }
        }
        private async void LoginFacebookAsync(string facebookToken)
        {
            try
            {
                IsBusy = true;

                var result = await authenticationService.LoginFacebookAsync(facebookToken);


                if (result.IsSuccess)
                {
                    if (!string.IsNullOrEmpty(result.Jwt.AuthToken))
                    {
                        var token = result.Jwt.AuthToken;
                        requestProvider.InitWithAuthorizationToken(token);
                        Settings.Token    = token;
                        Settings.UserName = result.Jwt.UserName;
                        Settings.UserID   = result.Jwt.ID;

                        NavigationService.IsCompany = await authenticationService.GetIsCompany();
                    }

                    IsBusy = false;

                    await NavigationService.NavigateToAsync <MainViewModel>();

                    NavigationService.ClearBackStack();
                }
                else
                {
                    await NavigationService.DisplayAlert("Error", result.Message, "Ok");
                }
            }
            catch (Exception ex)
            {
                await NavigationService.DisplayAlert("Error", "Error sign up via Facebook. Try sign up using Email.", "Cancel");
            }
            finally
            {
                IsBusy = false;
            }
        }
        public async Task LoadUserInfo()
        {
            if (string.IsNullOrWhiteSpace(Settings.Token))
            {
                await Task.Delay(1);

                await NavigationService.NavigateToAsync <LoginViewModel>();

                return;
            }

            try
            {
                IsBusy = true;

                requestProvider.InitWithAuthorizationToken(Settings.Token);

                var result = await userService.GetUserNameWithPhoto();

                if (result.IsSuccess)
                {
                    Settings.UserName         = result.UserName;
                    Settings.UserProfileImage = result.Photo;
                }

                UpdateUI();
            }
            catch (ServiceAuthenticationException ex)
            {
                await NavigationService.NavigateToAsync <LoginViewModel>();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                await NavigationService.DisplayAlert("Error", "Error loading. Check your internet connection and try again.", "Ok");
            }
            finally
            {
                IsBusy = false;
            }
        }