Session
Signing in to an account: a token pair, refreshing it, and who is signed in. Both the state and the actions are
outputs of the element itself
:
status:@st
,
user:@user
are read, while
signIn:@signIn
,
signOut:@signOut
publish callbacks that the markup calls by name (
onPressed:(){signIn(…)}
). Outputs are published downwards — an action is available right where it happens, and it is always clear which of the page's sessions is meant.
Type:
multi-child
(a generator, supports lifecycle slots)
@email:""
@password:""
Session baseUrl:"https://api.example.com"
loginPath:"/auth/login" refreshPath:"/auth/refresh" mePath:"/auth/me"
accessField:"access_token" refreshField:"refresh_token" expiresField:"expires_in"
user:@user signedIn:@in status:@st error:@err signIn:@signIn
CircularProgressIndicator id:loading
Column id:signedOut
TextField value:$email
TextField value:$password obscureText:true
ElevatedButton onPressed:(){signIn(email:email, password:password)}
Text $strings.authSignIn
Column id:ready
Text {user['email'] ?? ""}
No defaults for any particular backend. Paths and field names are given every time: the element does not know which server it is talking to. Verbosity is handled at the project level — declare a macro with your values and call it.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
baseUrl
|
string | — | API address; the paths below are appended to it |
loginPath
|
string | — | Where sign-in goes |
refreshPath
|
string | — | Where token refresh goes |
mePath
|
string | — | Who is signed in; asked only when the expiry is unknown |
logoutPath
|
string? | — | Optional; sign-out happens without it too |
accessField
|
string |
access_token
|
Name of the access token field in the response |
refreshField
|
string |
refresh_token
|
Name of the refresh token field |
expiresField
|
string |
expires_in
|
Name of the lifetime field, IN SECONDS |
refreshRotates
|
bool |
true
|
Whether the refresh token is replaced on refresh |
storageKey
|
string |
session
|
The key the pair is stored under — so two services do not overwrite each other |
signedOutRoute
|
string? | — | Where to send someone who is not signed in (websites only) |
transport
|
string? | — (bearer) |
"cookie"
— the session is carried by the server's cookie, not the
Authorization
header: there is no pair, no expiry and no refresh, and
logoutPath
is required. A mode for the site: the app has no HTTP client that keeps cookies
|
csrfCookie
|
string |
csrf
|
In cookie mode: the name of the cookie with the CSRF token (the server sets it without
httpOnly
so the page can read it)
|
Outputs
| Output | Value |
|---|---|
user:@user
|
the
mePath
response or
null
|
signedIn:@in
|
bool |
status:@st
|
checking
,
signedOut
or
signedIn
|
error:@err
|
the code of the last failure or
null
|
pending:@busy
|
bool: whether a request the markup asked for (sign-in, sign-out) is in flight. It has no slots: the sign-in form stays on screen, and the button shows the wait |
accessToken:@t
|
the current access token — for your own requests to protected addresses through `HttpHeaders` |
csrfToken:@csrf
|
the CSRF token in cookie mode, for a header (
HttpHeaders X-CSRF-Token:$csrf
). It has a value only on the site: the app has no way to read the cookie
|
signIn:@signIn
|
sign-in callback, see below |
signOut:@signOut
|
sign-out callback |
refresh:@refresh
|
"re-check the session now" callback |
acceptPair:@acceptPair
|
callback that accepts a pair issued by something other than sign-in |
Three states, not two
While the saved pair is being checked, it is
not yet known
whether the person is signed in. Treating that as "not signed in" means flashing the sign-in form at someone who is already signed in — on every launch. That is why
status
has
checking
, and the slots by
id:
mirror it:
loading
,
signedOut
,
ready
.
Error codes
error
carries the name of a state, not a phrase: in what words and in what language to show it is up to the markup, through
$strings
.
| Code | What happened | What to do |
|---|---|---|
badCredentials
|
sign-in was rejected | say so, keep what was typed |
signInFailed
|
something else broke during sign-in | offer to retry |
refreshRejected
|
the refresh token is dead — the session is over | show sign-in |
accountBlocked
|
the ACCOUNT was refused: blocked, email not confirmed | say which; signing in again will not help |
refreshUnavailable
|
the network or the server failed — this says NOTHING about the token | the session is kept, you can retry |
The last two are told apart because they are handled in opposite ways. A lost packet is not a dead token: mistaking one for the other, the app throws the person out of an account that was fine.
Restoring without a request
An accepted session is saved whole — the pair, the expiry and the user. On the next launch an unexpired session is accepted without a single request ; an expired one is refreshed right away instead of asking "who am I" only to hear 401. The element asks only where the backend does not report an expiry.
The expiry is taken from the response (
expiresField
), not by parsing the token: the token may be opaque, and the element knows nothing about which server it got.
The price: a token revoked on the server (sign-out on another device, a block) is noticed no earlier than the first real request returns 401 — through the path written for exactly that.
Actions:
signIn
,
signOut
,
refresh
,
acceptPair
Actions are callbacks the element puts into the named variables. They are called from a handler:
| Output | Call | What it does |
|---|---|---|
signIn:@signIn
|
signIn(email:email, password:password)
|
Sign-in. The named arguments are the body of the request to
loginPath
: every backend has its own field names
|
signOut:@signOut
|
signOut()
|
Sign-out: the pair is deleted even if telling the server failed |
refresh:@refresh
|
refresh()
|
Re-check the session now; rarely needed — an expired token is refreshed by itself |
acceptPair:@acceptPair
|
acceptPair(body: response)
|
Accept a pair from the body of a response not issued by sign-in (for example, after a password change): fields are taken by
accessField
/
refreshField
/
expiresField
|
@email:""
@password:""
Session baseUrl:"https://api.example.com" loginPath:"/auth/login" refreshPath:"/auth/refresh" mePath:"/auth/me"
status:@st user:@user pending:@busy signIn:@signIn signOut:@signOut
CircularProgressIndicator id:loading
Column id:signedOut
TextField value:$email
TextField value:$password obscureText:true
ElevatedButton onPressed:(){signIn(email:email, password:password)}
Text {busy ? "Signing in…" : "Sign in"}
Column id:ready
Text {user['email'] ?? ""}
TextButton onPressed:(){signOut()}
Text "Sign out"
Call them only from a handler:
onPressed:(){signIn(…)}
. A call in
{…}
is a value expression; it fires once, when the screen appears.
On the site
The same element works in
kind:website
, with two differences.
Account sections live as
separate pages
with real addresses, and
signedOutRoute:"/signin"
sends there anyone who turns out not to be signed in. This is a convenience, not access control: the page markup is served to anyone, there is nothing private in it, and the data is protected by the server.
Token refresh is one for all tabs : platforms that rotate refresh tokens treat a token presented twice as compromised and revoke the whole family — so two tabs refreshing at the same moment throw the person out of the account in both. The site takes care of this itself.