Actions and forms
An action is a function that runs on the server when a form is submitted. It is exported from a route file and named by the form that posts to it.
Declare one
import { action } from "albedo";
export const sign = action(({ form }) =>
append("guestbook", { author: form.author, message: form.message }),
);
export default function Guestbook() {
return (
<form action="action:sign" method="POST">
<input name="author" />
<input name="message" />
<button type="submit">Sign</button>
</form>
);
}action="action:sign" names the export. form holds the submitted fields by their name, and every value is a string: an <input name="id"> gives "3", not 3.
The form works with JavaScript off. With it on, the submit is sent without a page load. CSRF protection is added to every same-origin form for you.
Answer the form
What the action returns is written into the form:
return { error: { email: "That doesn't look like an email address." } }; // next to that field
return { ok: "You're on the list." }; // the form's success line
return {}; // clears bothThe compiler reserves a slot for each field's error and one for the success message inside the form. Style them with [data-albedo-error] and [data-albedo-ok]; the success slot is a role="status" region, so screen readers announce it. An error clears a stale success message, and a success clears stale errors.
With JavaScript off, the message rides the redirect back and the page lands on the form.
After a submit that succeeds, the form is cleared — its fields go back to what the page first rendered, the same as the fresh page JavaScript-off readers get. A submit that comes back with a field error keeps what was typed, so it can be fixed. A form rendered inside a list's .map(...) is never cleared: field errors are not shown in repeated forms, so a refusal there could not be told from a success. A form that should keep its values after saving, such as an edit form, says so with data-albedo-keep:
<form action="action:rename" method="POST" data-albedo-keep>While a submit is in flight the form carries aria-busy and data-albedo-state="busy", and a second press sends nothing. If the request never reached the server, the form gets data-albedo-state="failed".
Check on the server
The browser's checks (type="email", required) are a courtesy; the action is where the rule holds, because anyone can post to it directly:
export const join = action(({ form }) => {
const email = String(form.email ?? "").trim().toLowerCase();
if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) {
return { error: { email: "That doesn't look like an email address." } };
}
append("waitlist", { email, joined_at: Date.now() });
return { ok: "You're on the list." };
});That is the release list on this site's front page.
Who is asking
user is the signed-in person, or null when nobody is:
export const keep = action(({ form, user }) => {
if (!user) return { error: { body: "Sign in to keep notes." } };
append("notes", { owner: user.id, body: form.body });
return { ok: "Kept." };
});It is the same value a page's user prop gets: id, plus email, name and image (each null when the sign-in provider didn't supply it), the provider this session signed in with, and providers, every sign-in method connected to the account.
Checking user in the body is for the message. The rule holds anyway: a write to a collection partitioned by owner is refused unless the row's owner is the person asking.
Go somewhere else
return { redirect: "/account" };A path in this app is always allowed. Another site is allowed only when its https:// origin is listed in albedo.config.ts:
redirects: ["https://checkout.stripe.com"],Any other destination fails the action rather than sending anyone there, so a URL built from a form field can't become an open redirect. The check runs after the body, so anything the body wrote has already been written. A result that also carries an error stays on the form. With JavaScript off, the redirect is a plain 303.
Call another service
fetch works in an action body, the way the platform's does. When the service needs a secret key, name it in albedo.config.ts and leave it out of the code:
sources: {
stripe: { base: "https://api.stripe.com", auth: { bearerEnv: "STRIPE_SECRET_KEY" } },
},A service that signs in with a key id and a secret (HTTP Basic, like Razorpay) names both variables:
sources: {
razorpay: {
base: "https://api.razorpay.com",
auth: { basicEnv: { user: "RAZORPAY_KEY_ID", password: "RAZORPAY_KEY_SECRET" } },
},
},A source sends exactly one credential, so bearerEnv and basicEnv together fail the build, and so does a key inside auth that ALBEDO doesn't know — a misspelt one would otherwise send every call without the key.
Every fetch aimed at that origin carries the key, attached by the server after the body has run. The key never passes through your code, so no package can read it and no log of the call records it. A call that sets its own authorization header for that origin is refused: two credentials on one request is always a mistake.
When the other service needs an absolute URL back to you, like a payment provider's return page, build it on origin:
export const pay = action(async ({ user, origin }) => {
// …create the checkout, with success_url: origin + "/account"
});origin comes from configuration (ALBEDO_PUBLIC_ORIGIN, the --domain you serve, or localhost in development), never from the request's Host header, which the caller controls. It is null when nothing configures one.
Secrets are read from the environment, and the server refuses to start while one is missing. In development, albedo dev also reads .env.local and .env from the project, and never overrides a variable the shell already set. albedo serve reads only the real environment.
What an action can do
- Write to collections with
append,updateandremove. - Call
fetchto reach another service. - Queue background work, and send email when the app declares an
emailblock.
Next: styling.