Ejemplo n.º 1
0
        public async Task <TokenResponseModel> SignInAsync(UserSignInModel signInModel)
        {
            var user = await _userManager.FindByNameAsync(signInModel.Email);

            if (user is null)
            {
                throw new CustomExeption(Constants.Error.LOGIN_FAILD_NO_USER_WITH_THIS_EMAIL,
                                         StatusCodes.Status400BadRequest);
            }

            var signIn = await _signInManager.PasswordSignInAsync(signInModel.Email, signInModel.Password, false, false);

            if (!signIn.Succeeded)
            {
                throw new CustomExeption(Constants.Error.LOGIN_FAILD_WRONG_PASSWORD, StatusCodes.Status400BadRequest);
            }

            var roleList = await _userManager.GetRolesAsync(user);

            if (roleList is null)
            {
                throw new Exception($"{Constants.Error.ERROR_NO_USERROLE} {StatusCodes.Status500InternalServerError}");
            }

            bool isRoleAdmin = roleList.Any(s => s.Contains("admin"));

            var result = new TokenResponseModel();

            if (isRoleAdmin)
            {
                result.AccessToken = _jwtProvider.GenerateJwt(signInModel.Email, "admin");
                return(result);
            }

            result.AccessToken = _jwtProvider.GenerateJwt(signInModel.Email, "user");
            return(result);
        }