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.Application.Interfaces.UserSession;
using Govor.Contracts.DTOs; using Govor.Contracts.DTOs;
using Govor.Core.Models; using Govor.Core.Models;
using Govor.Core.Models.Users;
using Govor.Data.Repositories.Exceptions; using Govor.Data.Repositories.Exceptions;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging; 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.Interfaces.UserSession;
using Govor.Application.Services.UserSessions; using Govor.Application.Services.UserSessions;
using Govor.Core.Models; using Govor.Core.Models;
using Govor.Core.Models.Users;
using Govor.Core.Repositories.UserSessionsRepository; using Govor.Core.Repositories.UserSessionsRepository;
using Govor.Data.Repositories.Exceptions; using Govor.Data.Repositories.Exceptions;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
@@ -1,5 +1,6 @@
using Govor.Application.Services.UserSessions; using Govor.Application.Services.UserSessions;
using Govor.Core.Models; using Govor.Core.Models;
using Govor.Core.Models.Users;
using Govor.Core.Repositories.UserSessionsRepository; using Govor.Core.Repositories.UserSessionsRepository;
using Govor.Data.Repositories.Exceptions; using Govor.Data.Repositories.Exceptions;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
@@ -2,5 +2,5 @@ namespace Govor.Application.Interfaces.UserSession;
public interface IUserSessionReader 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.Logging;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
namespace Govor.Application.Services.UserSessions; namespace Govor.Application.Services.UserSessions
public class UserSessionOpener : IUserSessionOpener
{ {
private readonly IUserSessionsRepository _repository; public class UserSessionOpener : IUserSessionOpener
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)
{ {
_jwtService = jwtService; private readonly IUserSessionsRepository _repository;
_repository = repository; private readonly ILogger<UserSessionOpener> _logger;
_logger = logger; private readonly JwtRefreshOption _options;
_options = options.Value; private readonly IJwtService _jwtService;
}
public async Task<RefreshResult> OpenSessionAsync(User user, string deviceInfo) public UserSessionOpener(
{ IUserSessionsRepository repository,
_logger.LogInformation($"Opening session for user {user.Id} on device '{deviceInfo}'"); IJwtService jwtService,
IOptions<JwtRefreshOption> options,
try ILogger<UserSessionOpener> logger)
{ {
var sessions = await _repository.GetByUserIdAsync(user.Id); _jwtService = jwtService;
var session = sessions.FirstOrDefault(s => s.DeviceInfo == deviceInfo); _repository = repository;
_logger = logger;
_options = options.Value;
}
var newRefreshToken = await _jwtService.GenerateRefreshTokenAsync(user); public async Task<RefreshResult> OpenSessionAsync(User user, string deviceInfo)
var accessToken = await _jwtService.GenerateAccessTokenAsync(user); {
_logger.LogInformation($"Opening session for user {user.Id} on device '{deviceInfo}'");
var newExpiresAt = DateTime.UtcNow.AddDays(_options.RefreshTokenLifetimeDays); try
if (session is not null)
{ {
// Всегда обновляем токен и дату var sessions = await _repository.GetByUserIdAsync(user.Id);
session.RefreshToken = newRefreshToken; var session = sessions.FirstOrDefault(s => s.DeviceInfo == deviceInfo);
session.ExpiresAt = newExpiresAt;
session.CreatedAt = DateTime.UtcNow;
session.IsRevoked = false;
await _repository.UpdateAsync(session); var newRefreshToken = await _jwtService.GenerateRefreshTokenAsync(user);
_logger.LogInformation($"Updated session for user {user.Id} on device '{deviceInfo}'"); 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(); async Task<RefreshResult> 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
{ {
UserId = user.Id, var newRefreshToken = await _jwtService.GenerateRefreshTokenAsync(user);
DeviceInfo = deviceInfo, var accessToken = await _jwtService.GenerateAccessTokenAsync(user);
RefreshToken = newRefreshToken,
CreatedAt = DateTime.UtcNow,
ExpiresAt = DateTime.UtcNow.AddDays(_options.RefreshTokenLifetimeDays),
IsRevoked = false
};
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.Application.Interfaces.UserSession;
using Govor.Core.Models;
using Govor.Core.Repositories.UserSessionsRepository; using Govor.Core.Repositories.UserSessionsRepository;
using Govor.Data.Repositories.Exceptions; using Govor.Data.Repositories.Exceptions;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
@@ -17,7 +16,7 @@ public class UserSessionReader : IUserSessionReader
_logger = logger; _logger = logger;
} }
public async Task<List<UserSession>> GetAllSessionsAsync(Guid userId) public async Task<List<Govor.Core.Models.Users.UserSession>> GetAllSessionsAsync(Guid userId)
{ {
try try
{ {
@@ -1,7 +1,7 @@
using Govor.Application.Interfaces.Authentication; using Govor.Application.Interfaces.Authentication;
using Govor.Application.Interfaces.UserSession; using Govor.Application.Interfaces.UserSession;
using Govor.Application.Services.Authentication; using Govor.Application.Services.Authentication;
using Govor.Core.Models; using Govor.Core.Models.Users;
using Govor.Core.Repositories.Users; using Govor.Core.Repositories.Users;
using Govor.Core.Repositories.UserSessionsRepository; using Govor.Core.Repositories.UserSessionsRepository;
using Govor.Data.Repositories.Exceptions; using Govor.Data.Repositories.Exceptions;
@@ -1,4 +1,4 @@
namespace Govor.Core.Models; namespace Govor.Core.Models.Users;
public class UserSession public class UserSession
{ {
@@ -1,4 +1,4 @@
using Govor.Core.Models; using Govor.Core.Models.Users;
namespace Govor.Core.Repositories.UserSessionsRepository; namespace Govor.Core.Repositories.UserSessionsRepository;
@@ -1,4 +1,4 @@
using Govor.Core.Models; using Govor.Core.Models.Users;
namespace Govor.Core.Repositories.UserSessionsRepository; namespace Govor.Core.Repositories.UserSessionsRepository;
@@ -1,4 +1,5 @@
using Govor.Core.Models; using Govor.Core.Models;
using Govor.Core.Models.Users;
namespace Govor.Core.Repositories.UserSessionsRepository; namespace Govor.Core.Repositories.UserSessionsRepository;
@@ -1,5 +1,6 @@
using AutoFixture; using AutoFixture;
using Govor.Core.Models; using Govor.Core.Models;
using Govor.Core.Models.Users;
using Govor.Data.Repositories; using Govor.Data.Repositories;
using Govor.Data.Repositories.Exceptions; using Govor.Data.Repositories.Exceptions;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
@@ -1,5 +1,5 @@
using Govor.Core.Infrastructure.Extensions; using Govor.Core.Infrastructure.Extensions;
using Govor.Core.Models; using Govor.Core.Models.Users;
using Govor.Core.Repositories.UserSessionsRepository; using Govor.Core.Repositories.UserSessionsRepository;
using Govor.Data.Repositories.Exceptions; using Govor.Data.Repositories.Exceptions;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;