Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ SECRET_KEY=123
ALLOWED_HOSTS="*"
LOGIN_URL=/login/
LOGIN_REDIRECT_URL=/
SITE_ID=1

# Databases
DATABASE_URL=postgres://localhost:5432/django_db
Expand All @@ -41,3 +42,7 @@ SENTRY_DSN=
# Other apps
USE_DEBUG_TOOLBAR=on
USE_DJANGO_EXTENSIONS=on

# OAuth (Google)
GOOGLE_CLIENT_ID=
GOOGLE_CLIENT_SECRET=
54 changes: 54 additions & 0 deletions apps/users/migrations/0002_google_socialapp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from django.db import migrations
import os


def create_google_socialapp(apps, schema_editor):
Site = apps.get_model("sites", "Site")
SocialApp = apps.get_model("socialaccount", "SocialApp")

client_id = os.environ.get("GOOGLE_CLIENT_ID")
secret = os.environ.get("GOOGLE_CLIENT_SECRET")
if not client_id or not secret:
# Environment variables not set; skip creation
return

# Try to attach to the configured SITE_ID (default 1)
from django.conf import settings

try:
site = Site.objects.get(id=getattr(settings, "SITE_ID", 1))
except Site.DoesNotExist:
site = Site.objects.first()

if not site:
return

app, _ = SocialApp.objects.get_or_create(
provider="google", name="Google", defaults={"client_id": client_id, "secret": secret, "key": ""}
)

# Update secrets if they changed
app.client_id = client_id
app.secret = secret
app.key = ""
app.save()

app.sites.add(site)


def delete_google_socialapp(apps, schema_editor):
SocialApp = apps.get_model("socialaccount", "SocialApp")
SocialApp.objects.filter(provider="google").delete()


class Migration(migrations.Migration):

dependencies = [
("users", "0001_initial"),
("sites", "0001_initial"),
("socialaccount", "0001_initial"),
]

operations = [
migrations.RunPython(create_google_socialapp, delete_google_socialapp),
]
18 changes: 16 additions & 2 deletions conf/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,13 @@
{"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator"},
{"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator"},
]
AUTHENTICATION_BACKENDS = ("django.contrib.auth.backends.ModelBackend",)
AUTHENTICATION_BACKENDS = (
"django.contrib.auth.backends.ModelBackend",
"allauth.account.auth_backends.AuthenticationBackend",
)
LOGIN_URL = env("LOGIN_URL", default="/login/")
LOGIN_REDIRECT_URL = env("LOGIN_REDIRECT_URL", default="/")
SITE_ID = env.int("SITE_ID", default=1)

# -----------------------------------------------------------------------------
# Databases
Expand All @@ -68,8 +72,13 @@
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"django.contrib.sites",
# Third party
"webpack_loader",
"allauth",
"allauth.account",
"allauth.socialaccount",
"allauth.socialaccount.providers.google",
# Local
"conf.apps.CustomAdminConfig",
"apps.misc",
Expand Down Expand Up @@ -103,6 +112,11 @@
},
]

# allauth account settings
ACCOUNT_AUTHENTICATION_METHOD = "email"
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_EMAIL_VERIFICATION = "optional"

# -----------------------------------------------------------------------------
# Static & Media Files
# -----------------------------------------------------------------------------
Expand All @@ -128,7 +142,7 @@
"STATS_FILE": stats_file,
"POLL_INTERVAL": 0.1,
"TIMEOUT": None,
"IGNORE": [r".+\.hot-update.js", r".+\.map"],
"IGNORE": [r".+\\.hot-update.js", r".+\\.map"],
}
}

Expand Down
1 change: 1 addition & 0 deletions conf/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

urlpatterns = [
path("", include("apps.users.urls.auth")),
path("accounts/", include("allauth.urls")),
path("admin/", admin.site.urls),
]

Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ psycopg2-binary = "^2.8.4"
python = "^3.6"
redis = "^3.3.11"
sentry-sdk = "^0.14.1"
django-allauth = "^0.44.0"

[tool.poetry.dev-dependencies]
Werkzeug = "^0.16.0"
Expand Down
9 changes: 8 additions & 1 deletion templates/registration/login.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% extends 'registration/base.html' %} {% load i18n %} {% block page_title %}{%
{% extends 'registration/base.html' %} {% load i18n %} {% load socialaccount %} {% block page_title %}{%
trans 'Log in' %}{% endblock %} {% block content %}
<form method="post" action=".">
{% csrf_token %} {{ form.as_p }}
Expand All @@ -11,4 +11,11 @@
{% trans "Forgot password" %}?
<a href="{% url 'password_reset' %}">{% trans 'Reset it' %}</a>!
</p>

<hr />

<p>
{% trans "Or sign in with" %}:
<a href="{% provider_login_url 'google' process='login' %}">{% trans 'Google' %}</a>
</p>
{% endblock %}