首页 > 解决方案 > Django 3rd 方登录重复电子邮件

问题描述

我设置了我的 django 应用程序,以便能够使用 google 和 facebook 3rd 方应用程序登录。但是,每当我已经有一个使用已经存在的 gmail 帐户注册的帐户时,我会得到一个奇怪的注册页面,如下所示:

Menu:
Sign In
Sign Up
Sign Up
You are about to use your Google account to login to example.com. As a final step, please complete the following form:

Username: 
testuser

An account already exists with this e-mail address. Please sign in to that account first, then connect your Google account.
E-mail (optional): 
email@test.com

Sign Up »

有没有办法解决这个问题,所以它可以要求用户提供不同的电子邮件,而不是像这样中断?当用户之前没有通过应用程序注册时,不会出现此问题。

标签: djangoauthenticationgmail-apigoogle-developers-console

解决方案


简单的。创建一个测试函数,检查用户(尝试登录时)是否已经存在,基于电子邮件、用户名或你有什么,并将该消息返回给用户。

例如:

def login(request):
    if request.method == 'POST':
        # perform some logic
        try:
            User.objects.filter(email=email).exists()
            return "User with email already exists"
        except User.objects.filter(email=email).DoesNotExist():
            return "User doesn't exist yet"

推荐阅读