Open the settings of your Google or Instagram account and look for the devices section. There's a list of every place your account is logged in. Each row names the device, a rough location and when it was last used, plus a button to sign that device out of your account.
That feature is called device & session management. In School Portal, it is the "Devices" page in account settings. Picture a gym membership card checked against the system at every turnstile. If you lose the card, the front desk blocks it in the system, and the next tap is refused at once. Compare a paper ticket printed "valid until 17:00": it stays valid until then, whoever holds it. The gym card is like a session the server checks on every request: once it is revoked, the next request is refused. The paper ticket is like a JWT, which stays valid until its exp time.
One row per device
Siti is logged in in three places: her laptop, her phone and the school's lab computer (as in the Auth Best Practices lesson). School Portal's server keeps one row per login:
| id | Device | IP | Region | Last active |
|---|---|---|---|---|
sess_1_0 |
Laptop: Chrome 141, Windows 11 | 36.68.10.21 | Bristol | 07:20, this device |
sess_1_1 |
Phone: Chrome 141, Android 15 | 182.253.5.9 | Bristol | 07:12 |
sess_1_2 |
Lab computer: Firefox 143, Ubuntu | 36.68.200.4 | Bristol | yesterday 13:40 |
The columns stored in each row:
- The session or token id. In School Portal's own table, the database stores its hash (SHA-256), not the token itself. When a request comes in, the server hashes its token and looks up that hash in the table. So a leaked database doesn't leak tokens.
- user_id, so the Devices page shows only that user's sessions.
- The device, read from the
User-Agentheader, and the IP. The region comes from IP geolocation (a location guessed from the IP address): a rough city, not a home address. - last_active_at, updated on requests (once a minute is enough, so the database isn't written on every request).
- revoked_at:
nullmeans still active. When the device is signed out, its session is revoked, and this column gets the time of that revocation.
In Laravel with the database session driver (sessions are kept in a database table), the sessions table already has the id, user_id, ip_address, user_agent and last_activity columns. For API tokens, the personal_access_tokens table of Sanctum (Laravel's official package for API tokens) stores the token's hash and last_used_at.
There is one difference: the id column of Laravel's sessions table holds the session ID as it is (40 characters), not its hash. The session cookie carries that ID encrypted with APP_KEY (Laravel's encryption key). With the table alone, an attacker cannot yet make a cookie the server accepts. But if APP_KEY leaks too, that ID can be used. So guard APP_KEY as closely as the database.
The code: create, list, revoke
This is a simple version in JavaScript. The sessions array plays the table, and there are four actions: create a session, list sessions, revoke one session, and revoke all but one.
function createSessionManager() {
const sessions = []; // on a real server: the sessions table
return {
create(userId, deviceName, ip) {
const session = { id: "sess_" + userId + "_" + sessions.length, userId, deviceName, ip, lastActive: Date.now(), revoked: false };
sessions.push(session);
return session;
},
getSessions(userId) {
return sessions.filter((s) => s.userId === userId && s.revoked === false);
},
revoke(sessionId) {
const session = sessions.find((s) => s.id === sessionId);
if (!session) return false;
session.revoked = true;
return true;
},
revokeAllExcept(userId, keepSessionId) {
sessions.filter((s) => s.userId === userId && s.id !== keepSessionId).forEach((s) => { s.revoked = true; });
},
};
}
const sm = createSessionManager();
sm.create(1, "Chrome 141, Windows 11", "36.68.10.21"); // sess_1_0, laptop
sm.create(1, "Chrome 141, Android 15", "182.253.5.9"); // sess_1_1, phone
sm.create(1, "Firefox 143, Ubuntu", "36.68.200.4"); // sess_1_2, lab computer
sm.create(2, "Safari 26, iOS 26", "114.124.7.88"); // sess_2_3, Mr. Andi
console.log(sm.getSessions(1).length); // 3
console.log(sm.revoke("sess_1_2")); // true, the lab computer is signed out
console.log(sm.getSessions(1).length); // 2
sm.revokeAllExcept(1, "sess_1_0"); // after the password change
console.log(sm.getSessions(1).map((s) => s.deviceName)); // [ 'Chrome 141, Windows 11' ]
console.log(sm.getSessions(2).length); // 1, Mr. Andi's session untouched
- The
idis built from"sess_", the userId and the session's position insessions. On a real server the id is 128 random bits, as in Session-based Authentication. The position is used here so the results can be predicted. getSessionsfilters by two conditions: belongs to that user and not revoked.revokedoesn't delete the row, it only marks itrevoked. The row stays for history. If the id isn't found,revokereturnsfalse.revokeAllExceptrevokes all of that user's sessions exceptkeepSessionId, the session in use. It also checkss.userId === userId. Without that condition, Mr. Andi's session would be revoked too.
Why revoking takes effect at once
With sessions on the server, every request carries the session ID, and the server looks it up in the session store, as in the Session-based Authentication lesson. The moment sess_1_2 is marked revoked, the next request from the lab computer gets 401 {"message": "Unauthenticated."}, right then.
A stateless JWT (one checked without looking anything up in a store) is different, as in JSON Web Token (JWT). The server only checks the signature and exp, looking nothing up. Say at 07:20 Siti changes her password on her laptop and presses "Sign out other devices". The access token on her phone was made at 07:12 with a 15-minute life, so its exp is 07:27. Revoking the session at 07:20 stops that session's refresh token, but the access token it already holds stays valid. Siti's phone can still open the report until 07:27.
There are two ways to close that gap from 07:20 to 07:27:
- make access tokens very short, and revoke the refresh token (Token Refresh Flow);
- keep a list of revoked
jtis (the unique id of each token) in Redis, and check that list on every request.
The second way means the server looks something up on every request again, exactly like sessions.
A password change: sign out the other devices
After a password change, every other session must end, except the one in use. That way, if the old password had leaked, any device that logged in with it is signed out too. Laravel has two ways:
// The route uses the 'auth.session' middleware
Auth::logoutOtherDevices($request->input('password'));
// Or directly, with the database session driver:
DB::table('sessions')
->where('user_id', $request->user()->id)
->where('id', '!=', $request->session()->getId())
->delete();
The second way directly deletes all of that user's rows in the sessions table, except the row of the session in use.
Then write a token_revoke line in the audit log (the previous lesson, Audit Logs & the Auth Trail). Also email Siti: "Your password was changed, and two other devices were signed out."
💡 Suspicious logins
The Devices page is also the right place to flag odd logins. Two patterns that help most:
- Impossible travel: a login from Sydney at 10:00, then from New York at 10:05. That's about 16,000 km. Covering that in 5 minutes takes 16,000 ÷ (5 ÷ 60) = 192,000 km/h, while a plane flies about 900 km/h. So the two logins can't come from the same person.
- A risk score: each signal (a suspicious sign) gets a weight, and all the weights are added up. For example impossible travel +50, a browser and OS never used before +20, a known VPN IP +15, an unusual hour +10, an IP on a threat list +30. Thresholds (score limits) decide the action: 70 or more asks for MFA again, 40 or more allows but sends an email, and below that allows. Weights and thresholds can be tuned from log data without rewriting the rules. Start with simple rules like these, not a machine learning model.
⚠️ Signed out, yet still able to get in (or the opposite)
- Removing it from the list but not the store. The row disappears from the Devices page, but its session ID is still accepted. Revoke it in the session store, where the server checks.
revokeAllExceptwithout a user check. Every session other than the one Siti keeps is revoked, other users' sessions included.- Revoking the session in use too. Siti is thrown out right after changing her password. Leave her own session id out.
- Storing tokens as they are. Store their hash.
- Promising "signed out instantly" with a stateless JWT. A token already issued stays valid until
exp.
TL;DR: The Devices page shows all of a user's login sessions, one row per login, and can revoke sessions. Each row holds an id (in your own table, store its hash; Laravel's sessions table keeps it as it is), user_id, the device from the User-Agent, the IP and a rough region, last_active_at (last active), and revoked_at (when it was revoked). getSessions lists the user's sessions that are not revoked, and revoke marks one session as revoked. revokeAllExcept revokes all of that user's sessions except the one in use, and runs after a password change. A server-side session ends as soon as it is revoked, because the server looks it up on every request. A stateless JWT (checked without looking anything up on the server) stays valid until exp, unless the server keeps a list of revoked tokens. For odd logins: impossible travel (two logins too far apart for the time between them), and a risk score, with thresholds (score limits) that choose the action (MFA, email, allow).
⚡ One row per device
Meaning: The "Devices" page shows every active session of the user, which means every place the account is logged in. From there the user can revoke (cancel) one session, or every session on the other devices. Each login is one row: an id (in your own table, its hash is stored), user_id, the device from the User-Agent header, the IP, a rough region, last active, and revoked_at (when the session was revoked).
⚡ Revoke all but the one in use
Write:
revokeAllExcept(userId, keepSessionId) {
sessions.filter((s) => s.userId === userId && s.id !== keepSessionId).forEach((s) => { s.revoked = true; });
},
Meaning: sessions is the list of every session on the server. revokeAllExcept marks revoked on all sessions of userId, except keepSessionId, the session in use. Run it after a password change. The condition s.userId === userId is required: without it, other users' sessions, such as Mr. Andi's, get revoked as well. The session Siti is using is left out, so she isn't thrown out herself.
⚡ A session dies at once, a JWT doesn't
Guess first: at 07:20 Siti signs her phone out. The access token (a JWT) on that phone was made at 07:12 and lives 15 minutes. Until when can the phone still open the report?
Until 07:27, which is 07:12 plus 15 minutes. The server checks a stateless JWT only by its signature and exp, looking nothing up, so a token already issued stays valid until it expires. A server-side session is different: the next request gets 401 at once.
Meaning: There are two ways to close that 07:20 to 07:27 gap. The first: very short access tokens, plus a revoked refresh token. The second: a list of revoked jtis (the unique id of each token), kept in Redis and checked on every request.