Skip to content

Commit

Permalink
(#421) identity: update user service
Browse files Browse the repository at this point in the history
  • Loading branch information
SaintAngeLs committed Sep 22, 2024
1 parent 97b68d6 commit a735c82
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ public class SignIn : ICommand
public string Email { get; set; }
public string Password { get; set; }
public string DeviceType { get; set; }

public SignIn(string email, string password, string deviceType)
public string IpAddress { get; set; }
public SignIn(string email, string password, string deviceType, string ipAddress)
{
Email = email;
Password = password;
DeviceType = deviceType;
IpAddress = ipAddress;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ public class VerifyTwoFactorCode : ICommand
{
public Guid UserId { get; }
public string Code { get; }
public string DeviceType { get; }
public string IpAddress { get; }

public VerifyTwoFactorCode(Guid userId, string code)
public VerifyTwoFactorCode(Guid userId, string code, string deviceType, string ipAddress)
{
UserId = userId;
Code = code ?? throw new ArgumentNullException(nameof(code));
DeviceType = deviceType ?? throw new ArgumentNullException(nameof(deviceType));
IpAddress = ipAddress ?? throw new ArgumentNullException(nameof(ipAddress));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ public class AuthDto
public Guid UserId { get; set; }
public bool IsOnline { get; set; }
public string DeviceType { get; set; }
public string IpAddress { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public class UserDto
public string DeviceType { get; set; }
public DateTime? LastActive { get; set; }

public string IpAddress { get; set; }

public UserDto() { }

public UserDto(User user)
Expand All @@ -41,6 +43,8 @@ public UserDto(User user)
IsOnline = user.IsOnline;
DeviceType = user.DeviceType;
LastActive = user.LastActive;

IpAddress = user.IpAddress;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@ public class SignedIn : IEvent
{
public Guid UserId { get; }
public Role Role { get; }
public string DeviceType { get; }
public string IpAddress { get; }

public SignedIn(Guid userId, Role role)
public SignedIn(Guid userId, Role role, string deviceType, string ipAddress)
{
UserId = userId;
Role = role;
DeviceType = deviceType;
IpAddress = ipAddress;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,20 +95,24 @@ public async Task<AuthDto> SignInAsync(SignIn command)
claims.Add("permissions", user.Permissions);
}

user.SetOnlineStatus(true, command.DeviceType);
user.SetOnlineStatus(true, command.DeviceType, command.IpAddress);
await _userRepository.UpdateAsync(user);

var auth = _jwtProvider.Create(user.Id, user.Role, claims: claims);
auth.RefreshToken = await _refreshTokenService.CreateAsync(user.Id);

auth.IsOnline = true;
auth.DeviceType = command.DeviceType;
auth.IpAddress = command.IpAddress;
Console.WriteLine($"IPAddress {command.IpAddress}");
Console.WriteLine(JsonSerializer.Serialize(auth));

await _messageBroker.PublishAsync(new SignedIn(user.Id, user.Role));
await _messageBroker.PublishAsync(new SignedIn(user.Id, user.Role, command.DeviceType, command.IpAddress));

return auth;
}


public async Task SignUpAsync(SignUp command)
{
if (!EmailRegex.IsMatch(command.Email))
Expand Down Expand Up @@ -320,23 +324,40 @@ public async Task<AuthDto> VerifyTwoFactorCodeAsync(VerifyTwoFactorCode command)
var auth = _jwtProvider.Create(user.Id, user.Role, claims: claims);
auth.RefreshToken = await _refreshTokenService.CreateAsync(user.Id);

await _messageBroker.PublishAsync(new SignedIn(user.Id, user.Role));
auth.DeviceType = command.DeviceType;
auth.IpAddress = command.IpAddress; // Assuming IpAddress is part of the command

await _messageBroker.PublishAsync(new SignedIn(user.Id, user.Role, command.DeviceType, command.IpAddress));

return auth;
}

public async Task UpdateUserStatusAsync(UpdateUserStatus command)
{
var user = await _userRepository.GetAsync(command.UserId);

if (user == null)
{
_logger.LogError($"User with id {command.UserId} not found.");
throw new UserNotFoundException(command.UserId);
}

user.SetOnlineStatus(command.IsOnline, command.DeviceType);
await _userRepository.UpdateAsync(user);
try
{
user.SetOnlineStatus(command.IsOnline, command.DeviceType);

_logger.LogInformation($"Updated status for user {command.UserId}: Online = {command.IsOnline}, DeviceType = {command.DeviceType}");
user.UpdateLastActive();

await _userRepository.UpdateAsync(user);
}
catch (Exception ex)
{
_logger.LogError($"Error updating status for user {command.UserId}: {ex.Message}");
throw;
}
}



}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public async Task RevokeAsync(string refreshToken)
var user = await _userRepository.GetAsync(token.UserId);
if (user != null)
{
user.SetOnlineStatus(false, null);
// user.SetOnlineStatus(false, null);
await _userRepository.UpdateAsync(user);
}
}
Expand All @@ -68,7 +68,7 @@ public async Task<AuthDto> UseAsync(string refreshToken)

if (user != null)
{
user.SetOnlineStatus(false, null);
// user.SetOnlineStatus(false, null);
await _userRepository.UpdateAsync(user);
}

Expand Down

0 comments on commit a735c82

Please sign in to comment.