71 lines
2.4 KiB
C#
71 lines
2.4 KiB
C#
using System.Security.Claims;
|
|
using FluentResults;
|
|
using Microsoft.AspNetCore.Identity;
|
|
using NexusReader.Application.Abstractions.Services;
|
|
using NexusReader.Application.DTOs.User;
|
|
using NexusReader.Domain.Entities;
|
|
using Mapster;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using NexusReader.Data.Persistence;
|
|
|
|
namespace NexusReader.Web.New.Services;
|
|
|
|
public class ServerIdentityService : IIdentityService
|
|
{
|
|
private readonly UserManager<NexusUser> _userManager;
|
|
private readonly SignInManager<NexusUser> _signInManager;
|
|
private readonly INativeStorageService _storageService;
|
|
private readonly IDbContextFactory<AppDbContext> _contextFactory;
|
|
|
|
public event Func<Task>? OnStateInvalidated;
|
|
|
|
public ServerIdentityService(
|
|
UserManager<NexusUser> userManager,
|
|
SignInManager<NexusUser> signInManager,
|
|
INativeStorageService storageService,
|
|
IDbContextFactory<AppDbContext> contextFactory)
|
|
{
|
|
_userManager = userManager;
|
|
_signInManager = signInManager;
|
|
_storageService = storageService;
|
|
_contextFactory = contextFactory;
|
|
}
|
|
|
|
public async Task<Result> RegisterAsync(string email, string password)
|
|
{
|
|
var user = new NexusUser { UserName = email, Email = email };
|
|
var result = await _userManager.CreateAsync(user, password);
|
|
return result.Succeeded ? Result.Ok() : Result.Fail(result.Errors.Select(e => e.Description));
|
|
}
|
|
|
|
public async Task<Result> LoginAsync(string email, string password, bool rememberMe = false)
|
|
{
|
|
var result = await _signInManager.PasswordSignInAsync(email, password, rememberMe, lockoutOnFailure: false);
|
|
if (result.Succeeded)
|
|
{
|
|
if (OnStateInvalidated != null) await OnStateInvalidated.Invoke();
|
|
return Result.Ok();
|
|
}
|
|
return Result.Fail("Login failed");
|
|
}
|
|
|
|
public async Task<Result> LogoutAsync()
|
|
{
|
|
await _signInManager.SignOutAsync();
|
|
if (OnStateInvalidated != null) await OnStateInvalidated.Invoke();
|
|
return Result.Ok();
|
|
}
|
|
|
|
public async Task<Result<UserProfileDto>> GetProfileAsync()
|
|
{
|
|
// This is a simplified server-side profile fetch
|
|
// In a real app, you'd get the current user from HttpContext
|
|
return Result.Fail("Profile fetch not implemented on server identity service yet");
|
|
}
|
|
|
|
public Task<Result> RefreshTokenAsync()
|
|
{
|
|
return Task.FromResult(Result.Ok());
|
|
}
|
|
}
|