Example #1
0
        public async Task <UserForReturnTokenDto> Login(UserForLoginDto userForLoginDto)
        {
            var userFromRepo = await userDal.CheckUserForLogin(userForLoginDto.Email);

            if (userFromRepo == null)
            {
                throw new Exception("No registered users with this email address");
            }

            var checkPassword = authHelper.VerifyPasswordHash(userForLoginDto.Password, userFromRepo.PasswordHash, userFromRepo.PasswordSalt);

            if (!checkPassword)
            {
                throw new Exception("Your Email address or password are Incorrect");
            }

            var userToReturn = mapper.Map <UserForReturnTokenDto>(userFromRepo);
            var userRoles    = await userDal.GetUserWithRoles(userFromRepo.Id);

            var userWithTheirRoles = mapper.Map <UserWithRolesDto>(userRoles);

            List <string> userRolesList = new List <string>();

            userRolesList.AddRange(userWithTheirRoles.UserRoles.Select(t => t.RoleName));

            var token = authHelper.GenerateJwtToken(userFromRepo.Id, userFromRepo.UserName, userRolesList);

            if (string.IsNullOrEmpty(token))
            {
                throw new Exception("Jwt token Unable to created.!!");
            }
            userToReturn.Token = token;
            return(userToReturn);
        }