These ten demonstrations are the legacy tutorial. Preview the new cumulative course; its later lessons are still in development.
Step 9: Authentication
Editor accounts, a sign-in page and protected folders, all declared in your project's files.
How sign-in works
The course site keeps its own editor accounts in data/app.db. You add an account from the terminal; there is no built-in account and no password in any file of the project.
When you sign in, What starts a new session on the server and sends your browser a w_session cookie that names it. The session records which account signed in, and What looks that account up again on every request. The cookie holds no name, role or password, only the session's id. That is how the browser stays signed in, for up to a week (max_age = 604800 seconds). Sign out and What deletes the session. Reset an account's password and every browser signed in to it is signed out.
The configuration in what.toml
These are the two sections from the course site. Sign-in needs sessions, so both are on.
[session]
enabled = true
cookie_name = "w_session"
max_age = 604800
database = "data/sessions.db"
[auth]
enabled = true
provider = "local"
login_path = "/login"
after_login = "/"
login_path is where a signed-out visitor is sent. after_login is where a sign-in lands when it has no page to return to.
Add an account and protect a folder
In the terminal, in your site's folder, create the editor account. It asks for a password twice and shows nothing while you type. The full walk-through is in Give the site an administrator.
run-what admin add editor
Then one file protects a whole folder: every page in site/admin needs the role editor, which that account has. A signed-out visitor is sent to the sign-in page. Protect the editing area and its data covers it in full.
layout = "components/admin-layout.html"
title = "Admin | My website"
auth = "editor"
Sign in and sign out
This is the sign-in form from the course. It signs you in to the site you are building on your own computer, not to this website.
<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>
The hidden redirect field brings you back to the page you asked for, such as /admin.
The layout shows who is signed in, with a sign-out button:
<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>
Try it on your computer
Use your course site, or download the L14 checkpoint into a new, empty folder. The checkpoint has no account yet: run run-what admin add editor in that folder, then run-what dev. In your browser:
- Signed out, open
http://127.0.0.1:8085/admin. You are sent to the sign-in page. - Sign in as
editorwith a wrong password. You see “That username and password did not match. Try again.” - Sign in with the right password. Since What 2.0.0 you land on the home page, not on
/admin: after a failed attempt the sign-in page no longer remembers where you were going. The header now says “Signed in as editor”. - Open
/adminagain. It opens, because you are signed in. - Press Sign out, then open
/adminonce more. You are sent to the sign-in page again.
Signed in at the first try? Then step 3 takes you straight back to /admin. Sign-in allows five attempts a minute from one computer; wait a minute if it refuses.
[auth]withprovider = "local"plus[session]inwhat.tomlturns on sign-in with the site's own accountsrun-what admin add editorcreates an account; no password lives in the project's files- The browser stays signed in through a
w_sessioncookie that names a session on the server; signing out deletes it auth = "editor"insite/admin/application.whatprotects the whole folder- Sign in: a form posting
usernameandpasswordto/w-auth/login. Sign out:POST /w-auth/logout <if user.authenticated>and#user.username#show who is signed in