首页 > 解决方案 > django all-auth 通过 twitch 进行身份验证

问题描述

我正在尝试使用 Django 和 reactjs 从 twitch Auth 验证用户。我能够授权用户并将其详细信息从后端存储在 Django 管理员中,但不知道如何从 reactjs 执行此操作。

到目前为止我尝试了什么:

按照这里的文件:https ://django-allauth.readthedocs.io/en/latest/installation.html

我将所有内容配置为:

设置.py:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    'rest_framework',
    'rest_framework.authtoken',
    'rest_auth',
    'django.contrib.sites',
    'allauth',
    'allauth.account',
    'allauth.socialaccount',  
    'allauth.socialaccount.providers.twitch',    
    'django_extensions',
    'users.apps.UsersConfig',
]

SITE_ID = 1

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = app-name.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'sphinx.wsgi.application'

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': [
        'rest_framework.authentication.TokenAuthentication',
    ],
}

我还在 Django 管理员中注册了社交应用程序。

当我击中:

http://localhost:8000/accounts/twitch/login/

它把我带到了 twitch 授权页面,当我点击“授权”时,Django 管理员中的用户详细信息将我重定向回 url:

http://localhost:8000/accounts/profile/

问题是我需要确保 LOGIN_REDIRECT_URL 页面 '/accounts/profile/' 具有一些身份验证,因此只有登录用户才能访问它并显示该用户的用户名。

如何对用户进行身份验证?比如我如何将 access_token 发送到前端 reactjs 并验证用户?

标签: pythondjangotwitch

解决方案


推荐阅读