fix(auth): redirect authenticated sessions from / and /login to /dashboard

This commit is contained in:
Tyler Gillispie 2026-08-28 00:17:34 -07:00
parent 267ed4b17b
commit 3af104915f
3 changed files with 26 additions and 5 deletions

View File

@ -9,7 +9,12 @@ import type { AuthenticationResponseJSON } from "jsr:@simplewebauthn/server@13";
import { valkey } from "../../core/valkey.ts"; import { valkey } from "../../core/valkey.ts";
import { getClientIp, publicRateLimiter } from "../../core/middleware.ts"; import { getClientIp, publicRateLimiter } from "../../core/middleware.ts";
import { extractAllSessionIds, getCookieDomain } from "../../core/session.ts"; import {
extractAllSessionIds,
getAuthenticatedUser,
getCookieDomain,
} from "../../core/session.ts";
import { isSafeRedirectUrl } from "../../core/forward_auth.ts";
import { auditWrapper } from "../../core/audit.ts"; import { auditWrapper } from "../../core/audit.ts";
import { import {
@ -31,7 +36,15 @@ const origin = Deno.env.get("ORIGIN") ||
(import.meta.main ? undefined : "http://localhost"); (import.meta.main ? undefined : "http://localhost");
// UI Route // UI Route
loginRoutes.get("/login", (c) => { loginRoutes.get("/login", async (c) => {
const auth = await getAuthenticatedUser(c);
if (auth) {
const redirectParam = c.req.query("redirect");
if (redirectParam && isSafeRedirectUrl(redirectParam)) {
return c.redirect(redirectParam);
}
return c.redirect("/dashboard");
}
return c.html(LoginPageFragment()); return c.html(LoginPageFragment());
}); });

View File

@ -10,7 +10,7 @@ import type { RegistrationResponseJSON } from "jsr:@simplewebauthn/server@13";
import { valkey } from "../../core/valkey.ts"; import { valkey } from "../../core/valkey.ts";
import { getClientIp, publicRateLimiter } from "../../core/middleware.ts"; import { getClientIp, publicRateLimiter } from "../../core/middleware.ts";
import { auditWrapper } from "../../core/audit.ts"; import { auditWrapper } from "../../core/audit.ts";
import { getCookieDomain } from "../../core/session.ts"; import { getAuthenticatedUser, getCookieDomain } from "../../core/session.ts";
import { import {
createPasskey, createPasskey,
@ -30,7 +30,11 @@ const origin = Deno.env.get("ORIGIN") ||
(import.meta.main ? undefined : "http://localhost"); (import.meta.main ? undefined : "http://localhost");
// UI Route // UI Route
registerRoutes.get("/register", (c) => { registerRoutes.get("/register", async (c) => {
const auth = await getAuthenticatedUser(c);
if (auth) {
return c.redirect("/dashboard");
}
const code = c.req.query("code") || ""; const code = c.req.query("code") || "";
return c.html(RegisterPageFragment({ initialCode: code })); return c.html(RegisterPageFragment({ initialCode: code }));
}); });

View File

@ -32,8 +32,12 @@ app.use("*", contentNegotiation());
app.use("/public/*", serveStatic({ root: "./" })); app.use("/public/*", serveStatic({ root: "./" }));
// Root, Navigation & Logout Endpoints // Root, Navigation & Logout Endpoints
app.get("/", (c) => { app.get("/", async (c) => {
c.header("Cache-Control", "no-store, no-cache, must-revalidate, max-age=0"); c.header("Cache-Control", "no-store, no-cache, must-revalidate, max-age=0");
const auth = await getAuthenticatedUser(c);
if (auth) {
return c.redirect("/dashboard");
}
return c.redirect("/login"); return c.redirect("/login");
}); });