Introduction
Session management is the backbone of any app that requires user authentication. It involves storing the user's credentials (like a token), persisting them across app restarts, automatically logging the user back in, and handling logout. GetX makes session management straightforward with its reactive state, dependency injection, and easy integration with GetStorage. This guide covers everything you need to build a robust session management system in your Flutter app.
- Defining Session State
Create a service (or controller) that holds the session state. Extend GetxService to keep it permanent. Use reactive variables for user data and authentication status.
- Registering the Session Service
Register the service in an initial binding with permanent: true so it lives for the entire app lifecycle.
- Auto‑Login on App Start
In the splash screen, check the session state. If the user is logged in, navigate to the home screen; otherwise, go to login.
- Login Flow
When the user logs in, call the API, get the token and user data, then store them using the session service.
- Logout
Clearing the session and navigating back to login. Use Get.offAllNamed to remove the home screen from the stack.
- Session Timeout
To automatically log out after a period of inactivity, you can use a timer that resets on user interaction. Combine with a worker that watches session state.
- Handling Token Expiration
If your API returns a 401 (Unauthorized), you can automatically clear the session and redirect to login. Use an interceptor in your API client.
- Reacting to Session Changes in UI
Use Obx to listen to session state. For example, show different drawer items based on login status, or display the user's name.
Best Practices
- Use
GetxServicefor session management – It stays alive for the whole app. - Persist only the essential data – Store token and minimal user info; fetch the rest from API.
- Use
GetStorage– Simple and fast for key‑value storage. - Secure sensitive data – For production, consider encrypting tokens with
flutter_secure_storage. - Clear session on 401 – Always handle token expiration gracefully.
- Provide a logout button – Give users a way to end their session.
- Test session persistence – Verify that after killing and reopening the app, the user stays logged in if token is valid.
Common Mistakes
- ❌ Storing password or other sensitive data – Use token instead. ✅ Store only the token and basic user info.
- ❌ Not handling token expiration – App may crash or show odd behaviour. ✅ Intercept 401 and redirect to login.
- ❌ Forgetting to call
loadSessioninonInit– The session may not be restored. ✅ Always load persisted data on service initialization. - ❌ Using
Get.putinside a widget for the service – Creates duplicate instances. ✅ Register in initial binding withpermanent: true.
FAQ
- Q: Should I store the password in session?
A: No, only store the authentication token. The password should never be persisted. - Q: How to handle multiple user sessions?
A: Most apps have one active session. If you need multi‑account, you can store a list of user profiles, but that’s more advanced. - Q: How do I keep the session alive when the token is about to expire?
A: Use a refresh token mechanism. When a 401 occurs, try to refresh the token silently. If it fails, redirect to login. - Q: Can I use
GetXwith other storage like SharedPreferences?
A: Yes, butGetStorageis simpler and works on web. You can easily replace it. - Q: How to implement 'remember me' with session?
A: By default, we persist the token. If 'remember me' is false, you can store the token only in memory (not inGetStorage) or set a short expiration.
Conclusion
GetX provides a clean, reactive way to manage user sessions. By combining a permanent SessionService with GetStorage for persistence, you can implement auto‑login, session timeout, and logout with minimal boilerplate. The patterns shown here form the foundation for secure and user‑friendly authentication in your Flutter app.