Skip to content

Commit

Permalink
feat: add obtain bearer token with user info
Browse files Browse the repository at this point in the history
  • Loading branch information
vncsna committed Apr 16, 2024
1 parent 43c468d commit 6ee9f5d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
10 changes: 6 additions & 4 deletions bd_api/custom/graphql_auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -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)
Expand Down
15 changes: 15 additions & 0 deletions bd_api/custom/graphql_jwt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6ee9f5d

Please sign in to comment.