fix(server): avoid back to login after auth

Replace /login history entry on success and redirect authenticated /login to /, with no-store headers to prevent caching.
This commit is contained in:
Shantur Rathore
2026-02-17 18:27:41 +00:00
parent e8cfad1266
commit ef388adc4f
2 changed files with 20 additions and 2 deletions

View File

@@ -119,7 +119,8 @@
showError(message || `Login failed (${res.status})`) showError(message || `Login failed (${res.status})`)
return return
} }
window.location.href = "/" // Replace history entry so Back doesn't return to /login.
window.location.replace("/")
} catch (e) { } catch (e) {
showError(e && e.message ? e.message : String(e)) showError(e && e.message ? e.message : String(e))
} }

View File

@@ -51,7 +51,19 @@ function getTokenHtml(): string {
} }
export function registerAuthRoutes(app: FastifyInstance, deps: RouteDeps) { export function registerAuthRoutes(app: FastifyInstance, deps: RouteDeps) {
app.get("/login", async (_request, reply) => { app.get("/login", async (request, reply) => {
// If already authenticated, don't show the login page.
const session = deps.authManager.getSessionFromRequest(request)
if (session) {
reply.redirect("/")
return
}
// Avoid caching the login page (helps with bfcache/back behavior).
reply.header("Cache-Control", "no-store")
reply.header("Pragma", "no-cache")
reply.header("Expires", "0")
const status = deps.authManager.getStatus() const status = deps.authManager.getStatus()
reply.type("text/html").send(getLoginHtml(status.username)) reply.type("text/html").send(getLoginHtml(status.username))
}) })
@@ -67,6 +79,11 @@ export function registerAuthRoutes(app: FastifyInstance, deps: RouteDeps) {
return return
} }
// Avoid caching the token bootstrap page.
reply.header("Cache-Control", "no-store")
reply.header("Pragma", "no-cache")
reply.header("Expires", "0")
reply.type("text/html").send(getTokenHtml()) reply.type("text/html").send(getTokenHtml())
}) })