public async Task AddUserTokenAsync(JwtUserToken userToken)
        {
            if (!_configuration.Value.AllowMultipleLoginsFromTheSameUser)
            {
                await InvalidateUserTokensAsync(userToken.UserId);
            }
            await DeleteTokensWithSameRefreshTokenSourceAsync(userToken.RefreshTokenIdHashSource);

            _tokens.Add(userToken);
        }
        public async Task AddUserTokenAsync(User user, string refreshTokenSerial, string accessToken, string refreshTokenSourceSerial)
        {
            var now   = DateTimeOffset.UtcNow;
            var token = new JwtUserToken
            {
                UserId = user.Id,
                // Refresh token handles should be treated as secrets and should be stored hashed
                RefreshTokenIdHash       = _securityService.GetSha256Hash(refreshTokenSerial),
                RefreshTokenIdHashSource = string.IsNullOrWhiteSpace(refreshTokenSourceSerial) ?
                                           null : _securityService.GetSha256Hash(refreshTokenSourceSerial),
                AccessTokenHash             = _securityService.GetSha256Hash(accessToken),
                RefreshTokenExpiresDateTime = now.AddMinutes(_configuration.Value.RefreshTokenExpirationMinutes),
                AccessTokenExpiresDateTime  = now.AddMinutes(_configuration.Value.AccessTokenExpirationMinutes)
            };

            await AddUserTokenAsync(token);
        }
        public async Task <ActionResult <UserSignUpResponse> > SignUp([FromBody] UserSignUpRequest request)
        {
            _logger.LogInformation($"{nameof(SignUpIdentityController)}: Registration request with email = {request.Email}");

            var result = await _registrationService.RegisterDefaultUserAsync(request.Email, request.Password);

            if (!result)
            {
                _logger.LogInformation($"{nameof(SignUpIdentityController)}: Registration request with email = {request.Email}. Registration service sent an error");

                return(new UserSignUpResponse
                {
                    Email = request.Email,
                    IsRegistrationSuccessful = false,
                    ResponseException =
                        $"User with email {request.Email} is already exists or may be there another problem. Try another email"
                });
            }

            _logger.LogInformation($"{nameof(SignUpIdentityController)}: successful registration for user with email = {request.Email}");

            var token = new JwtUserToken(_jwtConfiguration)
            {
                Email = request.Email,
                Role  = ApplicationRoles.DefaultUser
            };

            return(new UserSignUpResponse
            {
                Email = request.Email,
                IsRegistrationSuccessful = true,
                ResponseException = null,
                Role = ApplicationRoles.DefaultUser,
                JwtToken = token.GetToken()
            });
        }
        public async Task <ActionResult <UserLoginResponse> > SignIn([FromBody] UserSignInRequest request)
        {
            var user =
                await _authenticationService.FindUserByEmailOrLoginAndPasswordAsync(request.LoginOrEmail,
                                                                                    request.Password);

            if (user is null)
            {
                _logger.LogWarning($"{nameof(SignInIdentityController)}: user with email or login = {request.LoginOrEmail} and wrote password not exist.");

                return(new UserLoginResponse
                {
                    IsUserExists = false,
                    IsUserActive = false,
                    IsUserBlocked = false,
                    MetaInfo =
                        $"User not found with request credentials. {nameof(request.LoginOrEmail)}:{request.LoginOrEmail}"
                });
            }

            _logger.LogInformation($"{nameof(SignInIdentityController)}: user with email or login = {request.LoginOrEmail} entered the service");
            await _usersEntrancesService.LogAsync(user.Id);

            var token = new JwtUserToken(_jwtConfiguration)
            {
                Email = user.Email,
                Role  = user.Role.RoleTitle
            };

            var userLoginResponse = _mapper.Map <UserEntityReadDto, UserLoginResponse>(user);

            userLoginResponse.IsUserExists = true;
            userLoginResponse.JwtToken     = token.GetToken();

            return(userLoginResponse);
        }