# Guardix, a SaaS for security-guard companies

> I took over Guardix from a previous agency, got auth, secrets and staging working again, and launched production on AWS with the web app and the iOS and Android guard app.

Source: https://ebrahimsameh.com/projects/guardix/

## What Guardix is

[Guardix](https://guardix.com) is a multi-tenant SaaS for security-guard companies. A company uses it to schedule shifts across its post sites, and its guards clock in from their phones inside a geofence. The office can follow guards on a live map. Guards scan QR or NFC tags on site tours and file incident reports with photos and signatures. Dispatch, tasks, time cards, payroll, invoicing, hiring, chat, push notifications, an SOS alert and an AI assistant are all part of the same product.

It ships as three apps that share one backend:

- The web dashboard for admins and office staff, built with Next.js 15, React 19 and TanStack Query, and hosted on AWS Amplify.
- The guard app for iOS and Android, built with Expo and React Native and shipped through EAS. It handles clock-in, background location, NFC tag scans, reports and push notifications.
- The client portal, where a guard company's own customers see reports and activity for their sites. Every widget is gated by permission.

## My role

I took Guardix over from a previous agency. From early 2026 until June I was the main full-stack and DevOps engineer on it. I stabilized staging, moved the infrastructure onto accounts the client controls, worked through the client's findings reports, built features across all three apps, launched production and shipped the mobile app to the stores. I've since moved on from the project.

## Stack and architecture

The repo is a Bun and Turbo monorepo with the web, mobile and backend apps plus three shared TypeScript packages for permissions, API types and timezone helpers.

- Backend: Django 5 and DRF, with Celery (worker, beat and Flower) on Redis, WebSockets for chat and about 25 domain apps. Postgres enforces tenant isolation with row-level security: a custom database cursor sets the tenant ID before each query, migrations run as a privileged user and the app runs as one that RLS applies to.
- Auth: self-hosted Logto over OIDC, with Logto organizations as tenants. There are separate Logto apps for the web, the mobile app and Django's machine-to-machine access to the Management API. Django validates tokens against Logto's JWKS.
- AI: the assistant runs on Gemini through Vertex AI and is tenant-aware, so it can answer from that company's own schedules and notes.
- Production on AWS: a dedicated VPC, two app servers behind an Application Load Balancer with a rolling deploy that drains one availability zone at a time, Multi-AZ RDS Postgres for the app and another for Logto, ElastiCache Redis with TLS, CloudFront with WAF in front, S3 for uploads, ECR for images and Secrets Manager for runtime config. GitHub Actions deploys through an OIDC role, so CI holds no long-lived AWS keys.
- Staging: EC2 with Docker Compose and Traefik handling TLS through Let's Encrypt, with DNS in Terraform. Pull requests get preview backends on ECS Fargate, each with its own database, and a nightly job cleans up previews whose PRs have closed.
- Errors and logs: Sentry and CloudWatch.

## Timeline

| Date | What happened |
| --- | --- |
| 2 March 2026 | The auth server, still running on a machine the previous developers controlled, is down. Local dev and staging are both broken. |
| 2 to 4 March | Logto self-hosted on AWS, the old secrets server removed from every pipeline, staging stable again. That stabilization pass touched 329 files. |
| 8 March | Runtime secrets moved to AWS Secrets Manager. Admin accounts and credentials left by the previous developers rotated. |
| March and April | Feature work across the three apps, plus the client's findings reports, worked through item by item. |
| 20 April | Production goes live on AWS. |
| 24 April | First Android release published to Google Play. iOS builds go out through TestFlight. |
| 1 May | Mobile 1.1.5 clears a Google Play rejection over photo and video permissions. |

## The hard parts

### Taking over a project that was locked out

When I arrived, the auth server sat on infrastructure the previous developers controlled, and it was returning 503s. Their secrets server, Infisical, had every CI workflow, both Dockerfiles and the Amplify build wired to it, and it started refusing us with `403 org_internal`. Nothing could build or deploy.

I self-hosted Logto next to Django on the staging server behind Traefik and gave it a real deploy pipeline. It had never had one. The pipeline seeds the database, applies Logto's schema alterations (including the ones that turn on row-level security for its own tables), waits through up to 18 health-check retries because Logto can take over a minute to start, and serves the auth domains over HTTPS. HTTPS is mandatory there: the PKCE login flow uses `crypto.subtle`, and browsers only expose it on secure origins.

I took Infisical out of five GitHub Actions workflows, both Django Dockerfiles, the Amplify build and the preview-environment pipeline, which also took its CLI out of the production images. Secrets went to GitHub Secrets and Amplify variables first, then to AWS Secrets Manager a few days later. The longer version is in [Inheriting a codebase that's locked out](/blog/inheriting-a-locked-out-codebase/).

### A tenant data leak

The endpoint that listed tenant roles returned every role from every tenant to any logged-in user. On the web, role data was cached under a key with no organization in it, so switching organizations could show the previous one's data. I fixed it in layers: an explicit tenant filter in the query on top of RLS, organization IDs in the TanStack Query keys, queries that wait until an organization is selected, and a header on every web and mobile request telling the backend not to bypass tenant scoping.

### The Amplify size limit

The Next.js build kept failing on Amplify. It couldn't resolve the monorepo's shared packages, and the server bundle was over Amplify's 220 MB limit. I rewrote the build to install from the monorepo root and build the shared packages in order, then stripped about 80 MB of SWC and esbuild binaries, build cache and server source maps from the output.

### Launch week on AWS

Setting up production took about two days, and most of the snags were small mismatches between AWS pieces:

- RDS requires SSL, and Logto's Node Postgres client also needed the RDS CA bundle mounted in.
- Django redirected the load balancer's plain-HTTP health checks to HTTPS, so every target looked unhealthy. The health route is now exempt from the redirect, and the target group's private IPs are in `ALLOWED_HOSTS`.
- CloudFront's API origin failed TLS until its domain matched the certificate.
- Moving the root domain from the coming-soon distribution to the new one hit `CNAMEAlreadyExists`, so the aliases had to come off the old distribution before they could go on the new one.
- Logto's built-in machine-to-machine apps returned `invalid_client` on a fresh seed, so I created the app's Logto clients directly in Logto's database under its RLS role.

### Shipping the guard app

The Android package name differs from the iOS bundle ID because the one we wanted was already taken on Google Play. After launch, Play rejected a build for asking for broad photo and video access. The cause was `expo-media-library`. It was installed but never registered as a plugin, so it quietly added `READ_MEDIA_IMAGES` and `READ_MEDIA_VIDEO` to the manifest. Registering the plugin with narrow settings and blocking those permissions fixed it in 1.1.5, with no app code changes, since the photo picker covers one-time uploads.

### Bugs that cost trust

Some fixes were small in code and large for users:

- Every deploy re-added default permissions to roles, which silently undid any permission an admin had removed. Roles now remember removed defaults, and there's an explicit reset-to-defaults action.
- The SOS button's copy said emergency services had been contacted. It only alerts the company's admins in the app. The copy now says that and tells guards to call 911 themselves.
- Payroll grouped hours by UTC date, which dropped the last day's hours for tenants west of UTC. It now uses the tenant's timezone.
