From 6ee9f5d50260d541415f0518f3197f95ec852f80 Mon Sep 17 00:00:00 2001 From: Vinicius Date: Tue, 16 Apr 2024 08:59:01 -0300 Subject: [PATCH] feat: add obtain bearer token with user info --- bd_api/custom/graphql_auto.py | 10 ++++++---- bd_api/custom/graphql_jwt.py | 15 +++++++++++++++ 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/bd_api/custom/graphql_auto.py b/bd_api/custom/graphql_auto.py index 4cfe0efe..c7c55d48 100644 --- a/bd_api/custom/graphql_auto.py +++ b/bd_api/custom/graphql_auto.py @@ -9,7 +9,6 @@ from functools import partial from typing import Iterable, Optional, get_type_hints -import graphql_jwt from django.apps import apps from django.core.exceptions import ValidationError from django.db import models @@ -40,8 +39,10 @@ ) from graphene_django.registry import get_global_registry from graphene_file_upload.scalars import Upload +from graphql_jwt import ObtainJSONWebToken, Refresh, Verify from bd_api.custom.graphql_base import CountableConnection, FileFieldScalar, PlainTextNode +from bd_api.custom.graphql_jwt import ObtainJSONWebTokenWithUser from bd_api.custom.model import BaseModel @@ -245,9 +246,10 @@ def build_mutation_schema(application_name: str): base_mutations = build_mutation_objs(application_name) base_mutations.update( { - "token_auth": graphql_jwt.ObtainJSONWebToken.Field(), - "verify_token": graphql_jwt.Verify.Field(), - "refresh_token": graphql_jwt.Refresh.Field(), + "token_auth": ObtainJSONWebToken.Field(), + "auth_token": ObtainJSONWebTokenWithUser.Field(), + "verify_token": Verify.Field(), + "refresh_token": Refresh.Field(), } ) mutation = type("Mutation", (ObjectType,), base_mutations) diff --git a/bd_api/custom/graphql_jwt.py b/bd_api/custom/graphql_jwt.py index ba33139c..5a6e510a 100644 --- a/bd_api/custom/graphql_jwt.py +++ b/bd_api/custom/graphql_jwt.py @@ -2,12 +2,27 @@ from functools import wraps from re import findall +from graphene import Field, ObjectType, String from graphql_jwt import exceptions from graphql_jwt.compat import get_operation_name from graphql_jwt.decorators import context +from graphql_jwt.relay import JSONWebTokenMutation from graphql_jwt.settings import jwt_settings +class User(ObjectType): + id = String() + email = String() + + +class ObtainJSONWebTokenWithUser(JSONWebTokenMutation): + user = Field(User) + + @classmethod + def resolve(cls, root, info, **kwargs): + return cls(user=info.context.user) + + def allow_any(info, **kwargs): """Custom function to determine the non-authentication per-field