Sign-in
Every app has sign-in for its users from day one: email and password, a sign-in link by email, Google, and GitHub with your own OAuth app. You decide who may sign up and as which role; staff are invited. You write no auth code and store no passwords.
What your users get
| Way in | How it is turned on |
|---|---|
| Email and password | On unless auth.providers leaves "password" out. Passwords are hashed and kept by the platform; resets by email |
| Sign-in link by email | On unless auth.providers leaves "magicLink" out. It signs in an account that exists; it doesn't make one |
"google" in auth.providers | |
| GitHub | Your own GitHub OAuth app, with the sign_in_providers tool |
| Invites | app_users { "action": "invite" }: the person gets a link to choose a password |
Code
// app.config.ts
export default defineApp({
brand: { name: "Stillwater", tagline: "Yoga and pilates in Lisbon" },
auth: {
providers: ["password", "google", "magicLink"], // the ways in; one left out is off
signup: { as: ["member"] }, // who may sign themselves up, and as what
admin: "instructor", // the staff role of the built-in admin sign-in
},
roles: {
member: { kind: "consumer", label: "Member", profile: { table: "member_profile", create: true } },
instructor: { kind: "staff", label: "Instructor" },
},
});Without auth.providers, passwords and sign-in links are on and Google is off. Open sign-up needs password or google; the check says so if neither is on.
Who may sign up
auth.signup | Means |
|---|---|
{ as: ["member"] } | Visitors sign themselves up as a member. With several roles the sign-up names one; default is the one used when it doesn't (else the first) |
"invite" | Nobody signs themselves up: people are invited (client portals, internal tools) |
| left out | The first consumer role may sign up; staff are invited |
A staff role named in as may sign itself up too. The rule covers every way in, Google included.
Profiles: a row per person
A role's profile table links each account through its user_id column, and ctx.profile_id('member') finds the signed-in member's row for your policies.
profile: "member_profile"only links: a new account takes an unlinked row (user_idnull) whose email column matches its email. Your code (a sign-up action, a trigger) creates rows for everyone else.profile: { table: "member_profile", create: true }has the platform also insert the row (user_id, the name and the email) at sign-up, a first Google sign-in, an invite or a demo sign-in. Every othernot nullcolumn needs a default; the check fails at the role's line otherwise.- Demo sign-ins: every apply makes one per role. In dev, each takes its role's first unlinked row that no email is waiting for, so seed one profile row without an email for it.
A sign-up page
/signin is built in; a sign-up form is yours to write. It is a few lines with useAuth:
// routes/signup.tsx
import { useState } from "react";
import { defineRoute, useAuth, useNavigate } from "@appbricx/ui";
export const meta = defineRoute({ public: true, title: "Create your account" });
export default function SignUp() {
const auth = useAuth();
const navigate = useNavigate();
const [form, setForm] = useState({ name: "", email: "", password: "" });
const [error, setError] = useState("");
const submit = async () => {
try {
await auth.signUp({ ...form, role: "member" });
navigate("/");
} catch (e) {
setError(e instanceof Error ? e.message : String(e));
}
};
// a form with Name, Email and Password fields that calls submit()
}The full page is in the todo tutorial.
Turn on Sign in with Google
- Add
"google"toauth.providersand apply. The sign-in screen now offers Continue with Google. - Where the platform has a shared Google client, that works with no setup, for testing. Google's consent screen then names Appbricx, and verify warns on production.
- Before launch, create a Google OAuth client of type "Web application", register this redirect URI for each host your users sign in on, and give the platform the client:
The id and secret are kept as the project secretshttps://<your-app-host>/__appbricx/auth/oauth/google/callback sign_in_providers { "project_id": "…", "provider": "google", "client": "own", "client_id": "…apps.googleusercontent.com", "client_secret": "…" }GOOGLE_CLIENT_IDandGOOGLE_CLIENT_SECRET.
GitHub works the same way with "provider": "github" and the callback /__appbricx/auth/oauth/github/callback; it needs your own GitHub OAuth app.
Your own sign-in screen
Write routes/signin.tsx and it replaces the built-in screen. useAuth() signs in and out and sends sign-in links; useAuthOptions() says which ways in are on; useAppHandle().auth.startProvider(name) starts Google or GitHub.
const auth = useAuth();
const options = useAuthOptions(); // null until the browser has asked
const app = useAppHandle();
await auth.signIn(email, password);
await auth.requestSignInLink(email);
{options?.providers.map((p) => (
<button key={p.name} type="button" onClick={() => void app.auth.startProvider(p.name)}>
Continue with {p.label}
</button>
))}Or keep the platform's form inside your own page: <SignIn next="/my-classes" />.
Two-factor codes
Not on code-first app screens yet. signIn rejects an account that has a second factor with "This account needs a second sign-in step, which this screen doesn't offer yet."
Teams, invites and organisations
- Invite with
app_users{ "action": "invite", "email": "…", "name": "…", "role": "instructor" }: the link is single use and lasts an hour, and is never shown to you. An invite claims an unlinked profile row with the same email. - Manage with
set_role,disable,enable,reset_passwordandsign_out. Accounts are shared by dev and production: a change that reaches production asks for the deploy scope anddry_run: false. - In the app,
<TeamAdmin />from@appbricx/uiis a roster page for the app's admin. - Data per organisation is policies over a membership table: see access and roles.
Check it
sign_in_providers { "project_id": "…" } // Google and GitHub, on or off, and whose client
app_users { "project_id": "…", "action": "list", "role": "member" } // accounts and the profile row each is linked to
screenshot { "project_id": "…", "route": "/signin", "as": "guest" }Gotchas
- Each custom domain needs its own redirect URI when you use your own Google or GitHub client.
- Keep the "Email" and "Password" labels and a "Sign in" button on your own screen: screenshots and verify fill fields by their labels.
- A new Google user takes the sign-up role (the default of
auth.signup), as an email sign-up would. - A sign-in link signs in, it doesn't sign up. An app with only
magicLinkneeds invites.