2021-09-07 20:11:44 +00:00
|
|
|
""" class views for login/register views """
|
|
|
|
from django.contrib.auth import authenticate, login, logout
|
|
|
|
from django.contrib.auth.decorators import login_required
|
|
|
|
from django.shortcuts import redirect
|
|
|
|
from django.template.response import TemplateResponse
|
|
|
|
from django.utils.decorators import method_decorator
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
from django.views import View
|
2021-09-07 20:21:40 +00:00
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
|
|
|
from django.views.decorators.debug import sensitive_variables, sensitive_post_parameters
|
2021-09-07 20:11:44 +00:00
|
|
|
|
|
|
|
from bookwyrm import forms, models
|
|
|
|
from bookwyrm.settings import DOMAIN
|
2021-10-06 20:01:29 +00:00
|
|
|
from bookwyrm.views.helpers import set_language
|
2021-09-07 20:11:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
# pylint: disable=no-self-use
|
|
|
|
@method_decorator(csrf_exempt, name="dispatch")
|
|
|
|
class Login(View):
|
|
|
|
"""authenticate an existing user"""
|
|
|
|
|
|
|
|
def get(self, request, confirmed=None):
|
|
|
|
"""login page"""
|
|
|
|
if request.user.is_authenticated:
|
|
|
|
return redirect("/")
|
|
|
|
# send user to the login page
|
|
|
|
data = {
|
|
|
|
"show_confirmed_email": confirmed,
|
|
|
|
"login_form": forms.LoginForm(),
|
|
|
|
"register_form": forms.RegisterForm(),
|
|
|
|
}
|
2021-10-16 17:00:31 +00:00
|
|
|
return TemplateResponse(request, "landing/login.html", data)
|
2021-09-07 20:11:44 +00:00
|
|
|
|
2021-09-07 20:21:40 +00:00
|
|
|
@sensitive_variables("password")
|
2021-09-07 22:03:15 +00:00
|
|
|
@method_decorator(sensitive_post_parameters("password"))
|
2021-09-07 20:11:44 +00:00
|
|
|
def post(self, request):
|
|
|
|
"""authentication action"""
|
|
|
|
if request.user.is_authenticated:
|
|
|
|
return redirect("/")
|
|
|
|
login_form = forms.LoginForm(request.POST)
|
|
|
|
|
2021-12-28 22:50:28 +00:00
|
|
|
localname = login_form.data.get("localname")
|
|
|
|
|
2021-09-07 20:11:44 +00:00
|
|
|
if "@" in localname: # looks like an email address to me
|
|
|
|
try:
|
|
|
|
username = models.User.objects.get(email=localname).username
|
|
|
|
except models.User.DoesNotExist: # maybe it's a full username?
|
|
|
|
username = localname
|
|
|
|
else:
|
2021-09-18 18:32:00 +00:00
|
|
|
username = f"{localname}@{DOMAIN}"
|
2021-12-28 22:50:28 +00:00
|
|
|
password = login_form.data.get("password")
|
2021-09-07 20:11:44 +00:00
|
|
|
|
|
|
|
# perform authentication
|
|
|
|
user = authenticate(request, username=username, password=password)
|
|
|
|
if user is not None:
|
|
|
|
# successful login
|
|
|
|
login(request, user)
|
2021-09-22 15:35:20 +00:00
|
|
|
user.update_active_date()
|
2021-09-07 20:11:44 +00:00
|
|
|
if request.POST.get("first_login"):
|
2021-10-06 20:01:29 +00:00
|
|
|
return set_language(user, redirect("get-started-profile"))
|
|
|
|
return set_language(user, redirect(request.GET.get("next", "/")))
|
2021-09-07 20:11:44 +00:00
|
|
|
|
|
|
|
# maybe the user is pending email confirmation
|
|
|
|
if models.User.objects.filter(
|
|
|
|
username=username, is_active=False, deactivation_reason="pending"
|
|
|
|
).exists():
|
|
|
|
return redirect("confirm-email")
|
|
|
|
|
|
|
|
# login errors
|
|
|
|
login_form.non_field_errors = _("Username or password are incorrect")
|
|
|
|
register_form = forms.RegisterForm()
|
|
|
|
data = {"login_form": login_form, "register_form": register_form}
|
2021-10-16 17:00:31 +00:00
|
|
|
return TemplateResponse(request, "landing/login.html", data)
|
2021-09-07 20:11:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
@method_decorator(login_required, name="dispatch")
|
|
|
|
class Logout(View):
|
|
|
|
"""log out"""
|
|
|
|
|
|
|
|
def get(self, request):
|
|
|
|
"""done with this place! outa here!"""
|
|
|
|
logout(request)
|
|
|
|
return redirect("/")
|