nav.js

1.0 KB · raw

1// The navbar when signed in: "admin" and "log out" in place of "log in". Loaded right after the2// nav, before the rest of the page, so the signed-out links never show first. Pages are the same3// for everyone; the readable kepr_editor cookie, set beside the session, carries the form token4// that log out posts back, so this needs no request.5{6	const token = /(?:^|;\s*)kepr_editor=([0-9a-f]{64})(?:;|$)/.exec(document.cookie)?.[1];7	const link = document.querySelector('.nav-links a[href^="/sign-in"]');8	if (token && link) {9		const admin = document.createElement("a");10		admin.href = "/admin";11		admin.textContent = "admin";12		const form = document.createElement("form");13		form.className = "nav-logout";14		form.method = "post";15		form.action = "/admin/sign-out";16		const field = document.createElement("input");17		field.type = "hidden";18		field.name = "form";19		field.value = token;20		const button = document.createElement("button");21		button.type = "submit";22		button.textContent = "log out";23		form.append(field, button);24		link.replaceWith(admin, form);25	}26}