LANCommander/LANCommander.Server/Controllers/AccountController.cs

85 lines
2.8 KiB
C#
Raw Normal View History

using System.Security.Claims;
using LANCommander.Server.Data.Models;
using LANCommander.Server.Models;
using LANCommander.Server.Services;
using LANCommander.Server.Services.Models;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Http.Extensions;
2025-01-06 18:16:43 -06:00
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using ZiggyCreatures.Caching.Fusion;
using AuthenticationService = LANCommander.Server.Services.AuthenticationService;
namespace LANCommander.Server.Controllers;
public class AccountController : BaseController
{
private readonly UserService UserService;
private readonly UserCustomFieldService UserCustomFieldService;
2025-01-06 18:16:43 -06:00
private readonly SignInManager<User> SignInManager;
private readonly IFusionCache Cache;
public AccountController(
UserService userService,
UserCustomFieldService userCustomFieldService,
2025-01-06 18:16:43 -06:00
SignInManager<User> signInManager,
IFusionCache cache,
ILogger<AccountController> logger) : base(logger)
{
UserService = userService;
UserCustomFieldService = userCustomFieldService;
2025-01-06 18:16:43 -06:00
SignInManager = signInManager;
Cache = cache;
}
2025-01-06 18:16:43 -06:00
[HttpGet("/Logout")]
public async Task<IActionResult> Logout()
{
await SignInManager.SignOutAsync();
return Redirect("/Login");
}
[HttpGet("/SignInOAuth")]
public async Task<IActionResult> SignInOAuth()
{
var authenticationProviders = await AuthenticationService.GetAuthenticationProviderTemplatesAsync();
return Ok();
}
[HttpPost("/AccountLink")]
public async Task<IActionResult> AccountLink(string providerSlug, string returnUrl = "/")
{
var user = await UserService.GetAsync(User.Identity.Name);
var provider = Settings.Authentication.AuthenticationProviders.FirstOrDefault(ap => ap.Slug == providerSlug);
2024-12-30 14:16:49 -06:00
var items = new Dictionary<string, string>()
{
2024-12-30 14:16:49 -06:00
{ "UserId", user.Id.ToString() },
2025-01-06 02:29:15 -06:00
{ "Action", AuthenticationProviderActionType.AccountLink },
2024-12-30 14:16:49 -06:00
};
2024-12-30 14:16:49 -06:00
return Challenge(new AuthenticationProperties(items) { RedirectUri = returnUrl, AllowRefresh = true }, provider.Slug);
}
2024-12-30 21:04:13 -06:00
[HttpPost("/RegisterByAuthenticationProvider")]
public async Task<IActionResult> RegisterByAuthenticationProvider(string provider, string returnUrl = "/")
{
if (!String.IsNullOrWhiteSpace(provider))
{
var properties = new AuthenticationProperties(new Dictionary<string, string>()
{
{ "Action", AuthenticationProviderActionType.Register }
});
properties.RedirectUri = returnUrl;
return Challenge(properties, provider);
}
else
{
return BadRequest();
}
}
}