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 _userManager; private readonly SignInManager _signInManager; private readonly INativeStorageService _storageService; private readonly IDbContextFactory _contextFactory; public event Func? OnStateInvalidated; public ServerIdentityService( UserManager userManager, SignInManager signInManager, INativeStorageService storageService, IDbContextFactory contextFactory) { _userManager = userManager; _signInManager = signInManager; _storageService = storageService; _contextFactory = contextFactory; } public async Task 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 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 LogoutAsync() { await _signInManager.SignOutAsync(); if (OnStateInvalidated != null) await OnStateInvalidated.Invoke(); return Result.Ok(); } public async Task> 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 RefreshTokenAsync() { return Task.FromResult(Result.Ok()); } }