# TIAR Webapp Baseline and Auth v1 Design

## Goal

Turn the existing `tiar-webapp` Git repository into a runnable copy of the customer-facing `tiar-charging-app` UI, preserving its browser routes and UI/UX as the migration baseline, then replace backend integrations incrementally through v1 adapters beginning with Auth.

## Repository safety

- Keep the existing `tiar-webapp/.git` repository and current branch history.
- Preserve the existing `tiar-webapp/.gitignore`, `README.md`, `docs/`, and ignored `local-documents/` content.
- Never copy `.git`, `.next`, `node_modules`, `.env`, or `tsconfig.tsbuildinfo` from `tiar-charging-app`.
- Do not overwrite existing migration documentation.
- Treat unrelated existing changes as user-owned and preserve them.

## Baseline import

Copy these baseline application assets from `tiar-charging-app` into `tiar-webapp`:

- `src/`
- `public/`
- `package.json`
- `package-lock.json`
- `next.config.ts`
- `postcss.config.mjs`
- `tsconfig.json`
- `eslint.config.mjs`
- `components.json`
- `.prettierrc`
- `.prettierignore`
- `next-env.d.ts`

The target repository's `.gitignore` remains authoritative. Dependency installation is performed inside `tiar-webapp` from the copied lockfile; dependencies are not copied from the old repository.

## Migration architecture

Browser pages and components continue to use the old customer-facing route structure, including `/signin`, `/signup`, `/otp-verify`, `/profile`, `/stations`, `/sessions`, and `/wallet`. Backend route differences are isolated behind server-side Next.js route handlers and focused service modules.

The migration unit is one feature vertical slice:

1. Preserve the old feature UI and browser route.
2. Record the old behavior from FE and BE source.
3. Define the new v1 adapter contract.
4. Connect the old UI to the adapter.
5. Test successful, loading, validation, unauthorized, and backend-error states.
6. Mark the feature complete before moving to the next feature.

The feature order is Auth, Profile, Dashboard, Stations/Map, Charging Sessions, Wallet/Transactions, and remaining content/support features.

## Auth v1 slice

### Upstream configuration

The Auth adapter uses server-only environment variables with these defaults for local development:

- `TIAR_V1_BASE_URL=https://dev.api.tiar.ai`
- `TIAR_V1_TENANT_HOST=parking-plus.dev.tiar.ai`
- `TIAR_V1_APP_NAME=mobile_app`

The browser never calls the upstream backend directly and never owns tenant-routing headers. The Next.js server adapter adds `X-Forwarded-Host` and `X-App-Name` to upstream requests.

### Initial endpoints

| Existing browser-facing route | New upstream endpoint | Purpose |
|---|---|---|
| `POST /api/auth/check-email` | `POST /v1/auth/check-email` | Determine account and password/sign-up state. |
| `POST /api/auth/sign-up` | `POST /v1/auth/sign-up` | Create the new backend registration state. |
| `POST /api/auth/login-password` | `POST /v1/auth/login` | Authenticate with email/password. |
| `GET /api/auth/me` | `GET /v1/user` | Validate the authenticated session and load the current user. |
| `POST /api/auth/refresh` | `POST /v1/auth/refresh-token` | Rotate access/refresh tokens when supported by the new response contract. |
| `POST /api/auth/change-password` | `POST /v1/auth/change-password` | Change password after authenticated login. |

OTP, logout revocation, required password setup, and reset-password completion remain compatibility gaps until their v1 contracts are confirmed. Local logout may still clear cookies and cached user state while backend revocation is unavailable.

### Data and security behavior

- Normalize email by trimming and lowercasing before Auth requests.
- Keep access and refresh tokens in HttpOnly, SameSite=Lax cookies.
- Never include tokens in browser JSON responses, logs, or test snapshots.
- Return the sanitized user and old-UI-compatible success/error fields from Next.js route handlers.
- Preserve the old redirects: complete profile to `/`, incomplete profile to `/onboarding`, unauthenticated protected routes to `/signin`.
- Show upstream validation/error messages in the existing toast and error UI without exposing internal response data.

## Testing strategy

- Add unit tests for v1 URL construction, tenant/app headers, email normalization, response parsing, and error mapping before implementing the adapter.
- Add route-handler tests that inject a fake upstream fetch and assert cookies/tokens are handled server-side.
- Run existing lint/build checks after the baseline copy.
- Run Auth tests after each red-green-refactor cycle.
- Start the migrated app locally and smoke-test `/signin` and `/signup` in a browser.
- Test registration against the new backend with a non-production account. A `.invalid` email can prove request acceptance but cannot complete verification.
- Test login only after the new backend account has completed verification and password setup.

## Completion criteria for this slice

1. `tiar-webapp` installs and runs independently without copied dependencies or secrets.
2. The old Auth UI and browser routes render from `tiar-webapp` with matching styling.
3. Registration reaches `https://dev.api.tiar.ai/v1/auth/sign-up` through the server adapter.
4. Login reaches `https://dev.api.tiar.ai/v1/auth/login` through the server adapter and safely stores tokens when the account is usable.
5. Current-user loading works through `/v1/user` after login.
6. Auth adapter tests, lint, and production build pass.
7. Missing v1 Auth capabilities remain explicitly documented rather than silently removed.

## Out of scope

- Migrating non-Auth feature APIs in this implementation slice.
- Redesigning the old UI or changing browser route names.
- Copying build artifacts, dependencies, Git metadata, or environment secrets.
- Treating the current Postman collection as complete backend documentation.
