Skip to content
Merged
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
40 changes: 40 additions & 0 deletions backend/apps/authentication/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Generated by Django 6.1 on 2026-08-23 16:39

import uuid

import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):
initial = True

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.CreateModel(
name="UserProfile",
fields=[
(
"id",
models.UUIDField(
default=uuid.uuid4,
editable=False,
primary_key=True,
serialize=False,
),
),
(
"user",
models.OneToOneField(
on_delete=django.db.models.deletion.CASCADE,
related_name="profile",
to=settings.AUTH_USER_MODEL,
),
),
],
),
]
3 changes: 3 additions & 0 deletions backend/apps/authentication/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@
urlpatterns = [
path("register/", views.RegisterationView.as_view(), name="register"),
path("login/", views.LoginView.as_view(), name="login"),
path("<str:provider>/", views.OAuthLoginView.as_view()),
path("<str:provider>/callback/", views.OAuthCallbackView.as_view()),
# path("google/",views.GoogleLogin.as_view(),name="google_login"),
]
55 changes: 55 additions & 0 deletions backend/apps/authentication/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
from urllib.parse import urlencode

import requests
from django.conf import settings
from django.contrib.auth.models import User
from django.shortcuts import redirect
from rest_framework import generics, status
from rest_framework.permissions import AllowAny
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework_simplejwt.tokens import RefreshToken

from .models import UserProfile
Expand Down Expand Up @@ -62,3 +69,51 @@ def create(self, request, *args, **kwags):
},
status=status.HTTP_200_OK,
)


class OAuthLoginView(APIView):
permission_classes = [AllowAny]

def get(self, request, provider):
if provider != "google":
return Response({"error": "Unsupported provider"}, status=400)
params = {
"client_id": settings.GOOGLE_CLIENT_ID,
"redirect_uri": settings.GOOGLE_REDIRECT_URI,
"response_type": "code",
"scope": "openid email profile",
"access_type": "offline",
}
google_url = "https://accounts.google.com/o/oauth2/v2/auth?" + urlencode(params)
return redirect(google_url)


class OAuthCallbackView(APIView):
permission_classes = [AllowAny]

def get(self, request, provider):
if provider != "google":
return Response({"error": "Unsupported provider"}, status=400)
code = request.GET.get("code")
if not code:
return Response({"error": "Authorization code missing"}, status=400)
token_response = requests.post(
"https://oauth2.googleapis.com/token",
data={
"code": code,
"client_id": settings.GOOGLE_CLIENT_ID,
"client_secret": settings.GOOGLE_CLIENT_SECRET,
"redirect_uri": settings.GOOGLE_REDIRECT_URI,
"grant_type": "authorization_code",
},
)
token_data = token_response.json()
access_token = token_data.get("access_token")
if not access_token:
return Response({"error": "Failed to get access_token"}, status=400)
user_response = requests.get(
"https://www.googleapis.com/oauth2/v2/userinfo",
headers={"Authorization": f"Bearer {access_token}"},
)
user_data = user_response.json()
return Response(user_data)
40 changes: 40 additions & 0 deletions backend/config/settings/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import os
from pathlib import Path

from dotenv import load_dotenv

load_dotenv()
GOOGLE_CLIENT_ID = os.getenv("GOOGLE_CLIENT_ID")
GOOGLE_CLIENT_SECRET = os.getenv("GOOGLE_CLIENT_SECRET")
GOOGLE_REDIRECT_URI = os.getenv("GOOGLE_REDIRECT_URI")
# BASE_DIR points to the root of your backend project (/app)
BASE_DIR = Path(__file__).resolve().parent.parent.parent

Expand All @@ -12,11 +18,20 @@
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"rest_framework.authtoken",
# Third-party apps
"rest_framework",
"rest_framework_simplejwt",
# Local apps
"apps.authentication",
# dj_auth_apps
"dj_rest_auth",
"dj_rest_auth.registration",
# allauth
"allauth",
"allauth.account",
"allauth.socialaccount",
"allauth.socialaccount.providers.google",
]

MIDDLEWARE = [
Expand All @@ -27,6 +42,7 @@
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
"allauth.account.middleware.AccountMiddleware",
]

ROOT_URLCONF = "config.urls"
Expand Down Expand Up @@ -90,3 +106,27 @@

# Default primary key field type
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
SITE_ID = 1
AUTHENTICATION_BACKENDS = [
"django.contrib.auth.backends.ModelBackend",
"allauth.account.auth_backends.AuthenticationBackend",
]
REST_FRAMEWORK = {
"DEFAULT_AUTHENTICATION_CLASSES": [
"rest_framework.authentication.TokenAuthentication",
],
}

SOCIALACCOUNT_PROVIDERS = {
"google": {
"SCOPE": [
"profile",
"email",
],
"AUTH_PARAMS": {
"access_type": "online",
},
},
}

LOGIN_REDIRECT_URL = "/"
1 change: 1 addition & 0 deletions backend/config/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
urlpatterns = [
path("admin/", admin.site.urls),
path("api/auth/", include("apps.authentication.urls")),
# path("accounts/", include("allauth.urls")),
# path("catalog/", include("apps.catalog.urls")),
# path("order/", include("apps.orders.urls")),
# path("profile/", include("apps.profiles.urls")),
Expand Down
7 changes: 3 additions & 4 deletions backend/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,15 @@ dependencies = [
"djangorestframework>=3.15.0",
"djangorestframework-simplejwt>=5.3.1",
"django-cors-headers>=4.4.0",

# Database
"psycopg[binary]>=3.2.0",

# Production WSGI Server
"gunicorn>=23.0.0",

# Utilities & OAuth Integrations
"python-dotenv>=1.0.1",
"requests>=2.32.0",
"dj-rest-auth>=7.2.0",
"django-allauth>=65.19.1",
]

[tool.uv]
Expand Down Expand Up @@ -51,4 +50,4 @@ select = [
[tool.pytest.ini_options]
DJANGO_SETTINGS_MODULE = "config.settings.local"
python_files = ["test_*.py", "*_test.py", "tests.py"]
addopts = "--strict-markers --no-migrations"
addopts = "--strict-markers --no-migrations"
27 changes: 27 additions & 0 deletions backend/uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading