Skip to main content

Preventing Multi-Tab Conflicts ("The Bouncer")

📌 What is this for? (Quick Summary for New Interns)​

In Simple Words:
Imagine an engineer in the control room clicks Launch on their desktop, while another engineer in the field clicks Emergency Stop on a phone using the same account at the exact same second. The pod wouldn't know which command to trust!

To prevent this disaster:

  • Only one browser tab can be actively controlling the pod at any given time.
  • If you open the dashboard in a second window or device, the older window is automatically logged out with the message: "Session Terminated: You have logged in on another device."
  • We call this rule "The Bouncer".

1. How It Works​

sequenceDiagram
autonumber
actor TabA as Browser Window 1 (Laptop)
actor TabB as Browser Window 2 (Phone)
participant Dynamo as Database (ActiveSessions)
participant Lambda as Backend Code

Note over TabA: Engineer opens Window 1 and logs in
TabA->>Lambda: Registers session ID "111"
Lambda->>Dynamo: Save "thirumalai" -> "111"

Note over TabB: Engineer opens Window 2 on phone
TabB->>Lambda: Registers session ID "222"
Lambda->>Dynamo: Overwrites "thirumalai" -> "222"

Note over TabA: Window 1 asks for speed next second
TabA->>Lambda: Fetch metrics (sending old ID "111")
Lambda->>Dynamo: Checks active ID -> Database has "222"
Lambda-->>TabA: 401 SESSION_SUPERSEDED!

Note over TabA: Window 1 logs out immediately and shows alert

2. What Happens in Your Code​

Whenever your dashboard sends any request (like FETCH_METRICS), the backend checks if your tab ID matches what is in DynamoDB.

If it does not match, the backend returns:

{
"error": "SESSION_SUPERSEDED",
"message": "Logged in from another device."
}

In usePodManager.jsx, our code catches this:

const handleEjection = (err) => {
if (err?.message === "SESSION_SUPERSEDED") {
alert("Session Terminated: You have logged in on another device.");
sessionStorage.clear();
signOut().catch(() => {});
window.location.reload();
}
};

This keeps vehicle operations safe, clear, and accident-free.