Home > Enterprise >  How can I request an already logged in user to confirm password to enter a specific page in django?
How can I request an already logged in user to confirm password to enter a specific page in django?

Time:11-16

I'm working on a django application. The application has a dashboard. How can i make it so that everytime the user wants to access the dashboard, he/she has to confirm their identity, with the same password they use to log into the same application?

Thank you

CodePudding user response:

#Verify that the USERNAME and PASSWORD combination exist USING THE AUTHENTICATE METHOD,

Views.py:

from django.contrib.auth.models import User
from django.contrib.auth import authenticate

#IN YOUR CASE get the pwd using forms or something,

instance_password = request.post.get('the_pwd_field')

user = authenticate(request, username= request.user.username, password= instance_password)

if user is not None:
    # REDIRECT TO THE DASHBOARD
else:
    # FAIL CASE SCENARIO

CodePudding user response:

In views.py

from django.contrib.auth.hashers import check_password

form = YourFormForPassword(request.POST or None)
if form.is_valid():
    currentpasswordentered = form.cleaned_data.get("try_password")
    currentpassword = request.user.password

    authenticate_user = check_password(currentpasswordentered, currentpassword)

if authenticate_user:
    # REDIRECT TO THE DASHBOARD
else:
   #Redirect to other page or keep same login page
  • Related