Give the site an administrator
Create one editor account from the terminal, and let that editor sign in and out of the site with an ordinary form.
Allow 20–30 minutes. This is an editorial estimate. Continue in the same my-site folder.
Read
Start at L12. The database owns your pages now, and visitors may read them but not change them. Before anyone can change them through the site, the site has to know who that person is. This lesson adds exactly one person: you, the editor.
The account lives in the site's database, next to the pages. You create it from the terminal in the project folder, because only someone who can already work in that folder should be able to make an editor. There is no built-in account, and the password itself is never stored: the database keeps only a one-way hash of it. The downloaded checkpoint contains no accounts or sessions. Your own project backups include the account database and may include active sessions, so keep those backups private.
Signing in is an ordinary form. When the password matches, the server starts a new session for your browser: a new random cookie that names your account. A cookie someone else planted before you signed in is thrown away. Signing out deletes that session on the server, so the old cookie is useless even if someone copied it. Each request checks your account again, so changing the password signs out every browser that was using the old one.
Forms that change something carry a hidden token that only pages from your own site have. The framework adds it to every form for you, and refuses any change that arrives without it, including a sign-in or sign-out sent from another site.
Change
1. Keep a copy. Stop the dev server with Ctrl+C. Copy the whole my-site folder to a new folder such as my-site-before-L13.
2. Turn on sign-in. Replace what.toml. Only the [auth] section at the end is new.
# What Project Configuration
[server]
port = 8085
host = "127.0.0.1"
[session]
enabled = true
cookie_name = "w_session"
max_age = 604800
database = "data/sessions.db"
# The database owns the pages now. On the first start after this change the
# server copies data/pages.json into the database once, keeps every id, and
# records that it did. It never reads the file again.
[import]
pages = "data/pages.json"
# Visitors may read the pages but not change them, and no two pages may share
# a slug: the database itself refuses the second one.
[collections.pages]
create = "none"
update = "none"
delete = "none"
unique = ["slug"]
# Sign-in. The site keeps its own editor accounts in data/app.db. You add one
# from the terminal with `run-what admin add`: there is no built-in account and
# no password in any file of the project.
[auth]
enabled = true
provider = "local"
login_path = "/login"
after_login = "/"
3. Create your account. In the terminal, in the my-site folder, run the command below. It asks for a password twice and shows nothing while you type. Use at least 12 characters, and a password you do not use anywhere else.
run-what admin add editor
It answers Added the editor account `editor`. Sign in at /login. Forgot the password later? run-what admin add editor --reset-password sets a new one.
4. Add the sign-in page. Create site/login.html.
<what>
title = "Sign in | My website"
description = "Editors sign in here."
</what>
<h1>Sign in</h1>
<if query.error>
<p class="form-error" role="alert">That username and password did not match. Try again.</p>
</if>
<form class="sign-in" method="post" action="/w-auth/login">
<input type="hidden" name="redirect" value="#query.redirect#">
<div class="field">
<label for="username">Username</label>
<input id="username" name="username" type="text" autocomplete="username" required>
</div>
<div class="field">
<label for="password">Password</label>
<input id="password" name="password" type="password" autocomplete="current-password" required>
</div>
<button type="submit">Sign in</button>
</form>
5. Show who is signed in. Replace components/layout.html. The header gains a corner that says Sign in, or your name and a Sign out button. Sign-out is a form, not a link: a link could be followed by any page that wanted to sign you out.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="#description#">
<title>#title#</title>
<link rel="stylesheet" href="/static/styles.css">
</head>
<body class="site-body">
<a class="skip-link" href="#main-content">Skip to content</a>
<header class="site-header">
<a class="site-name" href="/">My website</a>
<nav class="site-nav" aria-label="Main navigation">
<a href="/">Home</a>
<a href="/about">About</a>
<a href="/contact">Contact</a>
</nav>
<div class="site-account">
<if user.authenticated>
<span>Signed in as #user.username#</span>
<form method="post" action="/w-auth/logout">
<button type="submit">Sign out</button>
</form>
<else/>
<a href="/login">Sign in</a>
</if>
</div>
</header>
<main id="main-content" class="site-main" tabindex="-1">
<slot/>
</main>
<footer class="site-footer">
<p>Made one page at a time.</p>
</footer>
</body>
</html>
6. Style the new parts. Replace static/styles.css; only the lines at the end are new.
/* Framework styles are supplied automatically. These rules style our site. */
.site-body {
margin: 0;
color: #26372c;
background: #faf8f2;
font-family: system-ui, sans-serif;
line-height: 1.65;
}
.site-header, .site-main, .site-footer {
width: min(100% - 2rem, 48rem);
margin-inline: auto;
}
.site-header {
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: space-between;
gap: 1rem;
padding-block: 1.5rem;
border-bottom: 1px solid #c9cec4;
}
.site-name { font-size: 1.2rem; font-weight: 750; }
.site-nav { display: flex; flex-wrap: wrap; gap: 1rem; }
.site-body a { color: #315f40; text-underline-offset: 0.2em; }
.site-body a:focus-visible {
outline: 3px solid #825414;
outline-offset: 4px;
}
.site-main { padding-block: 2.5rem; }
.site-main h1 { font-size: clamp(2rem, 7vw, 3rem); line-height: 1.15; }
.site-main p { max-width: 65ch; }
.site-main figure { margin: 2rem 0; }
.site-image { display: block; max-width: 100%; height: auto; border-radius: 1rem; }
.site-main figcaption { margin-top: 0.6rem; color: #4b594e; }
.site-footer { border-top: 1px solid #c9cec4; padding-block: 1.5rem; }
.skip-link {
position: absolute;
top: -10rem;
left: 1rem;
padding: 0.6rem 1rem;
background: #fff;
z-index: 10;
}
.skip-link:focus { top: 1rem; }
.card-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 18rem), 1fr)); gap: 1.25rem; }
.content-card { padding: 1.5rem; border: 1px solid #c9cec4; border-radius: 1rem; background: #fffdf8; }
.content-card h2 { margin-top: 0; font-size: 1.4rem; }
.card-note { font-size: 0.9rem; color: #4b594e; }
.visually-hidden {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip-path: inset(50%);
white-space: nowrap;
border: 0;
}
.empty-state { padding: 1rem 1.25rem; border: 1px dashed #c9cec4; border-radius: 1rem; }
.table-wrap { overflow-x: auto; }
.inventory { border-collapse: collapse; width: 100%; min-width: 28rem; }
.inventory caption { text-align: left; padding-bottom: 0.6rem; color: #4b594e; }
.inventory th, .inventory td { text-align: left; padding: 0.6rem 0.8rem; border-bottom: 1px solid #c9cec4; vertical-align: top; }
.inventory thead th { border-bottom-width: 2px; }
.listing-controls { display: flex; flex-wrap: wrap; align-items: end; gap: 0.75rem 1rem; margin-bottom: 1rem; }
.listing-controls .field { display: grid; gap: 0.25rem; }
.listing-controls input, .listing-controls select, .listing-controls button { box-sizing: border-box; min-height: 2.75rem; font: inherit; padding: 0.45rem 0.7rem; border: 1px solid #8a9586; border-radius: 0.5rem; background: #fffdf8; color: inherit; }
.listing-controls input { min-width: min(100%, 16rem); }
.listing-controls button { background: #315f40; border-color: #315f40; color: #fff; cursor: pointer; }
.listing-controls :focus-visible { outline: 3px solid #825414; outline-offset: 2px; }
.result-count { color: #4b594e; }
.pager { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 1rem; margin-top: 1.5rem; }
.pager [aria-current] { font-weight: 650; }
/* L13: the account corner and the sign-in form */
.site-account { display: flex; flex-wrap: wrap; align-items: center; gap: 0.75rem; }
.site-account form { margin: 0; }
.site-account button, .sign-in button { box-sizing: border-box; min-height: 2.75rem; font: inherit; padding: 0.45rem 0.9rem; border: 1px solid #315f40; border-radius: 0.5rem; background: #315f40; color: #fff; cursor: pointer; }
.sign-in { display: grid; gap: 1rem; max-width: 24rem; }
.sign-in .field { display: grid; gap: 0.25rem; }
.sign-in input { box-sizing: border-box; min-height: 2.75rem; font: inherit; padding: 0.45rem 0.7rem; border: 1px solid #8a9586; border-radius: 0.5rem; background: #fffdf8; color: inherit; }
.form-error { padding: 0.75rem 1rem; border-left: 4px solid #a33a2e; background: #fbeeec; }
7. Start the server again with run-what dev.
Check
- Open the home page. The header shows Sign in.
- Follow it, and sign in with
editorand a wrong password. The form comes back with “That username and password did not match.” It does not say which of the two was wrong. - Sign in with the right password. You land on the home page and the header says “Signed in as editor”.
- Press Sign out. The header shows Sign in again.
Exercise
Sign in, then open the site in a second browser, or a private window of the same one. Is the second browser signed in? Explain in one sentence why not, using the word “session”.
If something goes wrong
- The server stops with “provider = "local" keeps the sign-in in a session”. Your
what.tomlturns sessions off. Use the file from step 2. - Sign-in always says the username and password did not match. The account may not exist in this folder's database: run
run-what admin add editoragain in themy-sitefolder. It answers “already exists” if it does; then set a new password with--reset-password. - A form says “This form has expired. Reload the page and try again.” The page was loaded in a session that has ended since: you signed in or out in another tab, the session expired, or it was reset. Reload it and submit again.
- Start again from a known state. Download the L13 checkpoint into a new, empty folder; never unpack it over your project. It contains no database and no account. Run
run-what admin add editorin that folder before you sign in.