mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-08-13 15:29:03 +00:00
Add logout page
This commit is contained in:
parent
ff823b5ae6
commit
ef76332fdb
9 changed files with 111 additions and 21 deletions
|
@ -130,6 +130,28 @@ namespace LBPUnion.ProjectLighthouse
|
|||
|
||||
#region Web Token Shenanigans
|
||||
|
||||
public async Task<User?> UserFromLighthouseToken(string lighthouseToken)
|
||||
{
|
||||
WebToken? token = await this.WebTokens.FirstOrDefaultAsync(t => t.UserToken == lighthouseToken);
|
||||
if (token == null) return null;
|
||||
|
||||
return await this.Users.Include(u => u.Location).FirstOrDefaultAsync(u => u.UserId == token.UserId);
|
||||
}
|
||||
|
||||
public async Task<User?> UserFromWebRequest(HttpRequest request)
|
||||
{
|
||||
if (!request.Cookies.TryGetValue("LighthouseToken", out string? lighthouseToken) || lighthouseToken == null) return null;
|
||||
|
||||
return await this.UserFromLighthouseToken(lighthouseToken);
|
||||
}
|
||||
|
||||
public async Task<WebToken?> WebTokenFromRequest(HttpRequest request)
|
||||
{
|
||||
if (!request.Cookies.TryGetValue("LighthouseToken", out string? lighthouseToken) || lighthouseToken == null) return null;
|
||||
|
||||
return await this.WebTokens.FirstOrDefaultAsync(t => t.UserToken == lighthouseToken);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
public async Task<Photo?> PhotoFromSubject(PhotoSubject subject)
|
||||
|
|
|
@ -4,4 +4,9 @@
|
|||
@{
|
||||
Layout = "Layouts/BaseLayout";
|
||||
}
|
||||
<h1>Welcome to <b>Project Lighthouse</b>.</h1>
|
||||
<h1>Welcome to <b>Project Lighthouse</b>!</h1>
|
||||
|
||||
@if (Model.User != null)
|
||||
{
|
||||
<p>You are currently logged in as <b>@Model.User.Username</b>.</p>
|
||||
}
|
|
@ -1,12 +1,25 @@
|
|||
#nullable enable
|
||||
using System.Threading.Tasks;
|
||||
using JetBrains.Annotations;
|
||||
using LBPUnion.ProjectLighthouse.Pages.Layouts;
|
||||
using LBPUnion.ProjectLighthouse.Types;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace LBPUnion.ProjectLighthouse.Pages.ExternalAuth
|
||||
{
|
||||
public class LandingPage : BaseLayout
|
||||
{
|
||||
public LandingPage(Database database) : base(database)
|
||||
{}
|
||||
|
||||
public new User? User { get; set; }
|
||||
|
||||
[UsedImplicitly]
|
||||
public IActionResult OnGet() => this.Page();
|
||||
public async Task<IActionResult> OnGet()
|
||||
{
|
||||
User = await this.Database.UserFromWebRequest(this.Request);
|
||||
|
||||
return this.Page();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -11,12 +11,8 @@ namespace LBPUnion.ProjectLighthouse.Pages.ExternalAuth
|
|||
{
|
||||
public class LoginForm : BaseLayout
|
||||
{
|
||||
private readonly Database database;
|
||||
|
||||
public LoginForm(Database database)
|
||||
{
|
||||
this.database = database;
|
||||
}
|
||||
public LoginForm(Database database) : base(database)
|
||||
{}
|
||||
|
||||
public bool WasLoginRequest { get; private set; }
|
||||
|
||||
|
@ -27,7 +23,7 @@ namespace LBPUnion.ProjectLighthouse.Pages.ExternalAuth
|
|||
|
||||
if (WasLoginRequest)
|
||||
{
|
||||
User? user = await this.database.Users.FirstOrDefaultAsync(u => u.Username == username);
|
||||
User? user = await this.Database.Users.FirstOrDefaultAsync(u => u.Username == username);
|
||||
if (user == null) return this.StatusCode(403, "");
|
||||
|
||||
if (!BCrypt.Net.BCrypt.Verify(password, user.Password)) return this.StatusCode(403, "");
|
||||
|
@ -38,8 +34,8 @@ namespace LBPUnion.ProjectLighthouse.Pages.ExternalAuth
|
|||
UserToken = HashHelper.GenerateAuthToken(),
|
||||
};
|
||||
|
||||
this.database.WebTokens.Add(webToken);
|
||||
await this.database.SaveChangesAsync();
|
||||
this.Database.WebTokens.Add(webToken);
|
||||
await this.Database.SaveChangesAsync();
|
||||
|
||||
this.Response.Cookies.Append("LighthouseToken", webToken.UserToken);
|
||||
|
||||
|
|
9
ProjectLighthouse/Pages/ExternalAuth/LogoutPage.cshtml
Normal file
9
ProjectLighthouse/Pages/ExternalAuth/LogoutPage.cshtml
Normal file
|
@ -0,0 +1,9 @@
|
|||
@page "/logout"
|
||||
@model LBPUnion.ProjectLighthouse.Pages.ExternalAuth.LogoutPage
|
||||
|
||||
@{
|
||||
Layout = "Layouts/BaseLayout";
|
||||
}
|
||||
|
||||
<p>You have been successfully logged out. You will be redirected in 5 seconds, or you may click <a href="/">here</a> to do so manually.</p>
|
||||
<meta http-equiv="refresh" content="5; url=/"/>
|
26
ProjectLighthouse/Pages/ExternalAuth/LogoutPage.cshtml.cs
Normal file
26
ProjectLighthouse/Pages/ExternalAuth/LogoutPage.cshtml.cs
Normal file
|
@ -0,0 +1,26 @@
|
|||
#nullable enable
|
||||
using System.Threading.Tasks;
|
||||
using LBPUnion.ProjectLighthouse.Pages.Layouts;
|
||||
using LBPUnion.ProjectLighthouse.Types;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace LBPUnion.ProjectLighthouse.Pages.ExternalAuth
|
||||
{
|
||||
public class LogoutPage : BaseLayout
|
||||
{
|
||||
public LogoutPage(Database database) : base(database)
|
||||
{}
|
||||
public async Task<IActionResult> OnGet()
|
||||
{
|
||||
WebToken? token = await this.Database.WebTokenFromRequest(this.Request);
|
||||
if (token == null) return this.BadRequest();
|
||||
|
||||
this.Database.WebTokens.Remove(token);
|
||||
await this.Database.SaveChangesAsync();
|
||||
|
||||
this.Response.Cookies.Delete("LighthouseToken");
|
||||
|
||||
return this.Page();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -11,12 +11,8 @@ namespace LBPUnion.ProjectLighthouse.Pages.ExternalAuth
|
|||
{
|
||||
public class RegisterForm : BaseLayout
|
||||
{
|
||||
private readonly Database database;
|
||||
|
||||
public RegisterForm(Database database)
|
||||
{
|
||||
this.database = database;
|
||||
}
|
||||
public RegisterForm(Database database) : base(database)
|
||||
{}
|
||||
|
||||
public bool WasRegisterRequest { get; private set; }
|
||||
|
||||
|
@ -32,10 +28,10 @@ namespace LBPUnion.ProjectLighthouse.Pages.ExternalAuth
|
|||
if (WasRegisterRequest)
|
||||
{
|
||||
Console.WriteLine(password);
|
||||
bool userExists = await this.database.Users.FirstOrDefaultAsync(u => u.Username.ToLower() == username.ToLower()) != null;
|
||||
bool userExists = await this.Database.Users.FirstOrDefaultAsync(u => u.Username.ToLower() == username.ToLower()) != null;
|
||||
if (userExists) return this.BadRequest();
|
||||
|
||||
this.database.CreateUser(username, HashHelper.BCryptHash(password));
|
||||
this.Database.CreateUser(username, HashHelper.BCryptHash(password));
|
||||
}
|
||||
|
||||
return this.Page();
|
||||
|
|
|
@ -2,6 +2,20 @@
|
|||
@using LBPUnion.ProjectLighthouse.Types
|
||||
@model LBPUnion.ProjectLighthouse.Pages.Layouts.BaseLayout
|
||||
|
||||
@{
|
||||
Model!.User = await Model.Database.UserFromWebRequest(Model.Request);
|
||||
|
||||
if (Model.User == null)
|
||||
{
|
||||
Model.NavigationItems.Add(new PageNavigationItem("Register", "/register"));
|
||||
Model.NavigationItems.Add(new PageNavigationItem("Log in", "/login"));
|
||||
}
|
||||
else
|
||||
{
|
||||
Model.NavigationItems.Add(new PageNavigationItem("Log out", "/logout"));
|
||||
}
|
||||
}
|
||||
|
||||
<!DOCTYPE html>
|
||||
|
||||
<html lang="en">
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
#nullable enable
|
||||
using System.Collections.Generic;
|
||||
using LBPUnion.ProjectLighthouse.Types;
|
||||
using Microsoft.AspNetCore.Mvc.RazorPages;
|
||||
|
@ -6,11 +7,19 @@ namespace LBPUnion.ProjectLighthouse.Pages.Layouts
|
|||
{
|
||||
public class BaseLayout : PageModel
|
||||
{
|
||||
public readonly Database Database;
|
||||
|
||||
public new User? User { get; set; }
|
||||
|
||||
public BaseLayout(Database database)
|
||||
{
|
||||
this.Database = database;
|
||||
}
|
||||
|
||||
public readonly List<PageNavigationItem> NavigationItems = new()
|
||||
{
|
||||
new PageNavigationItem("Home", "/"),
|
||||
new PageNavigationItem("Register", "/register"),
|
||||
new PageNavigationItem("Login", "/login"),
|
||||
};
|
||||
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue