跳转到主要内容

使用django graphene和JWT进行认证和注册

项目描述

https://badge.fury.io/py/graphene-jwt-auth-registration.svg https://travis-ci.org/fivethreeo/graphene-jwt-auth-registration.svg?branch=master https://codecov.io/gh/fivethreeo/graphene-jwt-auth-registration/branch/master/graph/badge.svg

使用graphene和JWT进行认证和注册

文档

快速入门

安装Graphene JWT Auth Registration

pip install graphene-jwt-auth-registration

将其添加到您的 INSTALLED_APPS

INSTALLED_APPS = [
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sites",

    ...

    "djoser",
    "graphene_django",

    "gjwt_auth",
]

设置AUTH_USER_MODEL

AUTH_USER_MODEL = "gjwt_auth.User"

将JSONWebTokenBackend后端添加到您的AUTHENTICATION_BACKENDS

AUTHENTICATION_BACKENDS = [
    'graphql_jwt.backends.JSONWebTokenBackend',
    'django.contrib.auth.backends.ModelBackend',
]

添加JSONWebTokenMiddleware

GRAPHENE = {
    'SCHEMA': 'yourproject.schema.schema',
    'MIDDLEWARE': [
        'graphql_jwt.middleware.JSONWebTokenMiddleware',
    ],
}

yourproject/schema.py 中创建graphene模式

import graphene
import graphql_jwt

from gjwt_auth.mutations import (
    Activate,
    DeleteAccount,
    Register,
    ResetPassword,
    ResetPasswordConfirm,
)

from gjwt_auth.schema import User, Viewer


class RootQuery(graphene.ObjectType):
    viewer = graphene.Field(Viewer)

    def resolve_viewer(self, info, **kwargs):
        if info.context.user.is_authenticated:
            return info.context.user
        return None


class Mutation(graphene.ObjectType):
    activate = Activate.Field()
    register = Register.Field()
    deleteAccount = DeleteAccount.Field()
    resetPassword = ResetPassword.Field()
    resetPasswordConfirm = ResetPasswordConfirm.Field()

    token_auth = graphql_jwt.ObtainJSONWebToken.Field()
    verify_token = graphql_jwt.Verify.Field()
    refresh_token = graphql_jwt.Refresh.Field()

schema = graphene.Schema(query=RootQuery, mutation=Mutation)

设置djoser设置

DOMAIN = os.environ.get('DJANGO_DJOSER_DOMAIN', 'localhost:3000')
SITE_NAME = os.environ.get('DJANGO_DJOSER_SITE_NAME', 'my site')

DJOSER = {

    'PASSWORD_RESET_CONFIRM_URL': '?action=set-new-password&uid={uid}&token={token}',
    'ACTIVATION_URL': 'activate?uid={uid}&token={token}',
    'SEND_ACTIVATION_EMAIL': True,
}

}

添加Graphenes URL模式

from django.conf.urls import url
from django.views.decorators.csrf import csrf_exempt

from graphene_django.views import GraphQLView

...

urlpatterns = [
    ...
    url(r'^graphql', csrf_exempt(GraphQLView.as_view(graphiql=True))),
    ...
]

运行测试

代码实际上是否工作?

source <YOURVIRTUALENV>/bin/activate
(myenv) $ pip install tox
(myenv) $ tox

致谢

在渲染此包中使用的工具

历史

0.1.0 (2019-06-04)

  • PyPI上的首次发布。

项目详情


下载文件

下载适用于您平台文件。如果您不确定选择哪个,请了解有关 安装包 的更多信息。

源分布

graphene-jwt-auth-registration-1.0.0.tar.gz (9.1 kB 查看哈希值)

上传时间:

构建分布

graphene_jwt_auth_registration-1.0.0-py2.py3-none-any.whl (10.1 kB 查看散列值)

上传时间 Python 2 Python 3

支持