Sidebar

Displays the mobile sidebar.

Search the docs

Search the docs

charter
charter
What charter is
See it running
Where to go
What an app made with charter looks like
Getting started
Getting started
Working on it
Before a push
Shipping it
Tools
Every command
The commands, and what each one proves
The commands, and what each one proves
What is checked, and when you find out
What no compiler checks
create
dev
add
check
test
deploy
pack
run
The rest
An app's layout
An app's layout
What the Worker is bound to
main.go
What is generated
What is never in git
Pages
Pages
With the commands
The tree
A page declares what it needs
A region that changes by itself
The page being shown
A fragment opened as a page
What a page calls
Links
A link shows the next page with no page load
Which pages search engines get
A page that is the same for every reader
The head
The API
The API
With the commands
An operation
Who may call
Limits
SDKs
MCP
The live feed
The live feed
With the commands
Writing
Three ways out, one feed
A page's stream holds no Go
Resuming
The database
The database
With the commands
Changing it
Queries
Every statement is checked
One definition
Seeding
A deployed database
Reads near the reader
By hand
Signing in, and who may do what
Signing in, and who may do what
With the commands
One caller
Saying what is needed
Roles
The first admin, and inviting people
Signing in
Another device
An address to get back in by
Testing it
The clients
What is left to you
Languages and search engines
Languages and search engines
With the commands
Languages
Search engines
Agents
Screens and themes
Screens and themes
With the commands
The interface is gsxui's
Screens
The layout
What a page is made of
Themes
The editor, in your app
A style
While developing
Light and dark
What is checked, and what is not
Docs
Docs
With the commands
Groups, and the order
What you write, and what charter writes
Links are checked
Showing them
What comes with them
What the pages have
Code taken from a real file
Tests
Tests
A page, as plain Go
The whole app, on a real Worker
A page, in a real browser
What to test, and what not to
The API, through its own SDK
Where a test runs
A test leaves the Worker as it found it
Secrets a test needs
What else charter test does
Deploying
Deploying
Two Workers
With the commands
The plan
Nothing of your account in the repository
Credentials
Secrets
Resources
Taking an app away
Your own domain
Email
After a deploy
Environments
Everything Cloudflare gives a Worker
The admin screen
The admin screen
The pages
The app's own facts
From a program
What it does not do
Watching a deployed Worker
Watching a deployed Worker
Is it well?
Three numbers
Cloudflare's limits
Under load
A first hit
Every data centre, by country
What happened?
A failure has a reference
Memory
What charter leaves to Cloudflare
Upgrading
Upgrading
0.82
0.81
0.80
0.79
0.78
0.77
0.76
0.75
0.74
0.73
0.72
0.71
0.69
0.65
0.64
A binding, a variable, a secret's name
A scope
Screens
0.63
When upgrade stops
For your coding agent
For your coding agent
The plugins
In an app made with charter
Anywhere else
One set of commands
What an agent can ask
What a language model reads
What the Worker is bound to
What the Worker is bound to

Signing in, and who may do what

An app has one answer to "who is asking, and may they?", for its pages and for its API alike.

With the commands

WhenCommandWhat it does
You want people to sign inadd screen:sign-inthe sign-in, account and people pages, and their tables in db/schema.sql
The first adminrun people:invite admina link that makes whoever signs up through it an admin, once, within the hour
To see who there isrun peopleeveryone who signed up, and what each is
Someone has no device left to sign in withrun people:device <name>a link: the device that opens it makes a passkey for their account, once, within the hour
Before a pushchecka scope is made only where it is declared; your schema still has the sign-in's tables as charter uses them; the role new people get is one of your roles
Before a pushtestsigning up and in with a passkey in a real browser, as each role, on the pages and through the API

Checked before the app runs: every scope a page, an operation or a rule names (a scope is a value: a misspelt one does not compile); the tables charter's sign-in reads and writes, column by column.

Not checked until it runs: that a role grants what you meant it to, which is your decision and what the tests of each role are for. A role's name is still a word ("moderator"): only the one new people get is checked, when the app starts.

One caller

