DEV Community

Amara
Amara

Posted on

Taking On A New (Backend) Challenge

As a developer in the early stages of my career, every problem is as interesting as it is surprising— that is, until it gets frustrating. I make it my goal to find the solution before reaching that point of frustration. Sometimes I fail, but the wonderful thing is, there is always a solution. Here's a problem I faced recently, and the simple solution that came my way after hours of frustration.

The Challenge

After learning the basics of python and making some small console apps, I tried my hand at making a web app with Django. I decided to make a simple blog where users could login and leave comments on posts.

The challenge was, when a user logged in to the application, it caused the admin user to be logged out. And when the admin logged back in on the admin interface, the app incorrectly showed the admin as the logged-in user for the session that should belong to the regular registered user. This implied that user sessions were not being isolated correctly.

The Solution

After a quick search online, I figured out that it was a problem with session management, or conflicts in cookie management. The first thing I did was retry logging in on incognito, in case there were any issues with cookies.

After the issue persisted, I asked on the Django forum if anyone had an idea on what the problem might be, and someone suggested I implement a custom middleware to ensure that the superuser/admin stayed logged out of the app in order to prevent session conflicts. It worked.

from django.contrib.auth import logout
from django.urls import resolve

class LogoutSuperUserMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        resolver_match = resolve(request.path_info)
        if request.user.is_authenticated and request.user.is_superuser:
            if resolver_match.app_name == 'admin':
                logout(request)
        response = self.get_response(request)
        return response
Enter fullscreen mode Exit fullscreen mode

This middleware ensures that superuser sessions are managed to prevent conflicts with regular user sessions by checking if the user is authenticated and a superuser, and logging the superuser out of the app. It maintains session integrity by isolating superuser sessions, preventing disruptions across different user roles.

HNG Internship

HNG Internship is a fast-paced bootcamp for learning digital skills. It's focused on advanced learners and those with some pre-knowledge, and it gets people into shape for job offers.

I learnt of HNG Internship a while ago and have looked forward to gaining practical hands-on experience for a while. It is the perfect opportunity to get my hands dirty, and work in a collaborative environment.

A solid reason I have for participating in this internship is the opportunity to grow and collaborate with my peers, as learning alone can be a lonely experience. Learning with like minded, driven people is a sure way to ensure and maintain a good perspective on progress.

Learn more about the Internship here:
HNG Internship

Learn about their rich talent pool here:
HNG Hire

Conclusion

I am excited for this opportunity to grow as a backend developer. I look forward to fixing more problems, collaborating with talents, and overcoming challenges.

Top comments (0)