nrbnlulu / strawberry-django-auth

Authentication system for django using strawberry
https://nrbnlulu.github.io/strawberry-django-auth/
MIT License
64 stars 30 forks source link

Unauthenticated issue: gqlauth.core.types_.GQLAuthError: Unauthenticated. #483

Closed BastiaanRudolf closed 7 months ago

BastiaanRudolf commented 7 months ago

Description

First of all thank you for all the time and effort you've put in this package! I really want to use it, but I've been running into an authentication issue for the last few days. I've followed the tutorial in the docs and everything works, except for the MeQuery at the very end, to which I get the following response:

{
    "data": null,
    "errors": [
        {
            "message": "Unauthenticated.",
            "locations": [
                {
                    "line": 13,
                    "column": 5
                }
            ],
            "path": [
                "me"
            ]
        }
    ]
}

With the full traceback:

Unauthenticated.

GraphQL request:13:5
12 | query Me {
13 |     me {
   |     ^
14 |         isSuperuser
Traceback (most recent call last):
  File "/Users/<REDACTED>/.pyenv/versions/3.10.12/lib/python3.10/site-packages/graphql/execution/execute.py", line 521, in execute_field
    result = resolve_fn(source, info, **args)
  File "/Users/<REDACTED>/.pyenv/versions/3.10.12/lib/python3.10/site-packages/strawberry_django/optimizer.py", line 810, in resolve
    ret = _next(root, info, *args, **kwargs)
  File "/Users/<REDACTED>/.pyenv/versions/3.10.12/lib/python3.10/site-packages/strawberry/schema/schema_converter.py", line 671, in _resolver
    return _get_result_with_extensions(
  File "/Users/<REDACTED>/.pyenv/versions/3.10.12/lib/python3.10/site-packages/strawberry/schema/schema_converter.py", line 658, in extension_resolver
    return reduce(
  File "/Users/<REDACTED>/.pyenv/versions/3.10.12/lib/python3.10/site-packages/strawberry/schema/schema_converter.py", line 653, in wrapped_get_result
    return _get_result(
  File "/Users/<REDACTED>/.pyenv/versions/3.10.12/lib/python3.10/site-packages/strawberry/schema/schema_converter.py", line 614, in _get_result
    return field.get_result(
  File "/Users/<REDACTED>/.pyenv/versions/3.10.12/lib/python3.10/site-packages/strawberry_django/fields/field.py", line 165, in get_result
    result = self.resolver(source, info, args, resolver_kwargs)
  File "/Users/<REDACTED>/.pyenv/versions/3.10.12/lib/python3.10/site-packages/strawberry_django/fields/base.py", line 209, in resolver
    return self.safe_resolver(*args, **kwargs)
  File "/Users/<REDACTED>/.pyenv/versions/3.10.12/lib/python3.10/site-packages/strawberry_django/resolvers.py", line 112, in inner_wrapper
    return f(*args, **kwargs)
  File "/Users/<REDACTED>/.pyenv/versions/3.10.12/lib/python3.10/site-packages/strawberry_django/resolvers.py", line 79, in sync_resolver
    retval = resolver(*args, **kwargs)
  File "/Users/<REDACTED>/.pyenv/versions/3.10.12/lib/python3.10/site-packages/strawberry/types/fields/resolver.py", line 203, in __call__
    return self.wrapped_func(*args, **kwargs)
  File "/Users/<REDACTED>/.pyenv/versions/3.10.12/lib/python3.10/site-packages/gqlauth/user/queries.py", line 36, in me
    raise GQLAuthError(code=GQLAuthErrors.UNAUTHENTICATED)
gqlauth.core.types_.GQLAuthError: Unauthenticated.

Steps to Reproduce

FYI I'm using from gqlauth.user import relay as mutations and a CustomUser with the setting AUTH_USER_MODEL = 'users.CustomUser'

  1. Run the register mutation
    mutation Register {
    register(
        input: { password1: "SuperSecureP@ssw0rd", password2: "SuperSecureP@ssw0rd", username: "Dummy", email: "dummy@dum.dum" }
    ) {
        success
        errors
    }
    }
  2. Run the verifyAccount mutation with the token received from the email
    mutation VerifyAccount {
    verifyAccount(
        input: {
            token: "eyJ1c2VybmFtZSI6IkR1bW15IiwiYWN0aW9uIjoiYWN0aXZhdGlvbiJ9:1rl6lV:cjoYCOEERGK5m-ECEFB6PWHsRM5dCX59gdrWM_vDXYI"
        }
    ) {
        success
        errors
    }
    }
  3. Run the tokenAuth mutation
    mutation TokenAuth {
    tokenAuth(input: { password: "SuperSecureP@ssw0rd", username: "Dummy" }) {
        success
        errors
        token {
            token
        }
    }
    }
  4. Use the provided token in a header with key authorization and value JWT ey... and run the me query
    query Me {
    me {
        email
        username
    }
    }

Requirements

aniso8601==9.0.1
anyio==4.3.0
asgiref==3.7.2
async-timeout==4.0.3
attrs==23.2.0
autobahn==23.6.2
Automat==22.10.0
cattrs==23.2.3
cffi==1.16.0
click==8.1.7
constantly==23.10.4
constructs==10.3.0
cryptography==42.0.5
daphne==2.5.0
Django==5.0
django-cors-headers==4.3.1
django-graphql-jwt==0.4.0
django-stubs==4.2.7
django-stubs-ext==4.2.7
exceptiongroup==1.2.0
graphene==3.3
graphene-django==3.2.0
graphql-core==3.2.3
graphql-relay==3.2.0
gunicorn==21.2.0
h11==0.14.0
httptools==0.6.1
hyperlink==21.0.0
idna==3.6
importlib_resources==6.1.3
incremental==22.10.0
jsii==1.95.0
msgpack==1.0.8
mypy==1.7.1
mypy-extensions==1.0.0
mysqlclient==2.2.4
packaging==23.2
promise==2.3
publication==0.0.3
pyasn1==0.5.1
pyasn1-modules==0.3.0
pycparser==2.21
PyJWT==2.8.0
pyOpenSSL==24.1.0
python-dateutil==2.9.0.post0
python-dotenv==1.0.1
PyYAML==6.0.1
redis==5.0.3
Rx==1.6.3
service-identity==24.1.0
singledispatch==4.1.0
six==1.16.0
sniffio==1.3.1
sqlparse==0.4.4
strawberry-django-auth==0.376.5
strawberry-graphql==0.220.0
strawberry-graphql-django==0.33.0
text-unidecode==1.3
tomli==2.0.1
Twisted==24.3.0
txaio==23.1.1
typeguard==2.13.3
types-pytz==2024.1.0.20240203
types-PyYAML==6.0.12.20240311
typing_extensions==4.10.0
uvicorn==0.27.1
uvloop==0.19.0
watchfiles==0.21.0
websockets==12.0
whitenoise==6.6.0
zope.interface==6.2

Any help or guidance is greatly appreciated! šŸ™

nrbnlulu commented 7 months ago

Hey, Are you using the JWTSchema?

BastiaanRudolf commented 7 months ago

Thank you for your response! I'm not at the moment. Should I?

nrbnlulu commented 7 months ago

I think that might solve it yeah.

BastiaanRudolf commented 7 months ago

You are right. That did the trick, thanks a lot! šŸ™

For others reading this, in my schema.py I changed:

schema = strawberry.Schema(
    query=Query,
    mutation=Mutation,
    extensions=[
        DjangoOptimizerExtension,  # not required, but highly recommended
    ],
)

to:

from gqlauth.core.middlewares import JwtSchema
...
schema = JwtSchema(
    query=Query,
    mutation=Mutation,
    extensions=[
        DjangoOptimizerExtension,  # not required, but highly recommended
    ],
)
nrbnlulu commented 7 months ago

I think the docs lacks a note about that... I want to remove that schema anyways though...