Getting Connected: Authentication Flow
Let’s follow a user named Alex as they open the MentraOS mobile app and connect their smart glasses to the cloud. This journey shows how authentication works and how WebSocket connections are established.Step 1: Mobile App Login
When Alex opens the MentraOS app on their phone:-
User Authentication
- Alex logs in with their email/password
- The app authenticates with our auth service
- App receives a
coreToken(JWT) that identifies Alex
-
What’s in the coreToken?
Step 2: Glasses Connect via Phone
Once authenticated, the mobile app:- Establishes Bluetooth connection with Alex’s smart glasses
- Opens WebSocket to cloud at
wss://cloud.mentraos.com/glasses-ws - Sends the coreToken in the Authorization header:
Step 3: WebSocket Handshake
Now the magic happens in our cloud:1. Connection Arrives at WebSocketService
2. GlassesWebSocketService Takes Over
3. The First Message Exchange
Mobile app must send CONNECTION_INIT within 30 seconds:Step 4: Heartbeat Keeps Connection Alive
Every 10 seconds, the cloud sends a ping to check if the connection is alive:Step 5: Handling Reconnections
What happens when Alex’s phone loses connection?-
Grace Period (30 seconds)
- UserSession remains in memory
- Apps stay in “resurrecting” state
- No data is lost
-
Reconnection Within Grace Period
- Same coreToken is used
- UserSession is retrieved from SessionStorage
- WebSocket is updated with new connection
- Apps are notified of reconnection
- Everything continues seamlessly
-
Reconnection After Grace Period
- New UserSession is created
- Apps must be restarted
- Previous state is lost
Security Considerations
-
JWT Validation
- All tokens are verified for signature
- Expired tokens are rejected
- Invalid tokens result in CONNECTION_ERROR
-
User Isolation
- Each UserSession is keyed by email
- No cross-user data access
- Sessions are completely isolated
-
Connection Limits
- One active glasses connection per user
- New connections replace old ones
- Prevents connection flooding