I get the admin id but when im logged in as a user i still get admin id when i try to fetch the user id

21 views Asked by At

i created a function in my views.py file which lets you see different dashboards according to the role of the email which is logging in now when im trying to fetch the id of user im getting the id of the user which was loggod on the server before what chnages should i be doing for this.I tried clearing the session during logout but it's still not working and the code is fetching me the id of previous logged in admin.

 def determine_role(request):
    user = request.user  # Get the authenticated user object
    role = user.role  # Access the role attribute
    # Now you can use the role to perform further actions
    if role == 'admin':
        # If the user is an admin, do something
        return render(request, 'loginapp/admin_dashboard.html')
    elif role == 'user':
        # If the user is a regular user, do something else
        return render(request, 'loginapp/user_dashboard.html')   
1

There are 1 answers

0
Luis Miguel Sotamba On

I don't know if you are cleaning your session manually. If so, you should use the logout function that Django provides you to log a user out. You can find this function in django.contrib.auth.logout(). According to Django's documentation:

When you call logout(), the session data for the current request is completely cleaned out. All existing data is removed. This is to prevent another person from using the same web browser to log in and have access to the previous user’s session data. If you want to put anything into the session that will be available to the user immediately after logging out, do that after calling django.contrib.auth.logout().