//-----------------------------------------------------------------------
//
// Copyright (c) aliasvault. All rights reserved.
// Licensed under the AGPLv3 license. See LICENSE.md file in the project root for full license information.
//
//-----------------------------------------------------------------------
namespace AliasVault.Api.Controllers.Abstracts;
using System.Security.Claims;
using AliasServerDb;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
///
/// Base controller that concrete controllers can extend from if all requests require authentication.
///
/// UserManager instance.
[Route("v{version:apiVersion}/[controller]")]
[ApiController]
[Authorize]
public abstract class AuthenticatedRequestController(UserManager userManager) : ControllerBase
{
///
/// Get the userManager instance.
///
/// UserManager instance.
protected UserManager GetUserManager() => userManager;
///
/// Get the current authenticated user.
///
/// AliasVaultUser object for current user.
protected async Task GetCurrentUserAsync()
{
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier) ?? throw new InvalidOperationException("Unable to find user ID.");
return await userManager.FindByIdAsync(userId);
}
}