# NearMe Auth Testing Playbook

## Overview
Auth is dual-mode:
1. **Email/password (custom JWT-like bearer)** — `POST /api/auth/register` and `POST /api/auth/login-email` return a session token that MUST be sent as `Authorization: Bearer <session_token>` (mobile) or is also written to an httpOnly cookie (`session_token`) for the web frontend.
2. **Emergent Google Auth** — user clicks Google login on the web, hits `https://auth.emergentagent.com/?redirect=<origin>/auth/callback`, is redirected back with `#session_id=...`. Web frontend calls `POST /api/auth/session` with `X-Session-ID` header, backend exchanges with Emergent, upserts user, sets cookie.

Backwards compat: the legacy admin single-password (X-Admin-Token: admin123) still works — the first user to sign up (or the built-in bootstrap flow) can be promoted to `owner` role.

## Roles
- `owner` — full admin (all endpoints)
- `editor` — can edit ad-config, categories, video rules
- `viewer` — read-only admin views
- `user` — mobile-app user (default role for signups)

## Endpoints under test
- POST `/api/auth/register` — email, password, name, phone?, city?
- POST `/api/auth/login-email` — email, password → { user, session_token }
- POST `/api/auth/session` — X-Session-ID header (Google callback) → sets cookie
- GET  `/api/auth/me` — reads session_token cookie OR Authorization: Bearer
- POST `/api/auth/logout` — deletes session, clears cookie
- GET  `/api/users` — admin only (owner/editor), list users
- PUT  `/api/users/{user_id}/role` — owner only, change role

## Backend curl smoke
```
API=https://mobile-hub-240.preview.emergentagent.com

# Register
curl -s -X POST $API/api/auth/register -H 'Content-Type: application/json' \
  -d '{"email":"a@b.com","password":"pass1234","name":"A","phone":"","city":""}'

# Login
TOK=$(curl -s -X POST $API/api/auth/login-email -H 'Content-Type: application/json' \
  -d '{"email":"a@b.com","password":"pass1234"}' | python3 -c "import sys,json;print(json.load(sys.stdin)['session_token'])")

# Me (bearer)
curl -s $API/api/auth/me -H "Authorization: Bearer $TOK"

# Legacy admin token still works
curl -s $API/api/favorites -H 'X-Admin-Token: admin123'
```

## Browser test (Emergent Google Auth)
Use the playbook's cookie-injection method with `session_token` name and domain of the preview host.

## Data model
Collection `users`:
```
{
  user_id: "user_<uuid12>",
  email: str (unique),
  name: str,
  phone: str | "",
  city: str | "",
  role: "owner"|"editor"|"viewer"|"user",
  password_hash: str | null,   # bcrypt, null if only Google auth
  google_linked: bool,
  picture: str | null,
  created_at: iso str
}
```

Collection `user_sessions`:
```
{
  session_token: str (unique),
  user_id: str,
  expires_at: iso str (UTC),
  created_at: iso str (UTC),
  method: "email" | "google"
}
```

## Test users (created via testing agent)
See `/app/memory/test_credentials.md` for current test accounts.
