The “My Devices” Feature: Managing Sessions Across Multiple Clients

Sami Salih İbrahimbaş
3 min readSep 26, 2023

--

article cover

Introduction

Learn how to manage different sessions across multiple devices and understand the intricacies of logging out of a device when a session ends. Go a step further and discover how you can list the user’s other devices through an app. This article will guide you through the process, emphasizing the importance of security and user management.

Step 1: Does the Server Recognize the Request’s Device?

In this step, we focus on the request’s cookie. Cookie security is paramount, and we operate under the assumption that we are in the strictest cookie mode.

Step 1 image

Tip: Employ this as middleware in modern frameworks.

If a deviceUUID exists in the request’s cookie, we verify its authenticity using the SDK provided by Google. If the UUID is valid, we proceed; if not, we assign a new UUID and continue.

Step 2: Is There an Active Session on the Device?

We search for an access token in the cookie or header of the request. If absent, we inform the device that the request is invalid.

Step 2 image

If a token is present and verified with JWT, we proceed to the third step with the claims of this token. If the token is invalid, the request is reported as such.

Step 3: Do This User and Device Have an Active Session with This Access Token?

This step is crucial. We create a key with deviceUUID and userUUID values, considering a user can have active sessions on multiple devices.

step 3 image

We then query Redis for any data corresponding to this key. If there’s no data, there’s no session, and the user is notified of an invalid request. If data exists, we compare the tokens from the request and Redis.

If the tokens match, the session is validated, and the transaction continues. If not, it’s likely due to a re-login attempt with a token from an old session, necessitating user notification and logout from all other devices.

Conclusion

Implementing the “My Devices” feature is not just about enhancing user experience but also about fortifying security. By meticulously managing sessions and ensuring the validity of requests, we can prevent unauthorized access and secure user data. The objective is to create a seamless yet secure environment where users can manage their sessions across different devices efficiently.

This approach is particularly beneficial in scenarios where tokens are compromised, allowing for immediate response and safeguarding user accounts. By notifying the user and logging out from all devices, we ensure the highest level of security, protecting both user data and system integrity.

Remember, the essence of this feature is to balance convenience and security, providing users with the flexibility to manage their devices while maintaining stringent security protocols. It’s about creating a harmonious ecosystem where user satisfaction meets top-notch security standards.

--

--

Sami Salih İbrahimbaş
Sami Salih İbrahimbaş

Written by Sami Salih İbrahimbaş

lifetime junior • software developer at monopayments

No responses yet