Example #1
0
        public LogingPageViewModel(ILoginServices loginServices, INavigationService navigationService)
            : base(navigationService)
        {
            _loginServices    = loginServices;
            Title             = "Login";
            UserName          = new ReactiveProperty <string>("*****@*****.**").SetValidateAttribute(() => UserName);
            Password          = new ReactiveProperty <string>("2bad4you").SetValidateAttribute(() => Password);
            IsLoading         = new ReactiveProperty <bool>(false);
            CanLogin          = new ReactiveProperty <bool>(true, ReactivePropertyMode.RaiseLatestValueOnSubscribe);
            LoginErrorMessage = new ReactiveProperty <string>();

            UserErrorMessage = UserName.ObserveErrorChanged
                               .Select(x => x?.OfType <string>()?.FirstOrDefault())
                               .ToReactiveProperty();

            PasswordErrorMessage = Password.ObserveErrorChanged
                                   .Select(x => x?.OfType <string>()?.FirstOrDefault())
                                   .ToReactiveProperty();

            this.CanLogin = new[]
            {
                this.UserName.ObserveHasErrors,
                this.Password.ObserveHasErrors
            }
            .CombineLatest(x => !x.Any(y => y))
            .ToReactiveProperty(mode: ReactivePropertyMode.RaiseLatestValueOnSubscribe);

            this.LoginCommand = new AsyncReactiveCommand(CanLogin)
                                .WithSubscribe
                                    (async() => {
                this.IsLoading.Value = true;
                var loginResult      = await _loginServices.LoginAsync(UserName.Value, Password.Value);
                this.IsLoading.Value = false;
                if (loginResult)
                {
                    await this.NavigationService.NavigateAsync("NavigationPage/PalletesPage", useModalNavigation: true);
                }
                else
                {
                    LoginErrorMessage.Value = "Can't login, please verify yor credentials.";
                }
            });
        }
Example #2
0
        public async Task <IActionResult> Login(LoginViewModel login)
        {
            if (ModelState.IsValid)
            {
                var isLogin = await _loginServices.LoginAsync(login.Email, login.Password);

                if (isLogin)
                {
                    var user = await _userServices.GetEmail(login.Email);

                    var role = "";
                    if (user.Role == 1)
                    {
                        role = "Admin";
                    }
                    else if (user.Role == 2)
                    {
                        role = "User";
                    }
                    var claims = new List <Claim>
                    {
                        new Claim(ClaimTypes.Name, user.Email),
                        new Claim("FullName", user.FullName),
                        new Claim("Email", user.Email),
                        new Claim(ClaimTypes.Role, role)
                    };
                    var claimsIdentity = new ClaimsIdentity(
                        claims, CookieAuthenticationDefaults.AuthenticationScheme);
                    var authProperties = new AuthenticationProperties();
                    await HttpContext.SignInAsync(
                        CookieAuthenticationDefaults.AuthenticationScheme,
                        new ClaimsPrincipal(claimsIdentity),
                        authProperties);

                    return(RedirectToAction("Index", "Store"));
                }
                ViewData["Error"] = _userLocalizer.GetLocalizedHtmlString("err_LoginFailure");
                return(View(login));
            }
            Log.Error("Login Error");
            return(View(login));
        }