From 7ac3d82ac4222a6a9e618bac8946b493908313a0 Mon Sep 17 00:00:00 2001 From: Michael Hall Date: Sat, 28 Apr 2018 16:33:37 -0400 Subject: [PATCH] Refactor login page to allow local account signup and login --- get_together/static/css/get_together.css | 9 +++ .../new_user/setup_1_confirm_profile.html | 20 ++++++ .../templates/get_together/users/login.html | 67 +++++++++++++++---- get_together/views/user.py | 33 ++++++++- 4 files changed, 114 insertions(+), 15 deletions(-) diff --git a/get_together/static/css/get_together.css b/get_together/static/css/get_together.css index 9f96ff2..e032615 100644 --- a/get_together/static/css/get_together.css +++ b/get_together/static/css/get_together.css @@ -77,3 +77,12 @@ form { color: #FFFFFF; } +ul.errorlist li { + padding: 0.75rem 1.25rem; + margin-bottom: 1rem; + border: 1px solid transparent; + border-radius: 0.25rem; + background-color: #f2dede; + border-color: #ebcccc; + color: #a94442; +} diff --git a/get_together/templates/get_together/new_user/setup_1_confirm_profile.html b/get_together/templates/get_together/new_user/setup_1_confirm_profile.html index eb0abf7..dd4fe3d 100644 --- a/get_together/templates/get_together/new_user/setup_1_confirm_profile.html +++ b/get_together/templates/get_together/new_user/setup_1_confirm_profile.html @@ -1,4 +1,5 @@ {% extends "get_together/base.html" %} +{% load static %} {% block content %}
@@ -20,8 +21,27 @@ {% endblock %} {% block javascript %} + diff --git a/get_together/templates/get_together/users/login.html b/get_together/templates/get_together/users/login.html index 6372125..8482eea 100644 --- a/get_together/templates/get_together/users/login.html +++ b/get_together/templates/get_together/users/login.html @@ -1,19 +1,58 @@ {% extends "get_together/base.html" %} {% block content %} -
- -

Signup or Login to get started

- -{% if settings.SOCIAL_AUTH_GOOGLE_OAUTH2_KEY %} Google{% endif %} -{% if settings.SOCIAL_AUTH_FACEBOOK_KEY %} Faceboook{% endif %} -{% if settings.SOCIAL_AUTH_TWITTER_KEY %} Twitter{% endif %} -{% if settings.SOCIAL_AUTH_GITHUB_KEY %} GitHub{% endif %} -{% if settings.DEBUG %}Local{% endif %} -
-
+
+
+
+
+
+

Login using one of these providers:

+ {% if settings.SOCIAL_AUTH_GOOGLE_OAUTH2_KEY %} Google{% endif %} + {% if settings.SOCIAL_AUTH_FACEBOOK_KEY %} Faceboook{% endif %} + {% if settings.SOCIAL_AUTH_TWITTER_KEY %} Twitter{% endif %} + {% if settings.SOCIAL_AUTH_GITHUB_KEY %} GitHub{% endif %} +
Note: The only information GetTogether recieves from these providers is your username and (optionally) your email address. GetTogether will not have access to read any other information from these accounts, not even your password, and will not have access to post information to them.
+
+
+
+
+
+
+
+
+
+

Or, use a local GetTogether account:

+ +
+
+
+ {% csrf_token %} + + {{ login_form }} +
+ +
+
+
+
+ {% csrf_token %} + + {{ signup_form }} +
+ +
+
+
+
+
+
+
{% endblock %} diff --git a/get_together/views/user.py b/get_together/views/user.py index 584cd6c..e7866da 100644 --- a/get_together/views/user.py +++ b/get_together/views/user.py @@ -2,6 +2,8 @@ from django.utils.translation import ugettext_lazy as _ from django.contrib import messages from django.contrib.auth import logout as logout_user +from django.contrib.auth import login as login_user, authenticate +from django.contrib.auth.forms import UserCreationForm, AuthenticationForm from django.shortcuts import render, redirect, get_object_or_404 from django.http import HttpResponse, JsonResponse from django.core.exceptions import ObjectDoesNotExist @@ -26,8 +28,37 @@ def login(request): if request.user.is_authenticated: messages.add_message(request, messages.INFO, message=_('You are already logged in.')) return redirect('home') - return render(request, 'get_together/users/login.html') + context = { + 'login_form': AuthenticationForm(), + 'signup_form': UserCreationForm(), + } + if request.method == 'POST': + if request.POST.get('action') == 'login': + login_form = AuthenticationForm(data=request.POST) + if login_form.is_valid(): + username = login_form.cleaned_data.get('username') + raw_password = login_form.cleaned_data.get('password') + user = authenticate(username=username, password=raw_password) + login_user(request, user, backend=user.backend) + return redirect('home') + else: + context['login_form'] = login_form + context['action'] = 'login' + elif request.POST.get('action') == 'signup': + signup_form = UserCreationForm(data=request.POST) + if signup_form.is_valid(): + signup_form.save() + username = signup_form.cleaned_data.get('username') + raw_password = signup_form.cleaned_data.get('password1') + user = authenticate(username=username, password=raw_password) + login_user(request, user, backend=user.backend) + return redirect('home') + else: + context['signup_form'] = signup_form + context['action'] = 'signup' + + return render(request, 'get_together/users/login.html', context) def show_profile(request, user_id): user = get_object_or_404(UserProfile, id=user_id)