Skip to main content

Logout from all devices কিভাবে implement করা হয়?

 সব ডিভাইস থেকে একসাথে লগআউট করার (Logout from all devices / Global Logout) ফিচারটি মূলত Session Management-এর ওপর ভিত্তি করে কাজ করে। ডেভেলপমেন্টে এটি প্রধানত ২টি উপায়ে ইমপ্লিমেন্ট করা হয়—আপনি সেশন ট্র্যাকিংয়ের জন্য Database/Redis (Stateful) ব্যবহার করছেন নাকি JWT (Stateless) ব্যবহার করছেন তার ওপর ভিত্তি করে।

নিচে সহজ ভাষায় এবং টেকনিক্যাল আর্কিটেকচারসহ দুটি পদ্ধতিই আলোচনা করা হলো:

১. Database বা Redis ব্যবহার করে (Stateful Session)

যদি আপনার অ্যাপ্লিকেশনে ট্র্যাডিশনাল সেশন আইডি (Session ID) বা টোকেন ডেটাবেজ/Redis-এ সেভ করা থাকে, তবে গ্লোবাল লগআউট করা সবচেয়ে সহজ এবং সিকিউর।

যেভাবে কাজ করে:

১. সেশন ট্র্যাকিং: ইউজার যখনই কোনো ডিভাইসে লগইন করে, ডেটাবেজ বা Redis-এ একটি নতুন রো বা কি (Key) তৈরি হয়। সেখানে user_id, session_id, device_info এবং expires_at সেভ থাকে। ২. গ্লোবাল লগআউট রিকোয়েস্ট: ইউজার যখন "Logout from all devices" বাটনে ক্লিক করে, তখন ব্যাকএন্ডে একটি রিকোয়েস্ট যায়। ৩. ডেটা ডিলিট/ইনভ্যালিডেট: ব্যাকএন্ড কুয়েরি চালিয়ে ওই নির্দিষ্ট user_id-এর যতগুলো অ্যাক্টিভ সেশন রেকর্ড আছে, সব ডিলিট করে দেয়।

DELETE FROM user_sessions WHERE user_id = :current_user_id;


৪. ফলাফল: এরপর অন্য যেকোনো ডিভাইস থেকে ইউজার যখনই কোনো রিকোয়েস্ট পাঠাবে, ব্যাকএন্ড ডেটাবেজে সেই সেশন আইডি আর খুঁজে পাবে না। ফলে অটোমেটিক্যালি সব ডিভাইস থেকে ইউজার লগআউট হয়ে যাবে।

💡 প্রো-টিপ: প্রোডাকশন লেভেলে এই কাজের জন্য Redis সেরা। কারণ Redis-এ user:userId:sessions নামে একটি Set বা Hash রেখে, গ্লোবাল লগআউটের সময় জাস্ট ওই Key-টি DEL করে দিলেই মুহূর্তের মধ্যে সব ডিভাইস আনঅথরাইজড হয়ে যায়।



২. JWT (Json Web Token) ব্যবহার করে (Stateless Session)

JWT সাধারণত Stateless বা সেলফ-কনটেইন্ড হয়। অর্থাৎ, একবার টোকেন জেনারেট হয়ে ক্লায়েন্টে চলে গেলে, সেটির এক্সপায়ারি টাইম শেষ না হওয়া পর্যন্ত ব্যাকএন্ড থেকে সরাসরি তা ডিলিট করা যায় না। এই ক্ষেত্রে গ্লোবাল লগআউট ইমপ্লিমেন্ট করার ২টি জনপ্রিয় উপায় আছে:

পদ্ধতি A: Token Blacklisting (Redis সহ)

১. ইউজার যখনই লগইন করে, তাকে একটি ইউনিক jti (JWT ID) সহ Access Token এবং Refresh Token দেওয়া হয়।

২. গ্লোবাল লগআউট রিকোয়েস্ট আসলে, ব্যাকএন্ড একটি jwt_invalidated_at টাইমস্ট্যাম্প বা session_version ইউজারের মেইন প্রোফাইল (ডেটাবেজে) আপডেট করে দেয় (যেমন: বর্তমান সময়)।

৩. অ্যাপ্লিকেশনের Middleware-এ চেক করা হয়—টোকেনটি ইস্যু করার সময় যদি ডেটাবেজের jwt_invalidated_at-এর চেয়ে পুরনো হয়, তবে টোকেনটি ভ্যালিড হলেও রিজেক্ট করে দেওয়া হয়।

৪. অথবা, রিফ্রেশ টোকেনগুলোকে Redis-এ একটি Blacklist বা Revocation List-এ যোগ করা হয়, যতক্ষণ না সেগুলোর অরিজিনাল মেয়াদ শেষ হচ্ছে।

পদ্ধতি B: JWT + Database Session (Hybrid Approach)

এটি সবচেয়ে বেশি ব্যবহৃত এবং নিরাপদ পদ্ধতি।

  • এখানে Access Token থাকে Stateless (স্বল্পমেয়াদী, যেমন ১৫ মিনিট)।

  • কিন্তু Refresh Token-টি ডেটাবেজ বা Redis-এ ট্র্যাক করা হয়।

  • ইউজার যখন গ্লোবাল লগআউট করবে, ব্যাকএন্ড থেকে ওই ইউজারের সমস্ত Refresh Token ডিলিট করে দেওয়া হয়।

  • এর ফলে অন্য ডিভাইসগুলোতে থাকা Access Token সর্বোচ্চ ১৫ মিনিট কাজ করবে, এবং মেয়াদ শেষ হওয়ার পর যখনই Refresh Token দিয়ে নতুন টোকেন নিতে যাবে, তখন রিজেক্টেড হয়ে লগআউট হয়ে যাবে।

