2026-07-22 20:05:59 +00:00
|
|
|
using Heimdall.Application.Services;
|
|
|
|
|
using Microsoft.AspNetCore.Identity;
|
|
|
|
|
|
2026-11-04 11:37:53 +01:00
|
|
|
using Heimdall.Infrastructure.Identity;
|
2026-07-22 20:05:59 +00:00
|
|
|
namespace Heimdall.Infrastructure.Security;
|
|
|
|
|
|
|
|
|
|
public class AuthSignInService : IAuthSignInService
|
|
|
|
|
{
|
2026-11-04 11:37:53 +01:00
|
|
|
private readonly SignInManager<IdentityAppUser> _signInManager;
|
2026-07-22 20:05:59 +00:00
|
|
|
|
2026-11-04 11:37:53 +01:00
|
|
|
public AuthSignInService(SignInManager<IdentityAppUser> signInManager)
|
2026-07-22 20:05:59 +00:00
|
|
|
{
|
|
|
|
|
_signInManager = signInManager;
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-04 11:37:53 +01:00
|
|
|
public async Task<SignInServiceResult> PasswordSignInAsync(IdentityAccount account, string password, bool lockoutOnFailure)
|
2026-07-22 20:05:59 +00:00
|
|
|
{
|
2026-11-04 11:37:53 +01:00
|
|
|
var user = await _signInManager.UserManager.FindByIdAsync(account.Id.ToString()).ConfigureAwait(false);
|
|
|
|
|
if (user is null) return new SignInServiceResult(SignInResultType.Failed);
|
2026-10-20 07:21:28 +02:00
|
|
|
var result = await _signInManager.CheckPasswordSignInAsync(user, password, lockoutOnFailure).ConfigureAwait(false);
|
|
|
|
|
if (result.IsLockedOut)
|
|
|
|
|
await _signInManager.UserManager.UpdateSecurityStampAsync(user).ConfigureAwait(false);
|
2026-07-22 20:05:59 +00:00
|
|
|
return result switch
|
|
|
|
|
{
|
|
|
|
|
{ Succeeded: true } => new SignInServiceResult(SignInResultType.Success),
|
|
|
|
|
{ IsLockedOut: true } => new SignInServiceResult(SignInResultType.LockedOut),
|
|
|
|
|
{ RequiresTwoFactor: true } => new SignInServiceResult(SignInResultType.RequiresTwoFactor),
|
|
|
|
|
_ => new SignInServiceResult(SignInResultType.Failed)
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SignOutAsync()
|
|
|
|
|
{
|
2026-07-27 18:59:12 +00:00
|
|
|
await _signInManager.SignOutAsync().ConfigureAwait(false);
|
2026-07-22 20:05:59 +00:00
|
|
|
}
|
|
|
|
|
}
|