Skip to Content

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 gets defaultRole, 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" }; types int, 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, plus GET /api/users, POST /api/users, PATCH /api/users/:id for the first role.
  • The table app/users/users.table.ts with columns id, email, passwordHash, name, role, active, avatarUrl, createdAt plus the profile fields.
  • app/shared/auth-roles.ts with ROLES, 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.tsx is ignored and check reports config.wrong-extension. A malformed config: auth.config-invalid.
  • Roles in access must be in roles, or check reports access.unknown-role.
  • After 5 wrong passwords for one e-mail, or 30 from one IP, within 15 minutes, POST /api/auth/login answers 429 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 /users page 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, then remove_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/oauth or cookie sessions. The token is a JWT sent in the Authorization header.

Next steps

Last updated