mahieyin-rahmun / NextJsWithDRFExample

This repository contains the code for a two-part article series that deals with connecting a Django Rest Framework backend with a Next.js + Next-Auth client with Social Authentication. In this example, we use OAuth with Google, but this can be extended to any arbitrary number of Providers.

Home Page:https://mahieyin-rahmun.medium.com/how-to-configure-social-authentication-in-a-next-js-next-auth-django-rest-framework-application-cb4c82be137

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Reverse for 'socialaccount_signup' not found. 'socialaccount_signup' is not a valid view function or pattern name.

w3ichen opened this issue · comments

I'm getting this error where I can't sign up new users using Google Sign in. (existing users can sign in).

I found this solution online: https://stackoverflow.com/a/41241934, while it did make this particular error message go away, I was still not able to login with google.

I've added this to my urlpatterns according to the tutorial: path("api/auth/", include("dj_rest_auth.urls")),, but there was no mention of adding a socialaccount_signup path.

Any help would be greatly appreciated and thank you for this tutorial!

Happy holidays!

Traceback (most recent call last):                                                                                        
    File "/.local/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner response = get_response(
    request)                                                                                      
    File "/.local/lib/python3.8/site-packages/django/core/handlers/base.py", line 181, in _get_response          
	response = wrapped_callback(request, *callback_args, **callback_kwargs)                                               
    File "/.local/lib/python3.8/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view         
	return view_func(*args, **kwargs)                                                                                     
    File "/.local/lib/python3.8/site-packages/django/views/generic/base.py", line 70, in view                    
	return self.dispatch(request, *args, **kwargs)                                                                        
    File "/.local/lib/python3.8/site-packages/django/utils/decorators.py", line 43, in _wrapper                  
	return bound_method(*args, **kwargs)                                                                                  
    File "/.local/lib/python3.8/site-packages/django/views/decorators/debug.py", line 89, in sensitive_post_parameters_wrapper                                                                                                           
	return view(request, *args, **kwargs)                                                                                 
    File "/.local/lib/python3.8/site-packages/dj_rest_auth/views.py", line 54, in dispatch                       
	return super().dispatch(*args, **kwargs)                                                                              
    File "/.local/lib/python3.8/site-packages/rest_framework/views.py", line 509, in dispatch                    
	response = self.handle_exception(exc)                                                                                 
    File "/.local/lib/python3.8/site-packages/rest_framework/views.py", line 469, in handle_exception            
	self.raise_uncaught_exception(exc)                                                                                    
    File "/.local/lib/python3.8/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception    
	raise exc                                                                                                             
    File "/.local/lib/python3.8/site-packages/rest_framework/views.py", line 506, in dispatch                    
	response = handler(request, *args, **kwargs)                                                                          
    File "/.local/lib/python3.8/site-packages/dj_rest_auth/views.py", line 125, in post                          
	self.serializer.is_valid(raise_exception=True)                                                                        
    File "/.local/lib/python3.8/site-packages/rest_framework/serializers.py", line 220, in is_valid              
	self._validated_data = self.run_validation(self.initial_data)                                                         
    File "/.local/lib/python3.8/site-packages/rest_framework/serializers.py", line 422, in run_validation        
	value = self.validate(value)                                                                                          
    File "/.local/lib/python3.8/site-packages/dj_rest_auth/registration/serializers.py", line 151, in validate                                                                                                                           
	complete_social_login(request, login)                                                                                 
    File "/.local/lib/python3.8/site-packages/allauth/socialaccount/helpers.py", line 151, in complete_social_login                                                                                                                      
	return _complete_social_login(request, sociallogin)                                                                   
    File "/.local/lib/python3.8/site-packages/allauth/socialaccount/helpers.py", line 172, in _complete_social_login                                                                                                                     
	ret = _process_signup(request, sociallogin)                                                                           
    File "/.local/lib/python3.8/site-packages/allauth/socialaccount/helpers.py", line 22, in _process_signup     
	url = reverse("socialaccount_signup")                                                                                 
    File "/.local/lib/python3.8/site-packages/django/urls/base.py", line 86, in reverse                          
	return resolver._reverse_with_prefix(view, prefix, *args, **kwargs)                                                   
    File "/.local/lib/python3.8/site-packages/django/urls/resolvers.py", line 694, in _reverse_with_prefix       
	raise NoReverseMatch(msg)                                                                                  
django.urls.exceptions.NoReverseMatch: Reverse for 'socialaccount_signup' not found. 'socialaccount_signup' is not a valid view function or pattern name. 

Hi, need a bit more information. Once the django.urls.exceptions.NoReverseMatch: Reverse for 'socialaccount_signup' not found error is resolved using the StackOverflow solution, what is the next error that you are getting (backend/frontend)? Or is it that using the StackOverflow solution still resulted in the stacktrace that you provided?

One other thing, your issue sounded familiar to something I had faced in a different project. Here is the corresponding issue that explains the situation (the solution is the same as the StackOverflow post but it's only partial). Can you confirm that the situation (not the solution) matches with yours?

Hi @mahieyin-rahmun,
Thank you for the prompt response and happy holidays! 🎄

I believe it is the same situation from the other project.
I found the issue to be that: if a email already exists, and you try to sign in with google, it will give the error.
I found that if a user who has not already sign up using the email sign up, they can sign in with google and not encounter this issue.

How do you suggest I fix this? Would it be an issue with next-auth on the front end or allauth on the backend?
I tried the solution from the other issue: url(r"^accounts", include("allauth.urls")),, but it still gave me Reverse for 'socialaccount_signup' not found.

A thing to note is that: url(r"^accounts", include("allauth.urls")), this line should be put in the urls.py of the django project and not a django app, make sure you have done this correctly.

I came to the full solution using Chrome Dev Tool's network tab. When this "account clash" happens, DRF actually sends back a response but we're too focused on the error in the DRF server terminal. Here's the code snippet from my other project, which should be put in [...nextauth].ts:

try {
  // the regular exchange of access tokens from Google server with DRF backend
} catch (error) {
  const response = error.response?.data;
  if (
    response?.non_field_errors &&
    Array.isArray(response?.non_field_errors) &&
    response?.non_field_errors.length === 1
  ) {
    return `/auth/login?error=${encodeURIComponent(
      response?.non_field_errors[0],
    )}`;
  }

  return false;
}

See if it works. You should not be able to sign in when the clash happens, but your frontend will be more descriptive about it.

Happy holidays.

Thank you so much! 🙏
It worked like a charm!