example/api/handlers.go
// Auth is that, for the whole app: the API's operations are checked by it (API), and so are the pages (main.go
// gives it to app.App). So a page that needs Write and the operation that needs Write ask the same thing of
// the same caller.
var Auth = charter.NewAuth(cloud.Var, Trusted...)

api.Auth works out who a request comes from, whatever it carries:

The request hasThe caller is
The session cookie of someone who signed in on a pageThat person
Authorization: Bearer with a token that person madeThat person, with the token's scopes
Authorization: Bearer with one of the Worker's secretsA program, with that secret's scopes
Cloudflare Access's token, or an OpenID Connect issuer'sWhoever those vouch for

The API's operations are checked by it, and so are the pages: main.go gives the same Auth to both.

Saying what is needed

What someone may do is a scope. Each is declared once, as a value, where the app says who may do what:

example/api/handlers.go
// What someone may do: each scope is declared here, once, and named everywhere else (a page's Needs, an
// operation's Security, Caller.May). A scope is a value, not a word, so one that is misspelt does not compile.
// charter.Admin is charter's own: whoever has it says who is what, and sees the admin screen.
var (
	Read     = charter.NewScope("read")
	Write    = charter.NewScope("write")
	Moderate = charter.NewScope("moderate")
)

An operation says what it needs in the contract. A page, or a group of pages, says it with a method. Both name the same value:

// api/contract.go
Security: charter.Needs(Write),

// pages/pages.go
func (PostNote) Needs() []charter.Scope { return []charter.Scope{api.Write} }

A scope is a value and not a word so that it cannot be written as one. Needs: "wrtie" would compile, name a scope no role grants, and shut everyone out of the page with nothing to say why. As it is, a scope that is not declared is a name that does not exist, and the compiler says so. charter check fails an app that makes one anywhere but in its declaration (its part scopes).

charter.Admin is charter's own scope: whoever has it says who is what and sees the admin screen. Your roles grant it to whom they will.

Needs on a group of the tree covers every route under it. A group that returns nil needs someone signed in, whoever they are.

The callerAn operation answersA page answers
Is nobody401Sent to the sign-in page, then back to where they were going
May not403403
MayThe resultThe page

A page that shows something of the caller declares them, as it declares the store:

example/pages/pages.go
// Props loads what the request wants, as structpages has it. A row's button asks for that row alone (its target is
// Home's Item, with the note's id as its key): then one note is read, and structpages renders Item with it. The
// list asks for itself when its stream says the notes changed (its target is Home's List): then the newest notes
// are read, and structpages renders List with them, for this reader. Anything else is the page. Each component
// takes its share of the results by their types.
func (p Home) Props(r *http.Request, store api.Store, caller charter.Caller, target structpages.RenderTarget) (HomeProps, []api.Note, charter.Caller, api.Note, Open, Removable, Kept, error) {
	if target.Is(p.Item) {
		id, err := strconv.ParseInt(structpages.KeyOf(target), 10, 64)
		if err != nil {
			return HomeProps{}, nil, caller, api.Note{}, false, false, false, app.NotFound("no such note")
		}
		note, err := store.Get(r.Context(), id)
		if err != nil {
			return HomeProps{}, nil, caller, api.Note{}, false, false, false, app.NotFound("no such note")
		}
		return HomeProps{}, nil, caller, note, r.URL.Query().Get("open") != "", Removable(api.MayRemove(caller, note)), false, nil
	}
	notes, err := store.Before(r.Context(), math.MaxInt64, shown)
	if err != nil {
		return HomeProps{}, nil, caller, api.Note{}, false, false, false, err
	}
	if target.Is(p.List) {
		return HomeProps{}, notes, caller, api.Note{}, false, false, false, nil
	}
	props := HomeProps{Notes: notes, MayWrite: caller.May(PostNote{}.Needs()...), Caller: caller}
	// One read of KV, for every reader: what it is for.
	if props.Notice, err = built.NoticeNow(r.Context()); err != nil {
		return HomeProps{}, nil, caller, api.Note{}, false, false, false, err
	}
	return props, notes, caller, api.Note{}, false, false, false, nil
}

In a component, caller.May(api.Write) decides what to show: the form, or the way to sign in. Hiding a button protects nothing; the check on the route does.

Roles

