Skip to content
This repository has been archived by the owner on Nov 21, 2024. It is now read-only.

Commit

Permalink
login done, but no email
Browse files Browse the repository at this point in the history
  • Loading branch information
DonHaul committed Sep 20, 2024
1 parent 61459ec commit cf7c06c
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 6 deletions.
2 changes: 2 additions & 0 deletions backoffice/.envs/local/.django
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
USE_DOCKER=yes
IPYTHONDIR=/app/.ipython



# Redis
# ------------------------------------------------------------------------------
REDIS_URL=redis://redis:6379/0
Expand Down
16 changes: 16 additions & 0 deletions backoffice/backoffice/users/adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,21 @@ def is_open_for_signup(
self, request: HttpRequest, sociallogin: SocialLogin
) -> bool:
return getattr(settings, "ACCOUNT_ALLOW_REGISTRATION", True)

from django.http import HttpResponseRedirect
def pre_social_login(self, request, sociallogin):
print("PRE SOCIAL SIGNUP")
print(str(request.user))
print(str(sociallogin.user))


# Extract email from the social login data
email = sociallogin.account.extra_data.get('email')

# If email is missing, redirect to the 'fill-email' page
if not email:
request.session['sociallogin'] = sociallogin.serialize() # Store social login data in session
return HttpResponseRedirect(reverse('fill_email'))

def populate_user(
self,
Expand All @@ -35,6 +50,7 @@ def populate_user(
See: https://django-allauth.readthedocs.io/en/latest/advanced.html?#creating-and-populating-user-instances
"""
print("adapter is adapting")
user = sociallogin.user
if name := data.get("name"):
user.name = name
Expand Down
20 changes: 18 additions & 2 deletions backoffice/backoffice/users/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,28 +36,44 @@ def me(self, request):
class OrcidLogin(SocialLoginView):
adapter_class = OrcidOAuth2Adapter
client_class = OAuth2Client
callback_url = "http://localhost:8000/api/v1/auth/google/callback/"
callback_url = "http://localhost:8000/api/oauth/authorized/orcid/"


class OrcidConnect(SocialConnectView):
adapter_class = OrcidOAuth2Adapter

from django.views.decorators.csrf import csrf_exempt
from rest_framework.permissions import AllowAny

from django.shortcuts import redirect
class OrcidLoginCallback(APIView):
permission_classes = [AllowAny] # Allow access to everyone

@csrf_exempt # Disable CSRF token check for testing purposes (optional)
def get(self, request, *args, **kwargs):
"""
If you are building a fullstack application (eq. with React app next to Django)
you can place this endpoint in your frontend application to receive
the JWT tokens there - and store them in the state
"""

params = request.GET.urlencode()

# Redirect to the target view with all parameters
return redirect(f"http://localhost:5000/callback?{params}")

code = request.GET.get("code")

if code is None:
return Response(status=status.HTTP_400_BAD_REQUEST)

# Remember to replace the localhost:8000 with the actual domain name before deployment
token_endpoint_url = urljoin("http://localhost:8000", reverse("orcid_login"))
token_endpoint_url = urljoin("http://localhost:8000", reverse("orcid_login2"))
response = requests.post(url=token_endpoint_url, data={"code": code})

data = response.json()
if data["user"]["email"]=="":

redirect

return Response(response.json(), status=status.HTTP_200_OK)
18 changes: 18 additions & 0 deletions backoffice/backoffice/users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,22 @@ def get_redirect_url(self):
return reverse("users:detail", kwargs={"pk": self.request.user.pk})


import requests

from django.http import HttpResponse, JsonResponse
from django.shortcuts import redirect
def orcid_callback(request):

#url = request.build_absolute_uri('/accounts/orcid/login/callback/'))
#params = request.GET.urlencode()
#return redirect(f"/accounts/orcid/login/callback/?{params}")
return HttpResponse("Send a POST request to see the body.", content_type="text/plain")


def success(request):

return JsonResponse(data={"wow":request.user.email})

user_redirect_view = UserRedirectView.as_view()


6 changes: 4 additions & 2 deletions backoffice/config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
"allauth",
"allauth.account",
"allauth.socialaccount",
"allauth.headless",
#"allauth.headless",
"allauth.socialaccount.providers.orcid",
"django_celery_beat",
"rest_framework",
Expand All @@ -115,6 +115,7 @@
"SESSION_LOGIN": True,
"USE_JWT": True,
"JWT_AUTH_COOKIE": "auth",
'JWT_AUTH_REFRESH_COOKIE': 'my-refresh-token',
"JWT_AUTH_HTTPONLY": False,
}

Expand All @@ -137,7 +138,7 @@
# https://docs.djangoproject.com/en/dev/ref/settings/#auth-user-model
AUTH_USER_MODEL = "users.User"
# https://docs.djangoproject.com/en/dev/ref/settings/#login-redirect-url
LOGIN_REDIRECT_URL = "/accounts/login/success"
LOGIN_REDIRECT_URL = "/accounts/login/success/"
# https://docs.djangoproject.com/en/dev/ref/settings/#login-url
LOGIN_URL = "account_login"

Expand Down Expand Up @@ -390,6 +391,7 @@
"client_id": env("ORCID_CLIENT_ID", default=""),
"secret": env("ORCID_CLIENT_SECRET", default=""),
},
"BASE_DOMAIN": "sandbox.orcid.org"
}
}
SOCIALACCOUNT_EMAIL_VERIFICATION = False
Expand Down
6 changes: 4 additions & 2 deletions backoffice/config/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from rest_framework_simplejwt.views import TokenObtainPairView, TokenRefreshView

from backoffice.users.api.views import OrcidConnect, OrcidLogin, OrcidLoginCallback
from backoffice.users.views import success

urlpatterns = [
path("", TemplateView.as_view(template_name="pages/home.html"), name="home"),
Expand All @@ -32,6 +33,7 @@
# API URLS
urlpatterns += [
# API base url
path("accounts/login/success/",success),
path("api/oauth/authorized/orcid/", OrcidLoginCallback.as_view(), name="orcid_callback"),
path("api/", include("config.search_router")),
path("api/", include("config.api_router")),
Expand All @@ -45,10 +47,10 @@
),
path("api/token/", TokenObtainPairView.as_view(), name="token_obtain_pair"),
path("api/token/refresh/", TokenRefreshView.as_view(), name="token_refresh"),
path("_allauth/", include("allauth.headless.urls")),
#path("_allauth/", include("allauth.headless.urls")),
path("dj-rest-auth/", include("dj_rest_auth.urls")),
path("dj-rest-auth/registration/", include("dj_rest_auth.registration.urls")),
path("dj-rest-auth/orcid/", OrcidLogin.as_view(), name="orcid_login"),
path("dj-rest-auth/orcid/", OrcidLogin.as_view(), name="orcid_login2"),
path("dj-rest-auth/orcid/connect/", OrcidConnect.as_view(), name="orcid_connect"),
]

Expand Down

0 comments on commit cf7c06c

Please sign in to comment.