Is it possible to use python-social-auth's EmailAuth with drf-social-oauth2 for registration
I have a facebook authentication in my project, and I've set up some pipelines. So It would be nice for non-social email registration to also utilize these pipelines.
I tried adding EmailAuth to the authentication backends list, but I don't know what view to use now for registratioin.
So, is it possible (or reasonable) to use EmailAuth with drf-social-oauth2 for non-social registration, and if so, how do I do it?
You can integrate EmailAuth with drf-social-oauth2
by adding it to AUTHENTICATION_BACKENDS
and using the same authentication pipelines.
Since drf-social-oauth2
lacks a native email registration view, create a custom API endpoint that registers users manually and authenticates them via the email backend. Then, expose this view in your urls
settings.py
AUTHENTICATION_BACKENDS = (
'social_core.backends.email.EmailAuth', # The email-based autentication
'social_core.backends.facebook.FacebookOAuth2', # Facebook login
'django.contrib.auth.backends.ModelBackend', # Default auth backend
)
custom registration view
# Other imports ..
from rest_framework.status import HTTP_400_BAD_REQUEST, HTTP_201_CREATED
from social_django.utils import load_backend, load_strategy
class EmailRegisterView(APIView):
def post(self, request):
email = request.data.get("email")
password = request.data.get("password")
if not email or not password:
return Response({"error": "Email & pass required"}, HTTP_400_BAD_REQUEST)
user, created = User.objects.get_or_create(email=email, defaults={"username": email})
if created:
user.set_password(password)
user.save()
strategy = load_strategy(request)
backend = load_backend(strategy, "email", redirect_uri=None)
user = backend.authenticate(request=request, email=email, password=password)
return Response({"detail": "User registered successfully"}, HTTP_201_CREATED)
urls.py
urlpatterns = [
# ...
path("auth/register/email/", EmailRegisterView.as_view(), name="email_register"),
]