fix(auth): make logout redirect after reauth
This commit is contained in:
@@ -187,6 +187,15 @@ function resolveAuthUser(req) {
|
|||||||
return value || null;
|
return value || null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function hasCookie(req, name, value) {
|
||||||
|
const raw = typeof req.headers.cookie === 'string' ? req.headers.cookie : '';
|
||||||
|
if (!raw) return false;
|
||||||
|
return raw
|
||||||
|
.split(';')
|
||||||
|
.map((p) => p.trim())
|
||||||
|
.some((kv) => kv === `${name}=${value}`);
|
||||||
|
}
|
||||||
|
|
||||||
function proxyApi(req, res, apiReadToken) {
|
function proxyApi(req, res, apiReadToken) {
|
||||||
const upstreamBase = new URL(API_UPSTREAM);
|
const upstreamBase = new URL(API_UPSTREAM);
|
||||||
const inUrl = new URL(req.url || '/', `http://${req.headers.host || 'localhost'}`);
|
const inUrl = new URL(req.url || '/', `http://${req.headers.host || 'localhost'}`);
|
||||||
@@ -256,12 +265,22 @@ function handler(req, res) {
|
|||||||
|
|
||||||
if (req.method === 'GET' && url.pathname === '/logout') {
|
if (req.method === 'GET' && url.pathname === '/logout') {
|
||||||
// NOTE: With HTTP basic auth handled upstream (Traefik), browser "logout" is best-effort.
|
// NOTE: With HTTP basic auth handled upstream (Traefik), browser "logout" is best-effort.
|
||||||
// This endpoint forces a 401 so the browser prompts again, allowing the user to switch accounts.
|
// We force a single 401 to show the browser prompt, then on retry we redirect back to '/'.
|
||||||
|
const marker = 'trade_logout';
|
||||||
|
if (!hasCookie(req, marker, '1')) {
|
||||||
|
res.setHeader('set-cookie', `${marker}=1; Path=/logout; Max-Age=10; SameSite=Lax`);
|
||||||
res.setHeader('www-authenticate', 'Basic realm="trade"');
|
res.setHeader('www-authenticate', 'Basic realm="trade"');
|
||||||
send(res, 401, { 'content-type': 'text/plain; charset=utf-8', 'cache-control': 'no-store' }, 'logged_out');
|
send(res, 401, { 'content-type': 'text/plain; charset=utf-8', 'cache-control': 'no-store' }, 'logged_out');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
res.setHeader('set-cookie', `${marker}=; Path=/logout; Max-Age=0; SameSite=Lax`);
|
||||||
|
res.statusCode = 302;
|
||||||
|
res.setHeader('location', '/');
|
||||||
|
res.end();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (BASIC_AUTH_ENABLED) {
|
if (BASIC_AUTH_ENABLED) {
|
||||||
let creds;
|
let creds;
|
||||||
try {
|
try {
|
||||||
|
|||||||
Reference in New Issue
Block a user