using Immense.RemoteControl.Shared; using Microsoft.AspNetCore.Components.Authorization; using Microsoft.AspNetCore.Identity; using Remotely.Shared.Entities; using System; using System.Collections.Generic; using System.Linq; using System.Security.Claims; using System.Security.Principal; using System.Threading.Tasks; namespace Remotely.Server.Services; public interface IAuthService { Task IsAuthenticated(); Task> GetUser(); } public class AuthService : IAuthService { private readonly AuthenticationStateProvider _authProvider; private readonly IDataService _dataService; public AuthService( AuthenticationStateProvider authProvider, IDataService dataService) { _authProvider = authProvider; _dataService = dataService; } public async Task IsAuthenticated() { var principal = await _authProvider.GetAuthenticationStateAsync(); return principal?.User?.Identity?.IsAuthenticated ?? false; } public async Task> GetUser() { var principal = await _authProvider.GetAuthenticationStateAsync(); if (principal?.User?.Identity?.IsAuthenticated == true) { return await _dataService.GetUserByName($"{principal.User.Identity.Name}"); } return Result.Fail("Not authenticated."); } }