একটি আদর্শ ডাটাবেজ মডেল (Database Schema Example)

গ্লোবাল লগআউট এবং ইউজারকে তার অ্যাক্টিভ ডিভাইসগুলোর লিস্ট দেখানোর জন্য নিচের মতো একটি UserSession টেবিল মেইনটেইন করা হয়:

Column NameTypeDescription
idUUID / INTPrimary Key
user_idINTForeign Key linked to User
refresh_token_hashSTRINGSHA-256 hash of refresh token
device_nameSTRINGe.g., "Chrome on Windows", "iPhone 15"
ip_addressSTRINGUser's last IP
created_atTIMESTAMPSession creation time
expires_atTIMESTAMPWhen this specific session expires

গ্লোবাল লগআউটের ব্যাকএন্ড লজিক (Pseudo-code)

Python
@app.post("/auth/logout-all-devices")
def logout_all_devices(current_user: User, db: Session):
    # ১. ডেটাবেজ বা Redis থেকে এই ইউজারের সব সেশন ডিলিট করুন
    db.query(UserSession).filter(UserSession.user_id == current_user.id).delete()
    
    # ২. যদি JWT ইউজ করেন, তবে ইউজারের সেশন ভার্সন বা ইনভ্যালিডেশন টাইম আপডেট করুন
    current_user.jwt_invalidated_at = datetime.utcnow()
    db.commit()
    
    return {"message": "Successfully logged out from all devices."}

সারসংক্ষেপ (Best Practice)

আপনি যদি নতুন কোনো প্রজেক্টে এটি আর্কিটেক্ট করতে চান, তবে JWT Refresh Token + Redis Session Tracking কম্বিনেশনটি ব্যবহার করা সবচেয়ে বুদ্ধিমানের কাজ হবে। এতে পারফরম্যান্সও বজায় থাকে (Stateless Access Token-এর কারণে) এবং সিকিউরিটির ওপর ফুল কন্ট্রোলও থাকে (Redis-এ Refresh Token ট্র্যাকিংয়ের কারণে)।

আপনার প্রজেক্টে বর্তমানে কোন টেকনোলজি স্ট্যাক (যেমন: Node.js, Python, Go) বা অথেনটিকেশন মেকানিজম ব্যবহার করছেন? জানালে সেই অনুযায়ী স্পেসিফিক কোড এক্সাম্পল দেওয়া যাবে। 


Comments

Popular posts from this blog

WSGI vs ASGI: What Every Django Developer Should Know !

  If you've been developing with Django, you've probably come across WSGI (Web Server Gateway Interface), the trusted friend of all traditional, synchronous web apps. But in this fast-moving, real-time world, you may have also heard about its dynamic, asynchronous cousin ASGI (Asynchronous Server Gateway Interface). WSGI (Web Server Gateway Interface): 1. The OG (original) Django interface, designed for synchronous HTTP requests. 2. Perfect for blogs, CMS, e-commerce, and standard web apps. 3. Uses servers like Gunicorn or uWSGI. 4. Limited to handling one request at a time. ASGI (Asynchronous Server Gateway Interface): 1. The modern, scalable interface designed for asynchronous web apps. 2. Ideal for handling WebSockets, HTTP/2, and real-time features like chat apps. 3. Built for high concurrency; uses Uvicorn, Daphne, or similar ASGI servers. 4. Allows you to leverage Python’s async and await for non-blocking code. When to Choose What: WSGI: Traditional apps where synchronou...

Django pk vs id

 Django pk VS id If you don’t specify primary_key=True for any fields in your model, Django will automatically add an IntegerField to hold the primary key, so you don’t need to set primary_key=True on any of your fields unless you want to override the default primary-key behavior. The primary key field is read-only. If you change the value of the primary key on an existing object and then save it, a new object will be created alongside the old one Example: class UserProfile ( models . Model ): name = models . CharField ( max_length = 500 ) email = models . EmailField ( primary_key = True ) def __str__ ( self ): return self . name suppose we have this model. In this model we have make email field as primary key. now django default primary key id field will be gone. It'll remove from database. we can not query as   UserProfile.objects.get(id=1) after make email as primary key this query will throw an error.  Now we have to use pk  Us...

How Django stores passwords

  Django Password Django provides a flexible password storage system and uses PBKDF2 by default. Django saves the password as below. <algorithm>$<iterations>$<salt>$<hash> example of a Hashed password stored in database: pbkdf2_sha256$390000$LCm33kvO7rbjbZhwJA90Sf$xfuGOzl/MJyUxqWNhsNdSThaQUvn1EjEfxZ48HA8HF4= Those are the components used for storing a User’s password,separated by the dollar-sign character and consist of:  1. The hashing algorithm 2. The number of algorithm iterations (work factor) 3. The random salt 4. The resulting password hash.  Most password hashes include a salt along with their password hash in order to protect against rainbow table attacks. Example of Making Hashed password: Here’s a simplified overview of how Django handles password storage: 1. Password Creation or Change : # When someone creates a new account or decides to change their password, Django takes their chosen password and performs a process called hashing. Has...