How to Disable Re-authentication in Django's allauth MFA's TOTP - Stack Overflow

admin2025-05-01  0

I am trying to customize the reauthentication form of the mfa. I aslo customized the TOTP activate and deactivate form and it works, but now i am struggling to customize the reauthentication form with my defined route name in django.

and is there any way to disable this reauth of the allauth mfa - 2fa

settings/urls.py

path('mfa/reauthenticate/', views.CustomReauthenticateView.as_view(), name='mfa_reauthenticate'),

settings/views.py

class CustomReauthenticateView(BaseReauthenticateView):
    template_name = "settings/mfa/reauthenticate.html"  # Ensure the correct template is used

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['custom_message'] = 'This is a custom message for reauthentication.'
        return context

    def form_valid(self, form):
        response = super().form_valid(form)
        return response

but it always rendering to localhost:8000/accounts/reauthenticate/?next=... and not my defined url. I also tried another way by customizing the allauth.account.decoders - reauthentication_required but not work

Please someone help, that i want to redirect to my customize reauth form

I am trying to customize the reauthentication form of the mfa. I aslo customized the TOTP activate and deactivate form and it works, but now i am struggling to customize the reauthentication form with my defined route name in django.

and is there any way to disable this reauth of the allauth mfa - 2fa

settings/urls.py

path('mfa/reauthenticate/', views.CustomReauthenticateView.as_view(), name='mfa_reauthenticate'),

settings/views.py

class CustomReauthenticateView(BaseReauthenticateView):
    template_name = "settings/mfa/reauthenticate.html"  # Ensure the correct template is used

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['custom_message'] = 'This is a custom message for reauthentication.'
        return context

    def form_valid(self, form):
        response = super().form_valid(form)
        return response

but it always rendering to localhost:8000/accounts/reauthenticate/?next=... and not my defined url. I also tried another way by customizing the allauth.account.decoders - reauthentication_required but not work

Please someone help, that i want to redirect to my customize reauth form

Share Improve this question asked Jan 2 at 14:44 SamIsRightHereSamIsRightHere 12 bronze badges 2
  • your path is /mfa/... not /accounts/...? – willeM_ Van Onsem Commented Jan 2 at 14:47
  • yes, I change to /mfa/... to path('accounts/reauthenticate/', views.CustomReauthenticateView.as_view(), name='mfa_reauthenticate'), it worked, Thanks man – SamIsRightHere Commented Jan 2 at 17:02
Add a comment  | 

1 Answer 1

Reset to default 0

I don't know if it will still help you but I have a good solution.

If you are using the classic Django Login you need to update the record authentication session:

from allauth.account.internal.flows.login import record_authentication
from django.contrib.auth.views import LoginView


class CustomLoginView(LoginView):
    def form_valid(self, form):
        res = super().form_valid(form)
        record_authentication(self.request, method="password", email=form.cleaned_data["username"])
        return res

You also need to set the settings "REAUTHENTICATION_TIMEOUT"

This is use in the method "did_recently_authenticate" which is used in the decorator "reauthentication_required".

This decorator is for example used for activate or deactivate TOTP

转载请注明原文地址:http://anycun.com/QandA/1746113412a91854.html