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.

what.toml
[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.

site/admin/application.what
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.

site/login.html
<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:

components/layout.html (part)
<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:

  1. Signed out, open http://127.0.0.1:8085/admin. You are sent to the sign-in page.
  2. Sign in as editor with a wrong password. You see “That username and password did not match. Try again.”
  3. 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”.
  4. Open /admin again. It opens, because you are signed in.
  5. Press Sign out, then open /admin once 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.

What you learned
  • [auth] with provider = "local" plus [session] in what.toml turns on sign-in with the site's own accounts
  • run-what admin add editor creates an account; no password lives in the project's files
  • The browser stays signed in through a w_session cookie that names a session on the server; signing out deletes it
  • auth = "editor" in site/admin/application.what protects the whole folder
  • Sign in: a form posting username and password to /w-auth/login. Sign out: POST /w-auth/logout
  • <if user.authenticated> and #user.username# show who is signed in