So i succesfully signed up a user through Github, via django All-auth
now the problem is when github grants access to the User, it takes them to a page in my website that tells them to sign up using thier github username and email address.
the only problem is that the email address already exists in my database,
so when i click on sign up, it throws a duplicate integrity check error. now how to solve this and make the user automatically login when thier email address already exists in the database.
Error
# IntegrityError at /accounts/social/signup/
duplicate key value violates unique constraint "account_emailaddress_email_key" DETAIL: Key (email)=(abcd.net@gmail.com) already exists.
|Request Method:|POST|
| --- | --- |
|Request URL:|http://0.0.0.0:8000/accounts/social/signup/|
|Django Version:|4.1.7|
|Exception Type:|IntegrityError|
|Exception Value:|duplicate key value violates unique constraint "account_emailaddress_email_key" DETAIL: Key (email)=(abcd@gmail.com) already exists.|
|Exception Location:|/home/firaki/.local/lib/python3.10/site-packages/django/db/backends/utils.py, line 89, in _execute|
|Raised during:|allauth.socialaccount.views.SignupView|
so the error is raised in this views file
allauth.socialaccount.views.SignupView
i have located it and i changed the form_valid method, but it still raises the ERROR
Changed it from THIS
def form_valid(self, form):
self.request.session.pop("socialaccount_sociallogin", None)
form.save(self.request)
return helpers.complete_social_signup(self.request, self.sociallogin)
TO THIS BELOW
def form_valid(self, form):
email = form.cleaned_data['email']
print(email)
if User.objects.filter(email=email).exists():
# Email already exists, log in the user and redirect to home page
user = User.objects.get(email=email)
get_account_adapter().add_message(
self.request,
messages.INFO,
"You were automatically logged in as your email already exists.",
)
auth_login(self.request, user, account_settings.AUTHENTICATION_BACKENDS[0])
return HttpResponseRedirect(reverse('home'))
else:
# Email does not exist, create a new user
self.request.session.pop('socialaccount_sociallogin', None)
form.save(self.request)
return helpers.complete_social_signup(self.request, self.sociallogin)
Thanks