refactor: move admin page out of blog area

This commit is contained in:
Oliver Booth 2024-02-24 15:00:36 +00:00
parent 8ef34d014b
commit 0d670554e6
Signed by: oliverbooth
GPG Key ID: E60B570D1B7557B5
8 changed files with 106 additions and 11 deletions

View File

@ -21,6 +21,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "scss", "scss", "{822F528E-3
src\scss\prism.vs.scss = src\scss\prism.vs.scss
src\scss\prism.css = src\scss\prism.css
src\scss\ribbon.scss = src\scss\ribbon.scss
src\scss\admin.scss = src\scss\admin.scss
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ts", "ts", "{BB9F76AC-292A-4F47-809D-8BBBA6E0A048}"

View File

@ -1,17 +1,17 @@
using System.Net;
using Microsoft.AspNetCore.Mvc;
using OliverBooth.Data.Blog;
using OliverBooth.Data.Web;
using OliverBooth.Services;
using ISession = OliverBooth.Data.Blog.ISession;
namespace OliverBooth.Controllers.Blog;
namespace OliverBooth.Controllers;
[Controller]
[Route("auth/admin")]
public sealed class AdminController : ControllerBase
{
private readonly ILogger<AdminController> _logger;
private readonly IBlogUserService _userService;
private readonly IUserService _userService;
private readonly ISessionService _sessionService;
/// <summary>
@ -21,7 +21,7 @@ public sealed class AdminController : ControllerBase
/// <param name="userService">The user service.</param>
/// <param name="sessionService">The session service.</param>
public AdminController(ILogger<AdminController> logger,
IBlogUserService userService,
IUserService userService,
ISessionService sessionService)
{
_logger = logger;
@ -39,14 +39,14 @@ public sealed class AdminController : ControllerBase
if (string.IsNullOrWhiteSpace(loginEmail))
{
_logger.LogInformation("Login attempt from {Host} with empty login", remoteIpAddress);
return RedirectToPage("/blog/admin/login");
return RedirectToPage("/admin/login");
}
if (string.IsNullOrWhiteSpace(loginPassword))
{
_logger.LogInformation("Login attempt as '{Email}' from {Host} with empty password", loginEmail,
remoteIpAddress);
return RedirectToPage("/blog/admin/login");
return RedirectToPage("/admin/login");
}
if (_userService.VerifyLogin(loginEmail, loginPassword, out IUser? user))
@ -56,14 +56,14 @@ public sealed class AdminController : ControllerBase
else
{
_logger.LogInformation("Login attempt for '{Email}' failed from {Host}", loginEmail, remoteIpAddress);
return RedirectToPage("/blog/admin/login");
return RedirectToPage("/admin/login");
}
ISession session = _sessionService.CreateSession(Request, user);
Span<byte> sessionBytes = stackalloc byte[16];
session.Id.TryWriteBytes(sessionBytes);
Response.Cookies.Append("sid", Convert.ToBase64String(sessionBytes));
return RedirectToPage("/blog/admin/index");
return RedirectToPage("/admin/index");
}
[HttpGet("logout")]
@ -72,7 +72,6 @@ public sealed class AdminController : ControllerBase
if (_sessionService.TryGetSession(Request, out ISession? session, true))
_sessionService.DeleteSession(session);
Response.Cookies.Delete("sid");
return RedirectToPage("/blog/admin/login");
return _sessionService.DeleteSessionCookie(Response);
}
}

View File

@ -0,0 +1,8 @@
@page
@model OliverBooth.Pages.Admin.Index
@{
ViewData["Title"] = "Admin";
}
<h1>Hello @Model.CurrentUser.DisplayName!</h1>

View File

@ -0,0 +1,41 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using OliverBooth.Data.Web;
using OliverBooth.Services;
using ISession = OliverBooth.Data.Blog.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()
{
if (!_sessionService.TryGetSession(HttpContext.Request, out ISession? session))
{
_logger.LogDebug("Session not found; 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();
}
}

View File

@ -0,0 +1,30 @@
@page
@model OliverBooth.Pages.Admin.Login
@{
ViewData["Title"] = "Login";
}
@section Styles
{
<link rel="stylesheet" href="~/css/admin.min.css" asp-append-version="true">
}
<div class="form-signin m-auto">
<form method="post" asp-controller="Admin" asp-action="Login">
<h1 class="h3 mb-3 fw-normal">Please sign in</h1>
<div class="form-floating">
<input type="email" class="form-control" id="login-email" name="login-email" placeholder="name@example.com">
<label for="login-email">Email address</label>
</div>
<div class="form-floating">
<input type="password" class="form-control" id="login-password" name="login-password" placeholder="Password">
<label for="login-password">Password</label>
</div>
<button class="btn btn-primary w-100 py-2" type="submit">Sign in</button>
</form>
</div>

View File

@ -0,0 +1,11 @@
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace OliverBooth.Pages.Admin;
public class Login : PageModel
{
public void OnGet()
{
}
}

View File

@ -7,6 +7,7 @@ using OliverBooth.Markdown.Template;
using OliverBooth.Markdown.Timestamp;
using OliverBooth.Services;
using Serilog;
using X10D.Hosting.DependencyInjection;
Log.Logger = new LoggerConfiguration()
.WriteTo.Console()
@ -36,11 +37,11 @@ builder.Services.AddHttpClient();
builder.Services.AddSingleton<IContactService, ContactService>();
builder.Services.AddSingleton<ITemplateService, TemplateService>();
builder.Services.AddSingleton<IBlogPostService, BlogPostService>();
builder.Services.AddSingleton<ISessionService, SessionService>();
builder.Services.AddSingleton<IUserService, UserService>();
builder.Services.AddSingleton<IProjectService, ProjectService>();
builder.Services.AddSingleton<IMastodonService, MastodonService>();
builder.Services.AddSingleton<IReadingListService, ReadingListService>();
builder.Services.AddHostedSingleton<ISessionService, SessionService>();
builder.Services.AddRazorPages().AddRazorRuntimeCompilation();
builder.Services.AddControllersWithViews();
builder.Services.AddRouting(options => options.LowercaseUrls = true);

4
src/scss/admin.scss Normal file
View File

@ -0,0 +1,4 @@
.form-signin {
max-width: 330px;
padding: 1rem;
}