UserSession namespace was changed

This commit is contained in:
Artemy
2025-07-24 21:37:12 +07:00
parent 5a734ecbdc
commit e9eee5bfec
14 changed files with 85 additions and 72 deletions
@@ -4,6 +4,7 @@ using Govor.Application.Interfaces.Infrastructure.Extensions;
using Govor.Application.Interfaces.UserSession;
using Govor.Contracts.DTOs;
using Govor.Core.Models;
using Govor.Core.Models.Users;
using Govor.Data.Repositories.Exceptions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
+8
View File
@@ -0,0 +1,8 @@
using Microsoft.AspNetCore.SignalR;
namespace Govor.API.Hubs;
public class GroupsHub : Hub
{
}
@@ -2,6 +2,7 @@ using AutoFixture;
using Govor.Application.Interfaces.UserSession;
using Govor.Application.Services.UserSessions;
using Govor.Core.Models;
using Govor.Core.Models.Users;
using Govor.Core.Repositories.UserSessionsRepository;
using Govor.Data.Repositories.Exceptions;
using Microsoft.Extensions.Logging;
@@ -1,5 +1,6 @@
using Govor.Application.Services.UserSessions;
using Govor.Core.Models;
using Govor.Core.Models.Users;
using Govor.Core.Repositories.UserSessionsRepository;
using Govor.Data.Repositories.Exceptions;
using Microsoft.Extensions.Logging;
@@ -2,5 +2,5 @@ namespace Govor.Application.Interfaces.UserSession;
public interface IUserSessionReader
{
Task<List<Core.Models.UserSession>> GetAllSessionsAsync(Guid userId);
Task<List<Core.Models.Users.UserSession>> GetAllSessionsAsync(Guid userId);
}
@@ -7,82 +7,83 @@ using Govor.Data.Repositories.Exceptions;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace Govor.Application.Services.UserSessions;
public class UserSessionOpener : IUserSessionOpener
namespace Govor.Application.Services.UserSessions
{
private readonly IUserSessionsRepository _repository;
private readonly ILogger<UserSessionOpener> _logger;
private readonly JwtRefreshOption _options;
private readonly IJwtService _jwtService;
public UserSessionOpener(
IUserSessionsRepository repository,
IJwtService jwtService,
IOptions<JwtRefreshOption> options,
ILogger<UserSessionOpener> logger)
public class UserSessionOpener : IUserSessionOpener
{
_jwtService = jwtService;
_repository = repository;
_logger = logger;
_options = options.Value;
}
private readonly IUserSessionsRepository _repository;
private readonly ILogger<UserSessionOpener> _logger;
private readonly JwtRefreshOption _options;
private readonly IJwtService _jwtService;
public async Task<RefreshResult> OpenSessionAsync(User user, string deviceInfo)
{
_logger.LogInformation($"Opening session for user {user.Id} on device '{deviceInfo}'");
try
public UserSessionOpener(
IUserSessionsRepository repository,
IJwtService jwtService,
IOptions<JwtRefreshOption> options,
ILogger<UserSessionOpener> logger)
{
var sessions = await _repository.GetByUserIdAsync(user.Id);
var session = sessions.FirstOrDefault(s => s.DeviceInfo == deviceInfo);
_jwtService = jwtService;
_repository = repository;
_logger = logger;
_options = options.Value;
}
var newRefreshToken = await _jwtService.GenerateRefreshTokenAsync(user);
var accessToken = await _jwtService.GenerateAccessTokenAsync(user);
public async Task<RefreshResult> OpenSessionAsync(User user, string deviceInfo)
{
_logger.LogInformation($"Opening session for user {user.Id} on device '{deviceInfo}'");
var newExpiresAt = DateTime.UtcNow.AddDays(_options.RefreshTokenLifetimeDays);
if (session is not null)
try
{
// Всегда обновляем токен и дату
session.RefreshToken = newRefreshToken;
session.ExpiresAt = newExpiresAt;
session.CreatedAt = DateTime.UtcNow;
session.IsRevoked = false;
var sessions = await _repository.GetByUserIdAsync(user.Id);
var session = sessions.FirstOrDefault(s => s.DeviceInfo == deviceInfo);
await _repository.UpdateAsync(session);
_logger.LogInformation($"Updated session for user {user.Id} on device '{deviceInfo}'");
var newRefreshToken = await _jwtService.GenerateRefreshTokenAsync(user);
var accessToken = await _jwtService.GenerateAccessTokenAsync(user);
return new RefreshResult(session.RefreshToken, accessToken);
var newExpiresAt = DateTime.UtcNow.AddDays(_options.RefreshTokenLifetimeDays);
if (session is not null)
{
// Всегда обновляем токен и дату
session.RefreshToken = newRefreshToken;
session.ExpiresAt = newExpiresAt;
session.CreatedAt = DateTime.UtcNow;
session.IsRevoked = false;
await _repository.UpdateAsync(session);
_logger.LogInformation($"Updated session for user {user.Id} on device '{deviceInfo}'");
return new RefreshResult(session.RefreshToken, accessToken);
}
return await OpenNewSession();
}
catch (NotFoundByKeyException<Guid> ex)
{
return await OpenNewSession();
}
return await OpenNewSession();
}
catch (NotFoundByKeyException<Guid> ex)
{
return await OpenNewSession();
}
async Task<RefreshResult> OpenNewSession()
{
var newRefreshToken = await _jwtService.GenerateRefreshTokenAsync(user);
var accessToken = await _jwtService.GenerateAccessTokenAsync(user);
var newSession = new Core.Models.UserSession
async Task<RefreshResult> OpenNewSession()
{
UserId = user.Id,
DeviceInfo = deviceInfo,
RefreshToken = newRefreshToken,
CreatedAt = DateTime.UtcNow,
ExpiresAt = DateTime.UtcNow.AddDays(_options.RefreshTokenLifetimeDays),
IsRevoked = false
};
var newRefreshToken = await _jwtService.GenerateRefreshTokenAsync(user);
var accessToken = await _jwtService.GenerateAccessTokenAsync(user);
await _repository.AddAsync(newSession);
var newSession = new UserSession
{
UserId = user.Id,
DeviceInfo = deviceInfo,
RefreshToken = newRefreshToken,
CreatedAt = DateTime.UtcNow,
ExpiresAt = DateTime.UtcNow.AddDays(_options.RefreshTokenLifetimeDays),
IsRevoked = false
};
_logger.LogInformation($"Created new session for user {user.Id} on device '{deviceInfo}'");
await _repository.AddAsync(newSession);
return new RefreshResult(newRefreshToken, accessToken);
_logger.LogInformation($"Created new session for user {user.Id} on device '{deviceInfo}'");
return new RefreshResult(newRefreshToken, accessToken);
}
}
}
}
@@ -1,5 +1,4 @@
using Govor.Application.Interfaces.UserSession;
using Govor.Core.Models;
using Govor.Core.Repositories.UserSessionsRepository;
using Govor.Data.Repositories.Exceptions;
using Microsoft.Extensions.Logging;
@@ -17,7 +16,7 @@ public class UserSessionReader : IUserSessionReader
_logger = logger;
}
public async Task<List<UserSession>> GetAllSessionsAsync(Guid userId)
public async Task<List<Govor.Core.Models.Users.UserSession>> GetAllSessionsAsync(Guid userId)
{
try
{
@@ -1,7 +1,7 @@
using Govor.Application.Interfaces.Authentication;
using Govor.Application.Interfaces.UserSession;
using Govor.Application.Services.Authentication;
using Govor.Core.Models;
using Govor.Core.Models.Users;
using Govor.Core.Repositories.Users;
using Govor.Core.Repositories.UserSessionsRepository;
using Govor.Data.Repositories.Exceptions;
@@ -1,4 +1,4 @@
namespace Govor.Core.Models;
namespace Govor.Core.Models.Users;
public class UserSession
{
@@ -1,4 +1,4 @@
using Govor.Core.Models;
using Govor.Core.Models.Users;
namespace Govor.Core.Repositories.UserSessionsRepository;
@@ -1,4 +1,4 @@
using Govor.Core.Models;
using Govor.Core.Models.Users;
namespace Govor.Core.Repositories.UserSessionsRepository;
@@ -1,4 +1,5 @@
using Govor.Core.Models;
using Govor.Core.Models.Users;
namespace Govor.Core.Repositories.UserSessionsRepository;
@@ -1,5 +1,6 @@
using AutoFixture;
using Govor.Core.Models;
using Govor.Core.Models.Users;
using Govor.Data.Repositories;
using Govor.Data.Repositories.Exceptions;
using Microsoft.EntityFrameworkCore;
@@ -1,5 +1,5 @@
using Govor.Core.Infrastructure.Extensions;
using Govor.Core.Models;
using Govor.Core.Models.Users;
using Govor.Core.Repositories.UserSessionsRepository;
using Govor.Data.Repositories.Exceptions;
using Microsoft.EntityFrameworkCore;