What people may be is said once, as names and what each may do. A person has one role; someone who signs up gets the first you name.

example/api/handlers.go
// Roles are what the app's people may be, and what each may do. Someone who signs up is a member. A moderator
// may also take anyone's note away; an admin may also say who is what.
var Roles = map[string][]charter.Scope{
	"member":    {Read, Write},
	"moderator": {Read, Write, Moderate},
	"admin":     {Read, Write, Moderate, charter.Admin},
}

The role someone who signs up gets (Role: "member") must be one of these: an app whose Role is none of its Roles does not start, which charter check finds as it writes the specs.

A role changes for everyone who has it when you change it here, and a person's changes at their next request: in the session they have open, and for the tokens they made before.

A rule that is about more than a scope is a function, asked by the page and by the API alike:

example/api/handlers.go
// MayRemove is the rule for taking a note away, said once: whoever wrote it may, and so may whoever may moderate.
// The page asks it to show the button, the page's route and the API's deleteNote ask it before they act.
func MayRemove(who charter.Caller, note Note) bool {
	return who.May(Moderate) || (who.SignedIn() && who.Subject != "" && who.Subject == note.AuthorID)
}

In the example a note says who wrote it. Its writer may take it away, and so may a moderator: the page shows the button to exactly those, and the page's route and the API's deleteNote ask the same function before they act.

Who is what is an admin's to say: on the people page (from their account), and through the API (listPeople, setRole), so the generated clients and the command-line client can do it too.

The first admin, and inviting people

Nobody is an admin until someone is invited to be. An invitation is a link: whoever signs up through it, once, within the hour, is what it says.

go tool charter run people:invite admin              # under charter dev
go tool charter run people:invite admin --deployed   # on the deployed Worker
go tool charter run people                           # who signed up, and what each is

Open the link, give a display name, and your device makes the passkey: you are signed in as an admin. The link's page says what the invitation is to, and one that was used or is too old says so. Someone who has an account already, and is signed in, gets a button there instead: "Accept the invitation". From then on you invite others on the people page ("Invite someone"), to any role, or change the role of someone who signed up already. Under a new invitation's link is a Share button: the device's own share sheet on a phone, the clipboard elsewhere.

On the people page an admin also takes a person away (the bin on their row, asked first): what your app keeps of theirs goes as it does when they delete their own account (Leaving). An admin's own row has no bin; their account is theirs to delete, on their account page.

An invitation is kept as a started sign-in is, by the SHA-256 of its code, so the database has nothing that invites anyone. A program does the same with auth.Invite(r, role).

Signing in

People sign in with a passkey: the device keeps the key and asks its owner (a finger, a face, a PIN). There is no password anywhere.

go tool charter add screen:sign-in

That brings the sign-in page, the account page, the people page and their routes, built from your app's gsxui components, and writes four tables into db/schema.sql: users, their passkeys, their tokens, and what was started and not finished (a sign-in, an invitation, a device link, a confirmation).

// pages/pages.go
signIn  screens.SignIn  `route:"/sign-in SignIn"`
account screens.Account `route:"/account Account"`

// main.go
Auth:        api.Auth,
SignIn:      screens.SignInPage{},
Args:        []any{api.Auth},
RequestArgs: []any{api.OpenStore, api.Auth.Caller},

<screens.AccountLink/> in a header says who is signed in, or leads to signing in.

The name someone gives when they make an account is a display name: it is shown to everyone, beside what they write. The field says so, and an email address is refused there, by the browser and by the Worker.

Someone signed in has a session: a token the app issued, kept by their browser as a cookie that scripts cannot read. On their account page they make tokens for their programs, each with a name; a token is shown once, and the database keeps only its SHA-256. A program sends one as Authorization: Bearer, and is that person.

At the foot of the account page someone can delete their account: asked twice, the second time with what goes. Their passkeys, sessions and tokens go with it, and so does what your app keeps of theirs, which you say once:

charter.People{Name: "Notes", Accounts: charter.D1Accounts(env.DB), Roles: Roles, Role: "member", Leaving: Leaving, Mail: env.MAIL},

The example takes away the notes they wrote, what the activity page recorded of those, and the files they attached. Only the person can do this, signed in on the page: a program with one of their tokens cannot.

