Exemple #1
0
        public async Task <LoginResult> LoginAsync(UserLogin userLogin, bool isRenew = false)
        {
            if (!isRenew)
            {
                ApplicationSettings.Clear();
            }

            var loginApiResponse = await authApi.Post(ApiEndPoints.AUTH_SERVICE_URL, ApiPrefixes.AUTH_PREFIX, "/Token", userLogin);

            if (loginApiResponse.IsSuccess)
            {
                if (LoadTokenInfo(loginApiResponse) == false)
                {
                    return(new LoginResult {
                        IsSuccess = false
                    });
                }

                var cesResponse = await authApi.Get <Common_ES>(
                    ApiEndPoints.API_SERVICE_URL, ApiPrefixes.API_PREFIX, "/Common/se",
                    new
                {
                    SiteId = string.Empty,
                    EleccionIdeleccionId = string.Empty,
                },
                    Token.AccessToken);

                if (!cesResponse.IsSuccess)
                {
                    ApplicationSettings.Clear();
                    return(new LoginResult {
                        IsSuccess = false
                    });
                }

                Token.CommonES = cesResponse.Result;

                // Arma login Result
                var loginResult = new LoginResult();

                loginResult.IsSuccess = true;

                // Instanciar el los settings los datos del login
                ApplicationSettings.LastUserLogin   = userLogin;
                ApplicationSettings.LastAccessToken = Token.AccessToken;

                var inGlobal   = Token.CommonES.InGlobal;
                var inEleccion = Token.CommonES.InEleccion;

                var selectedSiteId       = Token.CommonES.SiteId;
                var selectedSiteName     = Token.CommonES.SiteName;
                var selectedEleccionId   = Token.CommonES.EleccionId;
                var selectedEleccionName = Token.CommonES.EleccionName;


                if (ApplicationSettings.LastLoginStep == LoginStep.LoginOk && isRenew)
                {
                    inGlobal   = ApplicationSettings.InGlobal;
                    inEleccion = ApplicationSettings.InEleccion;

                    selectedSiteId       = ApplicationSettings.SelectedSiteId;
                    selectedSiteName     = ApplicationSettings.SelectedSiteName;
                    selectedEleccionId   = ApplicationSettings.SelectedEleccionId;
                    selectedEleccionName = ApplicationSettings.SelectedEleccionName;
                }

                if (inGlobal)
                {
                    ApplicationSettings.LastLoginStep = LoginStep.SelectSite;
                    loginResult.LoginStepTo           = LoginStep.SelectSite;
                }
                else if (!inEleccion)
                {
                    ApplicationSettings.LastLoginStep = LoginStep.SelectEleccion;
                    loginResult.LoginStepTo           = LoginStep.SelectEleccion;
                }
                else
                {
                    // Okey, esta dentro de una eleccion
                    ApplicationSettings.LastLoginStep = LoginStep.LoginOk;
                    ApplicationSettings.InEleccion    = true;
                    ApplicationSettings.InGlobal      = false;

                    loginResult.LoginStepTo = LoginStep.LoginOk;
                }

                ApplicationSettings.SelectedSiteId       = selectedSiteId;
                ApplicationSettings.SelectedSiteName     = selectedSiteName;
                ApplicationSettings.SelectedEleccionId   = selectedEleccionId;
                ApplicationSettings.SelectedEleccionName = selectedEleccionName;

                IsLoggedIn = true;

                StartRefreshToken();

                return(loginResult);
            }

            ApplicationSettings.Clear();
            IsLoggedIn = false;
            return(new LoginResult
            {
                IsSuccess = false,
            });
        } //LoginAsync