Example #1
0
        public async Task <AuthResponse> SignIn(SignInModel signInModel, ModelStateDictionary modelState)
        {
            var response     = default(AuthResponse);
            var errorMessage = "Sign in error";

            if (!modelState.IsValid)
            {
                response = AuthResponseError.FromModelStateErrors(modelState, errorMessage);
            }
            else
            {
                var user = await userManager.FindByEmailAsync(signInModel.UserName);

                var errors = default(IEnumerable <string>);

                if (user == null)
                {
                    errors   = GetErrorsFromParams($"User {signInModel.UserName} was not found");
                    response = AuthResponseError.GetNotFoundError(errors, errorMessage);
                }
                else
                {
                    var result = await signInManager.PasswordSignInAsync(user, signInModel.Password, false, false);

                    if (!result.Succeeded)
                    {
                        errors   = GetErrorsFromParams($"Incorrect password");
                        response = AuthResponseError.GetBadRequestError(errors, errorMessage);
                    }
                    else
                    {
                        var token = await generateJwt.CreateToken(user);

                        var authModel = await AuthModel.FromAppUser(user, userManager, token);

                        response = new AuthResponseOk(authModel, "Sign in success");
                    }
                }
            }

            return(response);
        }