-
+
-
\ No newline at end of file
diff --git a/OliverBooth/Pages/Admin/Login.cshtml.cs b/OliverBooth/Pages/Admin/Login.cshtml.cs
index b4a6bfc..6033f27 100644
--- a/OliverBooth/Pages/Admin/Login.cshtml.cs
+++ b/OliverBooth/Pages/Admin/Login.cshtml.cs
@@ -1,11 +1,20 @@
+using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
+using OliverBooth.Services;
namespace OliverBooth.Pages.Admin;
public class Login : PageModel
{
- public void OnGet()
+ private readonly ISessionService _sessionService;
+
+ public Login(ISessionService sessionService)
{
-
+ _sessionService = sessionService;
+ }
+
+ public IActionResult OnGet()
+ {
+ return _sessionService.TryGetCurrentUser(Request, Response, out _) ? RedirectToPage("/admin/index") : Page();
}
}
diff --git a/OliverBooth/Services/ISessionService.cs b/OliverBooth/Services/ISessionService.cs
index 99bce13..4d55c8c 100644
--- a/OliverBooth/Services/ISessionService.cs
+++ b/OliverBooth/Services/ISessionService.cs
@@ -45,7 +45,26 @@ public interface ISessionService
///
is .
///
void SaveSessionCookie(HttpResponse response, ISession session);
-
+
+ ///
+ /// Attempts to find the user associated with the client's current request.
+ ///
+ ///
The HTTP request.
+ ///
The response to edit.
+ ///
+ /// When this method returns, contains the user with the specified request, if the user is found; otherwise,
+ ///
.
+ ///
+ ///
+ /// if a user is found; otherwise, .
+ ///
+ ///
+ /// is .
+ /// -or-
+ /// is .
+ ///
+ bool TryGetCurrentUser(HttpRequest request, HttpResponse response, [NotNullWhen(true)] out IUser? user);
+
///
/// Attempts to find a session with the specified ID.
///
diff --git a/OliverBooth/Services/SessionService.cs b/OliverBooth/Services/SessionService.cs
index b726af4..7a0bb32 100644
--- a/OliverBooth/Services/SessionService.cs
+++ b/OliverBooth/Services/SessionService.cs
@@ -101,6 +101,35 @@ internal sealed class SessionService : BackgroundService, ISessionService
response.Cookies.Append("sid", Convert.ToBase64String(buffer));
}
+ ///
+ public bool TryGetCurrentUser(HttpRequest request, HttpResponse response, [NotNullWhen(true)] out IUser? user)
+ {
+ user = null;
+
+ if (!TryGetSession(request, out ISession? session))
+ {
+ _logger.LogDebug("Session not found; redirecting");
+ DeleteSessionCookie(response);
+ return false;
+ }
+
+ if (!ValidateSession(request, session))
+ {
+ _logger.LogDebug("Session invalid; redirecting");
+ DeleteSessionCookie(response);
+ return false;
+ }
+
+ if (!_userService.TryGetUser(session.UserId, out user))
+ {
+ _logger.LogDebug("User not found; redirecting");
+ DeleteSessionCookie(response);
+ return false;
+ }
+
+ return true;
+ }
+
///
public bool TryGetSession(Guid sessionId, [NotNullWhen(true)] out ISession? session)
{