fix error messages when setting up 2FA

This commit is contained in:
Hugh Rundle 2022-09-18 17:10:51 +10:00
parent 6db4fb39ed
commit 5b244f06d6
2 changed files with 11 additions and 7 deletions

View file

@ -32,7 +32,7 @@
<div class="field">
<label class="label" for="id_otp">{% trans "Enter the code from your authenticator app:" %}</label>
{{ form.otp }}
{% include 'snippets/form_errors.html' with errors_list=form.current_password.errors id="desc_current_password" %}
{% include 'snippets/form_errors.html' with errors_list=form.otp.errors id="desc_otp" %}
</div>
<button class="button is-primary" type="submit">{% trans "Confirm" %}</button>
</form>
@ -46,7 +46,7 @@
<div class="field">
<label class="label" for="id_password">{% trans "Password:" %}</label>
{{ form.password }}
{% include 'snippets/form_errors.html' with errors_list=form.current_password.errors id="desc_current_password" %}
{% include 'snippets/form_errors.html' with errors_list=form.password.errors id="desc_password" %}
</div>
<button class="button is-primary" type="submit">{% trans "Set up 2FA" %}</button>
</form>

View file

@ -31,11 +31,10 @@ class Edit2FA(View):
def post(self, request):
"""check the user's password"""
form = forms.ConfirmPasswordForm(request.POST, instance=request.user)
# TODO: display an error
if not form.is_valid():
data = {"form": form}
return TemplateResponse(request, "preferences/2fa.html", data)
qr_form = forms.Confirm2FAForm(request.POST, instance=request.user)
qr_form = forms.Confirm2FAForm()
data = {
"password_confirmed": True,
"qrcode": self.create_qr_code(request.user),
@ -66,10 +65,15 @@ class Confirm2FA(View):
def post(self, request):
"""confirm the 2FA works before requiring it"""
form = forms.Confirm2FAForm(request.POST, instance=request.user)
# TODO: show an error here
if not form.is_valid():
data = {"form": form}
return redirect("prefs-2fa")
data = {
"password_confirmed": True,
"qrcode": Edit2FA.create_qr_code(self, request.user),
"form": form,
}
return TemplateResponse(request, "preferences/2fa.html", data)
# set the user's 2FA setting on
request.user.two_factor_auth = True
request.user.save(broadcast=False, update_fields=["two_factor_auth"])