Remotely/Server/Services/AuthService.cs
2024-07-16 09:11:32 -07:00

51 lines
1.4 KiB
C#

using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Identity;
using Remotely.Shared.Entities;
using Remotely.Shared.Primitives;
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<bool> IsAuthenticated();
Task<Result<RemotelyUser>> 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<bool> IsAuthenticated()
{
var principal = await _authProvider.GetAuthenticationStateAsync();
return principal?.User?.Identity?.IsAuthenticated ?? false;
}
public async Task<Result<RemotelyUser>> GetUser()
{
var principal = await _authProvider.GetAuthenticationStateAsync();
if (principal?.User?.Identity?.IsAuthenticated == true)
{
return await _dataService.GetUserByName($"{principal.User.Identity.Name}");
}
return Result.Fail<RemotelyUser>("Not authenticated.");
}
}