是否可以将 django-allauth 设置为仅接受 google 登录?

问题描述 投票:0回答:1

我正在使用 Django 框架实现一个 Web 应用程序。 在我的业务中,我需要让用户只能通过谷歌登录来访问该应用程序。

我也不需要在我的数据库中注册用户,这不是必需的。我只需要用户使用他的谷歌帐户进入该网站,这样我就能够收到他的真实电子邮件并向他发送回会话结果。这是一款一次性应用程序。

我正在使用 django-allauth,但它默认公开了一种在本地登录和注册用户的方法。有没有办法禁用各种本地注册/登录并让用户只能通过谷歌登录进入应用程序?

谢谢你。

python django django-allauth google-signin
1个回答
0
投票

https://docs.allauth.org/en/latest/socialaccount/advanced.html#creating-and-populate-user-instances 您需要:

  • 禁用自定义注册
    ACCOUNT_ADAPTER
  • 仅通过自定义社交帐户(谷歌)启用注册
    SOCIALACCOUNT_ADAPTER

实践中:

# settings.py

ACCOUNT_ADAPTER = 'myapp.adapters.AccountAdapter'
SOCIALACCOUNT_ADAPTER = 'myapp.adapters.SocialAccountAdapter'
# myapp/adapters.py
from allauth.account.adapter import DefaultAccountAdapter
from allauth.socialaccount.adapter import DefaultSocialAccountAdapter


class AccountAdapter(DefaultAccountAdapter):

    def is_open_for_signup(self, request):
        return False


class SocialAccountAdapter(DefaultSocialAccountAdapter):

    def is_open_for_signup(self, request, sociallogin):
        return bool(sociallogin)

© www.soinside.com 2019 - 2024. All rights reserved.