Skip to content

Commit

Permalink
User Accounts with Signup/Login (Authentication)
Browse files Browse the repository at this point in the history
  • Loading branch information
KalobTaulien committed Sep 9, 2019
1 parent 6109533 commit 6ab586e
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 1 deletion.
31 changes: 31 additions & 0 deletions mysite/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,19 @@
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sitemaps',
'django.contrib.sites',

'captcha',
'wagtailcaptcha',
'rest_framework',

'allauth',
'allauth.account',
'allauth.socialaccount',
]

SITE_ID = 1

MIDDLEWARE = [
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
Expand Down Expand Up @@ -102,6 +109,13 @@
},
]

AUTHENTICATION_BACKENDS = (
# Needed to login by username in Django admin, regardless of `allauth`
'django.contrib.auth.backends.ModelBackend',
# `allauth` specific authentication methods, such as login by e-mail
'allauth.account.auth_backends.AuthenticationBackend',
)

WSGI_APPLICATION = 'mysite.wsgi.application'


Expand Down Expand Up @@ -188,3 +202,20 @@
RECAPTCHA_PUBLIC_KEY = "6LcNrZcUAAAAAADyWEJTIOXKr6x-8POg3Iqp8rEM"
RECAPTCHA_PRIVATE_KEY = "6LcNrZcUAAAAAPISF06kecWBC4EJPXy2uo_penMC"
NOCAPTCHA = True


LOGIN_URL = '/login/'
LOGIN_REDIRECT_URL = '/'
ACCOUNT_AUTHENTICATION_METHOD = "username_email"
ACCOUNT_CONFIRM_EMAIL_ON_GET = True
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_EMAIL_VERIFICATION = "mandatory"
ACCOUNT_LOGIN_ON_EMAIL_CONFIRMATION = True
ACCOUNT_LOGOUT_ON_GET = True
ACCOUNT_LOGIN_ON_PASSWORD_RESET = True
ACCOUNT_LOGOUT_REDIRECT_URL = '/login/'
ACCOUNT_PRESERVE_USERNAME_CASING = False
ACCOUNT_SESSION_REMEMBER = True
ACCOUNT_SIGNUP_PASSWORD_ENTER_TWICE = False
ACCOUNT_USERNAME_BLACKLIST = ["kalob", "admin", "god"]
ACCOUNT_USERNAME_MIN_LENGTH = 2
57 changes: 57 additions & 0 deletions mysite/templates/account/login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{% extends "account/base.html" %}

{% load i18n %}
{% load account socialaccount %}

{% block title %}{% trans "Sign In" %}{% endblock %}

{% block content %}

<div class="container mt-5 mb-5">
<div class="row">
<div class="col-md-6 offset-md-3">


<h1>{% trans "Sign In" %}</h1>

{% get_providers as socialaccount_providers %}

{% if socialaccount_providers %}
<p>{% blocktrans with site.name as site_name %}Please sign in with one
of your existing third party accounts. Or, <a href="{{ signup_url }}">sign up</a>
for a {{ site_name }} account and sign in below:{% endblocktrans %}</p>

<div class="socialaccount_ballot">

<ul class="socialaccount_providers">
{% include "socialaccount/snippets/provider_list.html" with process="login" %}
</ul>

<div class="login-or">{% trans 'or' %}</div>

</div>

{% include "socialaccount/snippets/login_extra.html" %}

{% else %}
<p>{% blocktrans %}If you have not created an account yet, then please
<a href="{{ signup_url }}">sign up</a> first.{% endblocktrans %}</p>
{% endif %}

<form class="login" method="POST" action="{% url 'account_login' %}">
{% csrf_token %}
{{ form.as_p }}
{% if redirect_field_value %}
<input type="hidden" name="{{ redirect_field_name }}" value="{{ redirect_field_value }}" />
{% endif %}

<button class="btn btn-primary" type="submit">{% trans "Sign In" %}</button>

<a class="btn btn-link" href="{% url 'account_reset_password' %}">{% trans "Forgot Password?" %}</a>

</form>
</div>
</div>
</div>

{% endblock %}
12 changes: 12 additions & 0 deletions mysite/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@
{% endfor %}
{% endcache %}
</ul>
<ul class="navbar-nav">
<li class="nav-item">
{% if request.user.is_authenticated %}
Hi {{ request.user.username }},
<a href="{% url 'account_logout' %}" class="nav-link d-inline">Logout?</a>
{% else %}
Hi Guest!
<a href="{% url 'account_login' %}" class="nav-link d-inline">Login?</a>

{% endif %}
</li>
</ul>
<!--
<form class="form-inline my-2 my-lg-0">
<input class="form-control mr-sm-2" type="text" placeholder="Search">
Expand Down
4 changes: 3 additions & 1 deletion mysite/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
url(r'^api/v2/', api_router.urls),

url(r'^sitemap.xml$', sitemap),
url(r'', include('allauth.urls')),
# url(r'^accounts/', include('allauth.urls')),
# For anything not caught by a more specific rule above, hand over to
# Wagtail's page serving mechanism. This should be the last pattern in
# the list:
Expand All @@ -46,4 +48,4 @@
import debug_toolbar
urlpatterns = [
path('__debug__/', include(debug_toolbar.urls)),
] + urlpatterns
] + urlpatterns
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ black==19.3b0
flake8
isort
wagtail-django-recaptcha==1.0
django-allauth==0.40.0

1 comment on commit 6ab586e

@KalobTaulien
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.