1 Commits
Author SHA1 Message Date
GeNii96 63ee71a19b Initial commit 2026-05-27 12:47:16 +02:00
13 changed files with 63 additions and 20 deletions

No files matched your search

@@ -16,7 +16,7 @@ namespace OnlyPrompt.Backend.Controllers
[Route("api/v1/auth")] [Route("api/v1/auth")]
public class AuthController : BaseController public class AuthController : BaseController
{ {
private static readonly CookieOptions AuthCookieOptions = new CookieOptions { Secure = true, HttpOnly = true, IsEssential = true }; private static readonly CookieOptions AuthCookieOptions = new CookieOptions { Secure = false, HttpOnly = true, IsEssential = true, SameSite = SameSiteMode.Lax };
private readonly IPasswordHasher<UserModel> _passwordHasher; private readonly IPasswordHasher<UserModel> _passwordHasher;
private readonly ITokenService _jwtService; private readonly ITokenService _jwtService;
private readonly ILogger<AuthController> _logger; private readonly ILogger<AuthController> _logger;
@@ -103,7 +103,18 @@ namespace OnlyPrompt.Backend.Controllers
public RedirectHttpResult Logout() public RedirectHttpResult Logout()
{ {
this.Response.Cookies.Delete("jwt", AuthCookieOptions); this.Response.Cookies.Delete("jwt", AuthCookieOptions);
return TypedResults.Redirect("login"); return TypedResults.Redirect("/login");
}
[Authorize]
[HttpGet("me")]
public async Task<Results<Ok<ApiUser>, NotFound<string>>> GetCurrentUserAsync()
{
var user = await GetUserAsync();
if (user is null)
return TypedResults.NotFound("User not found");
return TypedResults.Ok(_mapper.Map<ApiUser>(user));
} }
} }
} }
+8
View File
@@ -80,6 +80,7 @@ var rewrite = new RewriteOptions()
.AddRewrite(@"^(?!scalar\/?|api\/?)([^.]+)$", "$1.html", skipRemainingRules: true); .AddRewrite(@"^(?!scalar\/?|api\/?)([^.]+)$", "$1.html", skipRemainingRules: true);
app.UseRewriter(rewrite); app.UseRewriter(rewrite);
app.UseAuthentication();
app.UseAuthorization(); app.UseAuthorization();
if (app.Environment.IsDevelopment()) if (app.Environment.IsDevelopment())
{ {
@@ -97,6 +98,13 @@ else
} }
app.MapControllers(); app.MapControllers();
app.MapGet("/", async (HttpContext context) =>
{
var authResult = await context.AuthenticateAsync(JwtBearerDefaults.AuthenticationScheme);
return authResult.Succeeded
? Results.Redirect("/dashboard")
: Results.Redirect("/login");
});
app.MapFallbackToFile("/login.html"); app.MapFallbackToFile("/login.html");
using var scope = app.Services.CreateScope(); using var scope = app.Services.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<OnlyPromptContext>(); var db = scope.ServiceProvider.GetRequiredService<OnlyPromptContext>();
+2 -2
View File
@@ -108,7 +108,7 @@
</div> </div>
<script> <script>
fetch('../html/sidebar.html') fetch('/sidebar.html')
.then(r => r.text()) .then(r => r.text())
.then(data => { .then(data => {
document.getElementById('sidebar-container').innerHTML = data; document.getElementById('sidebar-container').innerHTML = data;
@@ -121,7 +121,7 @@
if (chatsLink) chatsLink.classList.add('active'); if (chatsLink) chatsLink.classList.add('active');
}); });
fetch('../html/topbar.html') fetch('/topbar.html')
.then(r => r.text()) .then(r => r.text())
.then(data => document.getElementById('topbar-container').innerHTML = data); .then(data => document.getElementById('topbar-container').innerHTML = data);
</script> </script>
+2 -2
View File
@@ -140,7 +140,7 @@
</div> </div>
<script> <script>
fetch('../html/sidebar.html') fetch('/sidebar.html')
.then(r => r.text()) .then(r => r.text())
.then(data => { .then(data => {
document.getElementById('sidebar-container').innerHTML = data; document.getElementById('sidebar-container').innerHTML = data;
@@ -153,7 +153,7 @@
if (thirdLink) thirdLink.classList.add('active'); if (thirdLink) thirdLink.classList.add('active');
}); });
fetch('../html/topbar.html') fetch('/topbar.html')
.then(r => r.text()) .then(r => r.text())
.then(data => document.getElementById('topbar-container').innerHTML = data); .then(data => document.getElementById('topbar-container').innerHTML = data);
</script> </script>
+2 -2
View File
@@ -160,7 +160,7 @@
}); });
// Fetch sidebar and topbar // Fetch sidebar and topbar
fetch('../html/sidebar.html') fetch('/sidebar.html')
.then(r => r.text()) .then(r => r.text())
.then(data => { .then(data => {
document.getElementById('sidebar-container').innerHTML = data; document.getElementById('sidebar-container').innerHTML = data;
@@ -173,7 +173,7 @@
if (createLink) createLink.classList.add('active'); if (createLink) createLink.classList.add('active');
}); });
fetch('../html/topbar.html') fetch('/topbar.html')
.then(r => r.text()) .then(r => r.text())
.then(data => document.getElementById('topbar-container').innerHTML = data); .then(data => document.getElementById('topbar-container').innerHTML = data);
</script> </script>
+9
View File
@@ -100,13 +100,22 @@
padding-top: 16px; padding-top: 16px;
} }
.sidebar-bottom form {
margin: 0;
}
.sidebar-logout { .sidebar-logout {
display: flex; display: flex;
align-items: center; align-items: center;
justify-content: space-between; justify-content: space-between;
width: 100%;
padding: 12px 16px; padding: 12px 16px;
border: 0;
background: transparent;
cursor: pointer;
text-decoration: none; text-decoration: none;
color: #64748b; color: #64748b;
font: inherit;
font-weight: 600; font-weight: 600;
transition: background 0.2s ease; transition: background 0.2s ease;
} }
+2 -2
View File
@@ -145,7 +145,7 @@
</div> </div>
<script> <script>
fetch('../html/sidebar.html') fetch('/sidebar.html')
.then(r => r.text()) .then(r => r.text())
.then(data => { .then(data => {
document.getElementById('sidebar-container').innerHTML = data; document.getElementById('sidebar-container').innerHTML = data;
@@ -158,7 +158,7 @@
if (firstLink) firstLink.classList.add('active'); if (firstLink) firstLink.classList.add('active');
}); });
fetch('../html/topbar.html') fetch('/topbar.html')
.then(r => r.text()) .then(r => r.text())
.then(data => document.getElementById('topbar-container').innerHTML = data); .then(data => document.getElementById('topbar-container').innerHTML = data);
</script> </script>
+13
View File
@@ -1,5 +1,16 @@
import { sendFormAsync } from "./shared.js"; import { sendFormAsync } from "./shared.js";
async function redirectIfAlreadySignedIn() {
const response = await fetch('/api/v1/auth/me', {
method: 'GET',
credentials: 'same-origin'
});
if (response.ok) {
window.location.replace('/dashboard');
}
}
function togglePassword() { function togglePassword() {
const passwordInput = document.getElementById('password'); const passwordInput = document.getElementById('password');
const newInputType = passwordInput.type === 'password' ? 'text' : 'password'; const newInputType = passwordInput.type === 'password' ? 'text' : 'password';
@@ -19,3 +30,5 @@ loginForm.addEventListener('submit', async (event) => {
event.preventDefault(); // Prevent the default form submission event.preventDefault(); // Prevent the default form submission
await submitLoginForm(); await submitLoginForm();
}); });
await redirectIfAlreadySignedIn();
+2 -2
View File
@@ -216,7 +216,7 @@
</div> </div>
<script> <script>
fetch('../html/sidebar.html') fetch('/sidebar.html')
.then(r => r.text()) .then(r => r.text())
.then(data => { .then(data => {
document.getElementById('sidebar-container').innerHTML = data; document.getElementById('sidebar-container').innerHTML = data;
@@ -229,7 +229,7 @@
if (secondLink) secondLink.classList.add('active'); if (secondLink) secondLink.classList.add('active');
}); });
fetch('../html/topbar.html') fetch('/topbar.html')
.then(r => r.text()) .then(r => r.text())
.then(data => document.getElementById('topbar-container').innerHTML = data); .then(data => document.getElementById('topbar-container').innerHTML = data);
</script> </script>
+2 -2
View File
@@ -93,7 +93,7 @@
<script> <script>
// Fetch sidebar and topbar // Fetch sidebar and topbar
fetch('../html/sidebar.html') fetch('/sidebar.html')
.then(r => r.text()) .then(r => r.text())
.then(data => { .then(data => {
document.getElementById('sidebar-container').innerHTML = data; document.getElementById('sidebar-container').innerHTML = data;
@@ -106,7 +106,7 @@
if (marketplaceLink) marketplaceLink.classList.add('active'); if (marketplaceLink) marketplaceLink.classList.add('active');
}); });
fetch('../html/topbar.html') fetch('/topbar.html')
.then(r => r.text()) .then(r => r.text())
.then(data => document.getElementById('topbar-container').innerHTML = data); .then(data => document.getElementById('topbar-container').innerHTML = data);
</script> </script>
+2 -2
View File
@@ -90,7 +90,7 @@
</div> </div>
<script> <script>
fetch('../html/sidebar.html') fetch('/sidebar.html')
.then(r => r.text()) .then(r => r.text())
.then(data => { .then(data => {
document.getElementById('sidebar-container').innerHTML = data; document.getElementById('sidebar-container').innerHTML = data;
@@ -103,7 +103,7 @@
if (profileLink) profileLink.classList.add('active'); if (profileLink) profileLink.classList.add('active');
}); });
fetch('../html/topbar.html') fetch('/topbar.html')
.then(r => r.text()) .then(r => r.text())
.then(data => document.getElementById('topbar-container').innerHTML = data); .then(data => document.getElementById('topbar-container').innerHTML = data);
</script> </script>
+2 -2
View File
@@ -179,7 +179,7 @@
}); });
// Fetch sidebar and topbar // Fetch sidebar and topbar
fetch('../html/sidebar.html') fetch('/sidebar.html')
.then(r => r.text()) .then(r => r.text())
.then(data => { .then(data => {
document.getElementById('sidebar-container').innerHTML = data; document.getElementById('sidebar-container').innerHTML = data;
@@ -189,7 +189,7 @@
const settingsLink = document.querySelector('#sidebar-container a[href="settings.html"]'); const settingsLink = document.querySelector('#sidebar-container a[href="settings.html"]');
if (settingsLink) settingsLink.classList.add('active'); if (settingsLink) settingsLink.classList.add('active');
}); });
fetch('../html/topbar.html') fetch('/topbar.html')
.then(r => r.text()) .then(r => r.text())
.then(data => document.getElementById('topbar-container').innerHTML = data); .then(data => document.getElementById('topbar-container').innerHTML = data);
</script> </script>
+4 -2
View File
@@ -67,12 +67,14 @@
<!-- Logout bottom --> <!-- Logout bottom -->
<div class="sidebar-bottom"> <div class="sidebar-bottom">
<a href="login.html" class="sidebar-logout"> <form action="/api/v1/auth/logout" method="post">
<button type="submit" class="sidebar-logout">
<div class="logout-left"> <div class="logout-left">
<i class="bi bi-box-arrow-right"></i> <i class="bi bi-box-arrow-right"></i>
<span class="nav-text">Logout</span> <span class="nav-text">Logout</span>
</div> </div>
<i class="bi bi-chevron-right logout-arrow"></i> <i class="bi bi-chevron-right logout-arrow"></i>
</a> </button>
</form>
</div> </div>
</div> </div>