A passkey belongs to a host name. Under charter dev that is localhost; an IP address has none.

Another device

A passkey stays with the device that made it, or with the devices of one maker: Apple's keychain takes it to that person's other Apple devices, Google's to their Android phones and their Chrome, and one a Windows computer made is usually that computer's alone. So someone who signed up on a Windows computer has nothing to sign in with on their iPhone, and an account that could only ever have the passkey it was made with would be an account for one device.

An account has as many passkeys as it has been given devices, and there is a way to the next one from wherever someone is:

Where they areWhat they doWhat happens
Signed in on one device, wanting another"Another device" on their account page makes a link; they open it on the other device (Share hands it to the phone's own share sheet)That device makes a passkey of its own for the account, and is signed in
At a computer that is not theirs, phone in handSign in, and choose the phone in the browser's own dialog (a QR code)Signed in with the phone's passkey; nothing is kept on the computer
With no device left, having given an address"No device to sign in with?" on the sign-in page, and their addressThe same link, sent to that address
With no device left, and no addressAn admin presses the key on their row of the people page, or you run people:deviceThe same link, made for them: it is shown once, to hand to them

The account page lists the account's passkeys, each with the browser and the system that made it ("Chrome, Windows"), the day it was made and the day it last signed in. One on a device that is gone is taken away there. The last one stays: without it there is no signing in, and an account is deleted as a whole.

go tool charter run people:device Ada              # under charter dev
go tool charter run people:device Ada --deployed   # on the deployed Worker

A device link is kept as an invitation is: by the SHA-256 of its code, once, for an hour. Whoever opens it is signed in as that person, so it is shown once, to whoever asked for it, and an admin who makes one for someone else can be them: that is what a way back in is, and why only whoever may manage people has it. A device that already holds a passkey of the account says so and makes no second.

An address to get back in by

Someone may give an email address on their account page ("A way back in"). It is for one thing: with no device left, the link is sent there. It is no name to sign in with, the sign-in page never asks for it first, and it is shown to no one, an admin included.

WhatHow
The address is theirsA confirmation goes to it, and it is kept once its link is opened. An address mistyped would otherwise be a stranger's way in
Nobody learns whose address is knownThe sign-in page answers the same for an address someone gave and for one nobody did
Nobody fills an inbox from hereOne confirmation and one way back in go to an address in five minutes
The message is in their languageIt is written by the sign-in screen, from the app's catalogs, in the language of the page that sent it

The app sends it through its send_email binding, which you hand to its people: Mail: env.MAIL, in the line above that says what the app's people are. It writes from sign-in@ the name it is served under, so this is there once the app is on a domain of its own, which charter deploy has Cloudflare send mail for. Under workers.dev an app has no address to write from: the account page has no such panel, and the sign-in page says to ask whoever runs the app.

In a test, what the app sent is read where wrangler dev keeps it: apptest.Mailed(t, address) is the link in the newest message to an address. The example's TestAnAddressGetsSomeoneBackIn goes the whole way: an address given and confirmed, a link asked for from a browser that has never been there, and the old passkey taken away.

Testing it

TestHow it has a caller
A page's logic (pages/pages_test.go)The test hands the page a caller, as it hands it a store
A page in a browserpage.SignUp(path, "Ada"): Chrome's own virtual authenticator makes the passkey, in a fraction of a second
The APIA bearer token: one of the Worker's secrets

The example holds both sides to one rule in test/page_signin_test.go: nobody is refused by the page's form and by the API's createNote; someone signed in is let through by both, and so is the token they made. A member may not remove another's note, on the page or through the API; made a moderator, they may, through both, at once.

The clients

Every client Fern generates is run against the Worker itself (go tool charter pack -prove), by the program in test/sdk/<language>/: a token nobody issued is refused, a token that may only read may not write, a token that may write does, and what it wrote is read back. Go, Python, TypeScript and the command-line client each prove it in their own language.

What is left to you

  • Someone with no device, no address and no admin to ask. There is nothing of theirs left to know them by. Say so where people sign up, if your app's people would not think to give an address.
  • More than one role each, groups, teams. A person has one role. Anything richer is your app's own tables and its own rule, written as MayRemove is.