使用django graphene和JWT进行认证和注册
项目描述
使用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上的首次发布。
项目详情
关闭
散列值 for graphene-jwt-auth-registration-1.0.0.tar.gz
算法 | 散列摘要 | |
---|---|---|
SHA256 | 28e4f30d0f58914d62415e7fcd5549d05c30c740693a8ef48cc5dd3e5f753317 |
|
MD5 | db49f5d35d63726c961692083ee0f590 |
|
BLAKE2b-256 | 1fde94bdb25df249c53ea3efa1792db18e51a35845def66c451c8c7e7da53911 |
关闭
散列值 for graphene_jwt_auth_registration-1.0.0-py2.py3-none-any.whl
算法 | 散列摘要 | |
---|---|---|
SHA256 | 146a91f098d0f4769d62ccaef9d2a9f3a2193ecdc0ad39f59a86c6013eecc81b |
|
MD5 | 210686dd36cb6ca53996958817e41067 |
|
BLAKE2b-256 | 5a0168fb9be4e954ee593f4c6bfc665267c92b2cce958efad96f2bf66e3e558c |