- Docs
- Specs
- Auth
- Sessions and roles
Sessions and Roles¶
1. Purpose¶
Define session/token lifecycle expectations, role model, route-prefix authorization, and tenancy boundaries for authenticated API access.
Sources: Client-Service Contract §2, §5, §7, §8, Phone + OTP, Glossary §1, §3.
2. Session Lifecycle¶
Identity Slice 1 implements phone + OTP authentication with Sanctum bearer tokens.
POST /api/auth/otp/verifyissues a Sanctum bearer token after successful OTP verification.- The token name is
authunless verify supplies an optionalclientstring; that string becomes the token name. - Clients send
Authorization: Bearer <token>on every authenticated request. - Slice 1 does not set a token expiry and does not implement refresh.
GET /api/auth/mereturns{ user, company, locale };companyisnullwithout a membership and the Company shape (with Tier) with one.POST /api/auth/logoutreturns{ message: "Logged out." }and revokes only the current token. Other device/session tokens remain valid.- Missing or invalid tokens return
401with{ message }. - Channel authorization callbacks must re-check membership; a stale token must not retain access when those channels are implemented.
3. Roles and User Fields¶
| Role | Domain meaning | Seat impact | Primary surfaces |
|---|---|---|---|
manager |
Company employee who runs fleet operations. | Free (not a seat). | Fleet Dashboard, company-scoped API. |
driver |
Person who operates a vehicle on a trip. | Free (not a seat). | Driver App, driver-scoped API. |
passenger |
Approved Company employee who books and rides. | Approved Passenger membership counts as a seat. | Passenger App, passenger-scoped API. |
administrator |
Platform owner/operator. | N/A. | Platform Admin (Filament), platform-scoped API. |
Terminology is strict: use Driver (never "coordinator"), Passenger (never "consumer"), Manager, Company, Fleet Dashboard, Front Views, Platform Admin.
Slice 1 User fields are:
| Field | Rule |
|---|---|
id |
Primary key. |
phone |
Required and unique; surrounding whitespace is trimmed. |
name |
Required; first verification initializes it to the phone value. |
email |
Nullable and unique when present; no email/password authentication. |
role |
One of passenger, driver, manager, administrator; defaults to passenger. |
locale |
en or ar; defaults to en. |
company_id |
Nullable; derived from the User's single Membership from Slice 2. Never client-supplied. |
The first successful OTP verification creates a Passenger User. Existing Users retain their role and profile. Slice 1 auth endpoints do not impose role-specific restrictions; each protected domain endpoint must define and test its authorization in its own slice.
4. Route Prefixes and Role Access¶
All prefixes below are relative to /api.
| Prefix | Audience | Auth | Examples / intent |
|---|---|---|---|
/auth/otp/send, /auth/otp/verify |
Public | Public (rate-limited where specified) | OTP login |
/auth/me, /auth/logout |
Any authenticated User | Bearer token | Session bootstrap/revocation |
/auth/enroll-company |
Public enrollment | Public (future slice) | Company enrollment with gender |
/onboarding/* |
Public | Public (rate-limited) | POST /onboarding/companies (Front Views) |
/passenger/* |
Passenger | Bearer token plus slice-specific authorization | Trips, bookings, live, reviews |
/driver/* |
Driver | Bearer token plus slice-specific authorization | Trips, manifest, boarding, feedback, route-plan |
/company/* |
Manager | Bearer token plus slice-specific authorization | Enrollments, codes, trips, drivers, car-requests, reviews, live |
/platform/* |
Platform Admin | Bearer token plus administrator authorization |
Companies, status, tiers |
/trips/{id}/review |
Passenger | Bearer token plus slice-specific authorization | Post-trip review |
5. Company Scoping and Tenancy¶
company_idis never accepted from the client. It is derived from authenticated membership when Slice 2 introduces membership persistence.- Slice 2 adds
tiers,companies,memberships, andcompany_codes;users.company_idmirrors the User's single Membership. TenantContextresolves the Company from the authenticated User's Membership on/company/*routes;CompanyScopefiltersmembershipsandcompany_codes. Future company-owned models (nav_routes,vehicles,trips) must apply the same scope; bookings are scoped bytrip.company_id.- A User has one Company membership context (one Company per User).
- Cross-tenant access returns 404, not 403, to avoid leaking existence.
- A suspended Company returns 403 with a clear message on every authenticated Company route.
6. Realtime Authorization¶
| Channel | Allowed |
|---|---|
private-company.{companyId} |
Manager of that Company only. |
trip.{tripId} |
Passenger with a booking on the trip, the assigned Driver, or a Manager of the trip's Company. |
Channel callbacks must re-check membership. A stale token must not retain access.
7. Error Cases¶
| Case | Status | Notes |
|---|---|---|
| Missing/invalid token | 401 | { message }. |
| Invalid/expired OTP | 422 | { message, errors: { otp: [...] } }. |
| OTP attempt limit or OTP send throttle | 429 | { message }. |
| Authenticated but action not allowed | 403 | { message }. |
| Suspended Company on authenticated Company route | 403 | Clear suspension message. |
| Cross-tenant resource access | 404 | Do not leak existence. |
| Validation failure | 422 | Standard error shape with errors. |
All errors follow { message: string, errors?: Record<string, string[]> }.
8. Tests¶
- OTP verification issues a usable Sanctum token with the default or client-supplied token name.
- Unauthenticated current-user/logout requests return
401with the standard envelope. - Current User includes
company(nullwithout a membership, Company shape with Tier otherwise) andlocale. - Logout revokes the current token without revoking other tokens.
- New Users default to
passenger,en, nullableemail, andcompany_id. - Role, tenant-isolation, suspension, and realtime tests are added with the slices that implement those resources.
9. Future Questions¶
- Token expiry, refresh, and device-wide revocation policy.
- Whether a Platform Admin can impersonate or act within a Company context, and how that is audited.
- Exact per-endpoint role matrix for endpoints outside the primary prefix audience.
- How role changes or membership suspension affect already-issued tokens before natural expiry.