diff --git a/fet2020/authentications/forms.py b/fet2020/authentications/forms.py
index 40a53f7c..2e87e7c4 100644
--- a/fet2020/authentications/forms.py
+++ b/fet2020/authentications/forms.py
@@ -1,6 +1,28 @@
-from django import forms
+from django.contrib.auth.forms import AuthenticationForm
+from django.contrib.auth.models import User
+
+from .authentications import authentication
-class LoginForm(forms.Form):
- username = forms.CharField()
- password = forms.CharField(label="Passwort", widget=forms.PasswordInput())
+class LoginForm(AuthenticationForm):
+ def clean(self):
+ username = self.cleaned_data.get("username")
+ password = self.cleaned_data.get("password")
+
+ if username is not None and password:
+ auth_user = authentication(username, password)
+
+ if auth_user:
+ try:
+ self.user_cache = User.objects.get(username=auth_user.lower())
+ except User.DoesNotExist:
+ self.user_cache = User.objects.create_user(auth_user.lower())
+ else:
+ raise self.get_invalid_login_error()
+
+ if self.user_cache is None:
+ raise self.get_invalid_login_error()
+ else:
+ self.confirm_login_allowed(self.user_cache)
+
+ return self.cleaned_data
diff --git a/fet2020/authentications/urls.py b/fet2020/authentications/urls.py
index f4c51d8b..723dd2d8 100644
--- a/fet2020/authentications/urls.py
+++ b/fet2020/authentications/urls.py
@@ -6,6 +6,6 @@ from . import views
app_name = apps.AuthenticationsConfig.name
urlpatterns = [
- path("login/", views.loginPage, name="login"),
+ path("login/", views.AuthLoginView.as_view(), name="login"),
path("logout/", views.logoutUser, name="logout"),
]
diff --git a/fet2020/authentications/views.py b/fet2020/authentications/views.py
index 31c7f5bf..719e1c1a 100644
--- a/fet2020/authentications/views.py
+++ b/fet2020/authentications/views.py
@@ -1,46 +1,17 @@
-from django.contrib import messages
-from django.contrib.auth import login, logout
-from django.contrib.auth.models import User
-from django.shortcuts import render, redirect
+from django.contrib.auth import logout
+from django.contrib.auth.views import LoginView
+from django.shortcuts import redirect
from documents.etherpadlib import del_ep_cookie
-from .authentications import authentication
-from .decorators import unauthenticated_user, authenticated_user
+
+from .decorators import authenticated_user
from .forms import LoginForm
-@unauthenticated_user
-def loginPage(request):
- if request.method == "POST":
- username = request.POST.get("username").lower()
- password = request.POST.get("password")
-
- auth_user = authentication(username, password)
-
- if auth_user:
- try:
- user = User.objects.get(username=auth_user.lower())
- except User.DoesNotExist:
- user = User.objects.create_user(auth_user.lower())
-
- login(request, user)
-
- try:
- return redirect(request.GET.get("next"))
- except:
- return redirect("home")
- else:
- messages.error(
- request,
- "Anmeldung nicht erfolgreich. Bitte überprüfe Benutzername und Passwort.",
- )
-
- form = LoginForm()
-
- context = {
- "form": form,
- }
- return render(request, "authentications/login.html", context)
+class AuthLoginView(LoginView):
+ authentication_form = LoginForm
+ redirect_authenticated_user = True
+ template_name = "authentications/login.html"
@authenticated_user
diff --git a/fet2020/fet2020/settings.py b/fet2020/fet2020/settings.py
index c59f244d..215e8e14 100644
--- a/fet2020/fet2020/settings.py
+++ b/fet2020/fet2020/settings.py
@@ -66,6 +66,7 @@ AUTHENTICATION_BACKENDS = [
"django.contrib.auth.backends.ModelBackend",
]
+LOGIN_REDIRECT_URL = 'home'
LOGIN_URL = "/auth/login"
diff --git a/fet2020/templates/authentications/login.html b/fet2020/templates/authentications/login.html
index 25e99264..1a13f436 100644
--- a/fet2020/templates/authentications/login.html
+++ b/fet2020/templates/authentications/login.html
@@ -7,9 +7,11 @@
Anmeldung für FET-Mitarbeiter