Esempio n. 1
0
        public async Task <Token> RefreshTokenLoginAsync(string refreshToken)
        {
            User user = await _userRepository.GetWhereAsync(x => x.RefreshToken == refreshToken);

            if (user != null && user?.RefreshTokenEndDate > DateTime.Now)
            {
                JwtTokenHandler tokenHandler = new JwtTokenHandler();
                Token           token        = tokenHandler.CreateAccessToken(user);

                user.RefreshToken        = token.RefreshToken;
                user.RefreshTokenEndDate = token.Expiration.AddMinutes(3);
                await _userRepository.UpdateAsync(user);

                return(token);
            }
            return(null);
        }
Esempio n. 2
0
        public async Task <BaseResponseDto <UserDto> > Login(AuthDto auth)
        {
            try
            {
                var user = await GetByEmailAsync(auth.Email);

                BaseResponseDto <UserDto> userResponse = new BaseResponseDto <UserDto>();
                if (user == null)
                {
                    userResponse.Errors.Add("Email", "Enail is wrong");
                    return(userResponse);
                }

                var    locker          = CreateLockerInstance();
                string decrpytPassword = locker.Decrypt(user.Password);
                if (decrpytPassword != auth.Password)
                {
                    userResponse.Errors.Add("Password", "Password is wrong");
                    return(userResponse);
                }

                JwtTokenHandler tokenHandler = new JwtTokenHandler();
                var             token        = tokenHandler.CreateAccessToken(user);
                user.AccessToken         = token.AccessToken;
                user.RefreshToken        = token.RefreshToken;
                user.RefreshTokenEndDate = token.Expiration.AddDays(1);
                UserDto userDto = new UserDto(user.Id, user.FirstName, user.LastName, user.Address, user.AccessToken, user.Role);
                userResponse.Data = userDto;
                await _userRepository.UpdateAsync(user);

                return(userResponse);
            }
            catch (Exception)
            {
                throw;
            }
        }