mirror of
https://github.com/Govor-team/Govor.git
synced 2026-07-21 11:44:56 +00:00
UserSession namespace was changed
This commit is contained in:
@@ -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;
|
||||||
|
|||||||
@@ -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,83 +7,84 @@ 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);
|
|
||||||
|
|
||||||
if (session is not null)
|
try
|
||||||
{
|
{
|
||||||
// Всегда обновляем токен и дату
|
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);
|
||||||
|
|
||||||
|
var newExpiresAt = DateTime.UtcNow.AddDays(_options.RefreshTokenLifetimeDays);
|
||||||
|
|
||||||
return new RefreshResult(session.RefreshToken, accessToken);
|
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();
|
|
||||||
}
|
|
||||||
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,
|
return await OpenNewSession();
|
||||||
DeviceInfo = deviceInfo,
|
}
|
||||||
RefreshToken = newRefreshToken,
|
|
||||||
CreatedAt = DateTime.UtcNow,
|
async Task<RefreshResult> OpenNewSession()
|
||||||
ExpiresAt = DateTime.UtcNow.AddDays(_options.RefreshTokenLifetimeDays),
|
{
|
||||||
IsRevoked = false
|
var newRefreshToken = await _jwtService.GenerateRefreshTokenAsync(user);
|
||||||
};
|
var accessToken = await _jwtService.GenerateAccessTokenAsync(user);
|
||||||
|
|
||||||
await _repository.AddAsync(newSession);
|
|
||||||
|
|
||||||
_logger.LogInformation($"Created new session for user {user.Id} on device '{deviceInfo}'");
|
var newSession = new UserSession
|
||||||
|
{
|
||||||
|
UserId = user.Id,
|
||||||
|
DeviceInfo = deviceInfo,
|
||||||
|
RefreshToken = newRefreshToken,
|
||||||
|
CreatedAt = DateTime.UtcNow,
|
||||||
|
ExpiresAt = DateTime.UtcNow.AddDays(_options.RefreshTokenLifetimeDays),
|
||||||
|
IsRevoked = false
|
||||||
|
};
|
||||||
|
|
||||||
return new RefreshResult(newRefreshToken, accessToken);
|
await _repository.AddAsync(newSession);
|
||||||
|
|
||||||
|
_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;
|
||||||
|
|||||||
Reference in New Issue
Block a user