Skip to content
This repository has been archived by the owner on Dec 26, 2023. It is now read-only.

Commit

Permalink
feat/add OAuth social (#5)
Browse files Browse the repository at this point in the history
* feat: add drf-social-oauth

* chore: update env example

---------

Co-authored-by: bayoog <obaydaba96@gmail.com>
  • Loading branch information
djobyte and BayooG authored Jul 16, 2023
1 parent a1d04ef commit c921ad7
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 15 deletions.
7 changes: 7 additions & 0 deletions backend/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,10 @@ SECRET_KEY=django-insecure-9n$j0#l-fb*-q#hqq+b&0e+c14e0b*7s@^x1yfj13$6thb4uir

GDAL_LIBRARY_PATH='/opt/homebrew/opt/gdal/lib/libgdal.dylib'
GEOS_LIBRARY_PATH = '/opt/homebrew/opt/geos/lib/libgeos_c.dylib'


FACEBOOK_KEY="test"
FACEBOOK_SECRET="test"

GOOGLE_KEY="test"
GOOGLE_SECRET="test"
58 changes: 50 additions & 8 deletions backend/backend/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@


# Take environment variables from .env file
environ.Env.read_env(os.path.join(BASE_DIR, '.env'))
environ.Env.read_env(os.path.join(BASE_DIR, ".env"))

# SECURITY WARNING: don't run with debug turned on in production!
# False if not in os.environ because of casting above
DEBUG = env('DEBUG')
DEBUG = env("DEBUG")


# GDAL_LIBRARY_PATH = '/opt/homebrew/opt/gdal/lib/libgdal.dylib'
Expand All @@ -37,7 +37,7 @@
# exception if SECRET_KEY not in os.environ
# SECURITY WARNING: keep the secret key used in production secret!

SECRET_KEY = env('SECRET_KEY')
SECRET_KEY = env("SECRET_KEY")

ALLOWED_HOSTS = ["*"]
# trusted origin for aws app runner
Expand All @@ -60,6 +60,9 @@
"rest_framework",
"rest_framework_gis",
"drf_spectacular",
"oauth2_provider",
"social_django",
"drf_social_oauth2",
]

MIDDLEWARE = [
Expand All @@ -85,6 +88,8 @@
"django.template.context_processors.request",
"django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages",
"social_django.context_processors.backends",
"social_django.context_processors.login_redirect",
],
},
},
Expand All @@ -93,14 +98,51 @@

REST_FRAMEWORK = {
# YOUR SETTINGS
'DEFAULT_SCHEMA_CLASS': 'drf_spectacular.openapi.AutoSchema',
"DEFAULT_SCHEMA_CLASS": "drf_spectacular.openapi.AutoSchema",
"DEFAULT_AUTHENTICATION_CLASSES": (
"oauth2_provider.contrib.rest_framework.OAuth2Authentication", # django-oauth-toolkit >= 1.0.0
"drf_social_oauth2.authentication.SocialAuthentication",
),
}

AUTHENTICATION_BACKENDS = (
# Facebook OAuth2
"social_core.backends.facebook.FacebookAppOAuth2",
"social_core.backends.facebook.FacebookOAuth2",
# Google OAuth2
"social_core.backends.google.GoogleOAuth2",
# drf_social_oauth2
"drf_social_oauth2.backends.DjangoOAuth2",
# Django
"django.contrib.auth.backends.ModelBackend",
)

# Facebook configuration
SOCIAL_AUTH_FACEBOOK_KEY = os.environ["FACEBOOK_KEY"]
SOCIAL_AUTH_FACEBOOK_SECRET = os.environ["FACEBOOK_SECRET"]


# Define SOCIAL_AUTH_FACEBOOK_SCOPE to get extra permissions from Facebook.
# Email is not sent by default, to get it, you must request the email permission.
SOCIAL_AUTH_FACEBOOK_SCOPE = ["email"]
SOCIAL_AUTH_FACEBOOK_PROFILE_EXTRA_PARAMS = {"fields": "id, name, email"}

# Google configuration
SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = os.environ["GOOGLE_KEY"]
SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = os.environ["GOOGLE_SECRET"]

# Define SOCIAL_AUTH_GOOGLE_OAUTH2_SCOPE to get extra permissions from Google.
SOCIAL_AUTH_GOOGLE_OAUTH2_SCOPE = [
"https://www.googleapis.com/auth/userinfo.email",
"https://www.googleapis.com/auth/userinfo.profile",
]


SPECTACULAR_SETTINGS = {
'TITLE': 'Ba7besh API',
'DESCRIPTION': 'Ba7besh backend API - for Ba7besh App use only',
'VERSION': '1.0.0',
'SERVE_INCLUDE_SCHEMA': False,
"TITLE": "Ba7besh API",
"DESCRIPTION": "Ba7besh backend API - for Ba7besh App use only",
"VERSION": "1.0.0",
"SERVE_INCLUDE_SCHEMA": False,
# OTHER SETTINGS
}

Expand Down
11 changes: 5 additions & 6 deletions backend/backend/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,15 @@
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path, include
from django.urls import path, include, re_path
from drf_spectacular.views import SpectacularAPIView, SpectacularSwaggerView
from rest_framework.schemas import get_schema_view

urlpatterns = [
path("admin/", admin.site.urls),
path("api/", include("ba7besh.urls")),
path(r"^api-auth/", include("rest_framework.urls", namespace="rest_framework")),
path('schema/', SpectacularAPIView.as_view(), name='schema'),
# path(r"^api-auth/", include("rest_framework.urls", namespace="rest_framework")),
re_path(r"^auth/", include("drf_social_oauth2.urls", namespace="drf")),
path("schema/", SpectacularAPIView.as_view(), name="schema"),
# Optional UI:
path('', SpectacularSwaggerView.as_view(url_name='schema'), name='swagger-ui'),

path("", SpectacularSwaggerView.as_view(url_name="schema"), name="swagger-ui"),
]
3 changes: 2 additions & 1 deletion backend/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ PyYAML==5.4.1
uritemplate==4.1.1
djangorestframework-gis==1.0
django-environ==0.10.0
drf-spectacular==0.26.3
drf-spectacular==0.26.3
drf_social_oauth2==2.1.3

0 comments on commit c921ad7

Please sign in to comment.