与django graphene和jwt的认证和注册
graphene-jwt-auth-registration的Python项目详细描述
使用graphene和jwt进行身份验证和注册
文档
快速启动
安装Graphene JWT验证注册:
pip install graphene-jwt-auth-registration
将其添加到已安装的应用程序中
INSTALLED_APPS=["django.contrib.auth","django.contrib.contenttypes","django.contrib.sites",..."rest_framework","rest_framework_jwt","djoser","graphene_django","gjwt_auth",]
设置身份验证用户模型:
AUTH_USER_MODEL="gjwt_auth.User"
添加jwtauthenticationmiddleware:
MIDDLEWARE=[...'gjwt_auth.middleware.JWTAuthenticationMiddleware',]
在yourproject/schema.py中创建石墨烯模式:
importgraphenefromgjwt_auth.mutationsimport(Activate,DeleteAccount,Login,RefreshToken,Register,ResetPassword,ResetPasswordConfirm,)fromgjwt_auth.schemaimportUser,ViewerclassRootQuery(graphene.ObjectType):viewer=graphene.Field(Viewer)defresolve_viewer(self,info,**kwargs):ifinfo.context.user.is_authenticated:returninfo.context.userreturnNoneclassMutation(graphene.ObjectType):activate=Activate.Field()login=Login.Field()register=Register.Field()deleteAccount=DeleteAccount.Field()refreshToken=RefreshToken.Field()resetPassword=ResetPassword.Field()resetPasswordConfirm=ResetPasswordConfirm.Field()schema=graphene.Schema(query=RootQuery,mutation=Mutation)
设置石墨烯架构:
GRAPHENE={'SCHEMA':'yourproject.schema.schema'}
设置djoser设置:
DJOSER={'DOMAIN':os.environ.get('DJANGO_DJOSER_DOMAIN','localhost:3000'),'SITE_NAME':os.environ.get('DJANGO_DJOSER_SITE_NAME','my site'),'PASSWORD_RESET_CONFIRM_URL':'?action=set-new-password&uid={uid}&token={token}','ACTIVATION_URL':'activate?uid={uid}&token={token}','SEND_ACTIVATION_EMAIL':True,}
设置JWT身份验证设置:
JWT_AUTH={'JWT_ALLOW_REFRESH':True,}
添加graphene jwt auth的url模式:
fromdjango.conf.urlsimporturlfromdjango.views.decorators.csrfimportcsrf_exemptfromgraphene_django.viewsimportGraphQLView...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上的第一个版本。