diff --git a/.env.example b/.env.example index 88deb2d..27945f3 100644 --- a/.env.example +++ b/.env.example @@ -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 @@ -41,3 +42,7 @@ SENTRY_DSN= # Other apps USE_DEBUG_TOOLBAR=on USE_DJANGO_EXTENSIONS=on + +# OAuth (Google) +GOOGLE_CLIENT_ID= +GOOGLE_CLIENT_SECRET= diff --git a/apps/users/migrations/0002_google_socialapp.py b/apps/users/migrations/0002_google_socialapp.py new file mode 100644 index 0000000..ea389de --- /dev/null +++ b/apps/users/migrations/0002_google_socialapp.py @@ -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), + ] \ No newline at end of file diff --git a/conf/settings.py b/conf/settings.py index c22d609..8518950 100644 --- a/conf/settings.py +++ b/conf/settings.py @@ -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 @@ -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", @@ -103,6 +112,11 @@ }, ] +# allauth account settings +ACCOUNT_AUTHENTICATION_METHOD = "email" +ACCOUNT_EMAIL_REQUIRED = True +ACCOUNT_EMAIL_VERIFICATION = "optional" + # ----------------------------------------------------------------------------- # Static & Media Files # ----------------------------------------------------------------------------- @@ -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"], } } diff --git a/conf/urls.py b/conf/urls.py index b2ddfbf..66e0483 100644 --- a/conf/urls.py +++ b/conf/urls.py @@ -6,6 +6,7 @@ urlpatterns = [ path("", include("apps.users.urls.auth")), + path("accounts/", include("allauth.urls")), path("admin/", admin.site.urls), ] diff --git a/pyproject.toml b/pyproject.toml index ccbc7ff..2ce2dd4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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" diff --git a/templates/registration/login.html b/templates/registration/login.html index 505107f..e33ed32 100644 --- a/templates/registration/login.html +++ b/templates/registration/login.html @@ -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 %}