# Path: sandbox/sample/forms.py
# class RegistrationForm(forms.Form):
#     email = forms.EmailField()
#     password = forms.CharField(widget=forms.PasswordInput)
#     confirm_password = forms.CharField(widget=forms.PasswordInput)
# 
#     def __init__(self, *args, **kwargs):
#         super(RegistrationForm, self).__init__(*args, **kwargs)
#         for field in self.fields.values():
#             field.widget.attrs = {'class': 'form-control'}
# 
#     def clean_confirm_password(self):
#         password = self.cleaned_data.get("password")
#         confirm_password = self.cleaned_data.get("confirm_password")
#         if password and confirm_password and password != confirm_password:
#             raise forms.ValidationError("Passwords not matched")
#         return confirm_password
# 
# class LoginForm(forms.Form):
#     email = forms.EmailField()
#     password = forms.CharField(widget=forms.PasswordInput)
# 
#     def __init__(self, *args, **kwargs):
#         super(LoginForm, self).__init__(*args, **kwargs)
#         for field in self.fields.values():
#             field.widget.attrs = {'class': 'form-control'}
# 
#     def clean(self):
#         email = self.cleaned_data.get('email')
#         password = self.cleaned_data.get('password')
#         self.user = authenticate(username=email, password=password)
#         if not self.user:
#             raise forms.ValidationError("Invalid Credentials")
#         return self.cleaned_data
# 
# Path: django_mfa/models.py
# def is_u2f_enabled(user):
#     """
#     Determine if a user has U2F enabled
#     """
#     return user.u2f_keys.all().exists()


from django.shortcuts import render
from django.contrib.auth import login, logout
from django.http.response import HttpResponseRedirect, JsonResponse
from .forms import RegistrationForm, LoginForm
from django.contrib.auth.models import User
from django_mfa.models import is_u2f_enabled
from django.conf import settings


def index(request):
    if request.user and request.user.is_authenticated:
        return HttpResponseRedirect(settings.LOGIN_REDIRECT_URL)
    if request.method == 'POST':
        form = LoginForm(request.POST, request.FILES)
        if form.is_valid():
            user = form.user
            if is_u2f_enabled(user):
                request.session['u2f_pre_verify_user_pk'] = user.pk
                request.session['u2f_pre_verify_user_backend'] = user.backend
            login(request, form.user)
            return JsonResponse({"error": False})
        else:
            return JsonResponse({"error": True, "errors": form.errors})
    context = {

# Path: sandbox/sample/forms.py
# class RegistrationForm(forms.Form):
#     email = forms.EmailField()
#     password = forms.CharField(widget=forms.PasswordInput)
#     confirm_password = forms.CharField(widget=forms.PasswordInput)
# 
#     def __init__(self, *args, **kwargs):
#         super(RegistrationForm, self).__init__(*args, **kwargs)
#         for field in self.fields.values():
#             field.widget.attrs = {'class': 'form-control'}
# 
#     def clean_confirm_password(self):
#         password = self.cleaned_data.get("password")
#         confirm_password = self.cleaned_data.get("confirm_password")
#         if password and confirm_password and password != confirm_password:
#             raise forms.ValidationError("Passwords not matched")
#         return confirm_password
# 
# class LoginForm(forms.Form):
#     email = forms.EmailField()
#     password = forms.CharField(widget=forms.PasswordInput)
# 
#     def __init__(self, *args, **kwargs):
#         super(LoginForm, self).__init__(*args, **kwargs)
#         for field in self.fields.values():
#             field.widget.attrs = {'class': 'form-control'}
# 
#     def clean(self):
#         email = self.cleaned_data.get('email')
#         password = self.cleaned_data.get('password')
#         self.user = authenticate(username=email, password=password)
#         if not self.user:
#             raise forms.ValidationError("Invalid Credentials")
#         return self.cleaned_data
# 
# Path: django_mfa/models.py
# def is_u2f_enabled(user):
#     """
#     Determine if a user has U2F enabled
#     """
#     return user.u2f_keys.all().exists()


# Path: sandbox/sample/views.py
from django.shortcuts import render
from django.contrib.auth import login, logout
from django.http.response import HttpResponseRedirect, JsonResponse
from .forms import RegistrationForm, LoginForm
from django.contrib.auth.models import User
from django_mfa.models import is_u2f_enabled
from django.conf import settings



def index(request):
    if request.user and request.user.is_authenticated:
        return HttpResponseRedirect(settings.LOGIN_REDIRECT_URL)
    if request.method == 'POST':
        form = LoginForm(request.POST, request.FILES)
        if form.is_valid():
            user = form.user
            if is_u2f_enabled(user):
                request.session['u2f_pre_verify_user_pk'] = user.pk
                request.session['u2f_pre_verify_user_backend'] = user.backend
            login(request, form.user)
            return JsonResponse({"error": False})
        else:
            return JsonResponse({"error": True, "errors": form.errors})
    context = {

        "registration_form": RegistrationForm,