oliverbooth.dev/OliverBooth/Pages/Admin/Index.cshtml.cs

48 lines
1.4 KiB
C#
Raw Normal View History

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using OliverBooth.Data.Web;
using OliverBooth.Services;
using ISession = OliverBooth.Data.Web.ISession;
namespace OliverBooth.Pages.Admin;
public class Index : PageModel
{
private readonly ILogger<Index> _logger;
private readonly IUserService _userService;
private readonly ISessionService _sessionService;
public Index(ILogger<Index> logger, IUserService userService, ISessionService sessionService)
{
_logger = logger;
_userService = userService;
_sessionService = sessionService;
}
public IUser CurrentUser { get; private set; } = null!;
public IActionResult OnGet()
{
2024-02-24 15:04:03 +00:00
if (!_sessionService.TryGetSession(Request, out ISession? session))
{
_logger.LogDebug("Session not found; redirecting");
return _sessionService.DeleteSessionCookie(Response);
}
2024-02-24 15:04:03 +00:00
if (!_sessionService.ValidateSession(Request, session))
{
_logger.LogDebug("Session invalid; redirecting");
return _sessionService.DeleteSessionCookie(Response);
}
if (!_userService.TryGetUser(session.UserId, out IUser? user))
{
_logger.LogDebug("User not found; redirecting");
return _sessionService.DeleteSessionCookie(Response);
}
CurrentUser = user;
return Page();
}
}