auth.ts
app/auth.ts turns on sign-in for the app with a few lines of configuration. From it manh.run generates the users table, the sign-in API and the user management page.
app/
├── auth.ts # roles, sign-up, Google
├── users/users.table.ts # [generated] users table
└── shared/auth-roles.ts # [generated] ROLES, ADMIN_ROLE, ROLE_LABELS…What it does
Sign-in is configuration, not code you write. With this file every request carrying a valid token has user = { id, name, email, role, avatarUrl }, and every handler needs a signed-in user unless its access is "public".
manh.run only reads this file and never runs it, so use literal values only.
Exports
default, an object:
roles: the role list. The first role manages users. It goes only to the first account registered from the owner’s preview in the workspace, while the app has nobody with that role. On the deployed app a sign-up always getsdefaultRole, so a stranger can’t register first and take admin.defaultRole: the role every other sign-up gets.signup:"open"(anyone can register) or"closed"(only the first admin signs up, from the preview; admins create the other accounts).providers(optional):["google"]adds Sign in with Google. The sign-in starter already writes it; drop it only when the owner wants passwords only.labels(optional): role names shown on screens, e.g.{ owner: "Shop owner", technician: "Technician" }. Common roles (admin, manager, staff, cashier…) already have Vietnamese names.profile(optional): extra user fields, e.g.{ branchId: "int", phone: "text" }; typesint,text,real,bool, up to 12 fields.
Example
app/auth.ts
export default {
roles: ["admin", "manager", "staff"],
defaultRole: "staff",
signup: "closed",
providers: ["google"],
labels: { manager: "Store manager" },
profile: { branchId: "int" },
};What manh.run generates:
- APIs
POST /api/auth/login,POST /api/auth/register(return{ token, user }),GET /api/auth/me,GET/PATCH /api/auth/profile(users edit their own name and avatar),POST /api/auth/password(change their own password),GET /api/auth/providers,GET /api/users/members, plusGET /api/users,POST /api/users,PATCH /api/users/:idfor the first role. - The table
app/users/users.table.tswith columnsid, email, passwordHash, name, role, active, avatarUrl, createdAtplus theprofilefields. app/shared/auth-roles.tswithROLES,ADMIN_ROLE,DEFAULT_ROLE,SIGNUP,PROFILE_FIELDS,ROLE_LABELS.
GET /api/users/members returns an array of active users to anyone signed in, for assignee pickers. Text profile fields (phone…) are left out.
Good to know
- The extension must be
.ts.app/auth.tsxis ignored andcheckreportsconfig.wrong-extension. A malformed config:auth.config-invalid. - Roles in
accessmust be inroles, orcheckreportsaccess.unknown-role. - After 5 wrong passwords for one e-mail, or 30 from one IP, within 15 minutes,
POST /api/auth/loginanswers429 TOO_MANY_ATTEMPTS. An unknown e-mail takes as long as a wrong password, so answers never reveal which e-mails exist. - With
signup: "closed", the starter’s/userspage has Thêm tài khoản (add account): that is where the owner gives each employee a sign-in. With Google, only e-mails an admin already added can get in. - To try each role before handover, the agent uses
create_test_accounts, thenremove_test_accounts. Never promote an account with SQL. - To change a generated piece, write your own file at that path; the project file wins. Keep the generated columns.
- Don’t use
manh/oauthor cookie sessions. The token is a JWT sent in theAuthorizationheader.
Next steps
- Sign-in and permissions — set
accesson handlers and show the session in the UI. - Sign-in and sign-up pages — build a sign-in screen that looks like a real product.
- middleware.ts — lock a whole API folder to one role.
Last updated