eadwinCode / django-ninja-jwt

A JSON Web Token authentication plugin for the Django REST Framework.
https://eadwincode.github.io/django-ninja-jwt/
MIT License
149 stars 21 forks source link

How to get user groups #58

Closed Bouni closed 10 months ago

Bouni commented 10 months ago

How can I get the groups of a user when getting the TokenPair?

This is what I tried but I get an empty groups list as result for user = UserSchema.from_orm(self._user)

from django.contrib.auth import get_user_model
from django.contrib.auth.models import Group
from ninja_extra import api_controller, route
from ninja_jwt.controller import TokenObtainPairController
from ninja_jwt.schema import TokenObtainPairSerializer
from ninja_schema import ModelSchema, Schema

User = get_user_model()

class GroupSchema(ModelSchema):
    class Config:
        model = Group
        include = ("name",)

class UserSchema(Schema):
    first_name: str
    last_name: str
    username: str
    email: str
    groups: list[GroupSchema]

class TokenObtainPairOutSchema(Schema):
    refresh: str
    access: str
    user: UserSchema

class TokenObtainPairSchema(TokenObtainPairSerializer):
    def output_schema(self):
        out_dict = self.get_response_schema_init_kwargs()
        user = UserSchema.from_orm(self._user)
        out_dict.update(user=user)
        return TokenObtainPairOutSchema(**out_dict)

@api_controller("/auth/token", tags=["Auth"])
class CustomTokenObtainPairController(TokenObtainPairController):
    auto_import = True
    @route.post("/pair", response=TokenObtainPairOutSchema, url_name="token_obtain_pair")
    def obtain_token(self, user_token: TokenObtainPairSchema):
        return user_token.output_schema()
eadwinCode commented 10 months ago

@Bouni we are not joining Group table when fetching for user for authentication. But this is supposed to be lazy loaded though. Have you checked if the user belongs to a group?

eadwinCode commented 10 months ago

@Bouni whats the version ninja-jwt are you on?

eadwinCode commented 10 months ago

I ran your code and I got this. I think your user is not assigned to a group

{
  "refresh": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbl90eXBlIjoicmVmcmVzaCIsImV4cCI6MTcwNTA3MzM3OCwiaWF0IjoxNzA0OTg2OTc4LCJqdGkiOiI5MWM4MWQyYmU0MmQ0YjI4OWM0ZWUwMGI0NzhkZGVkMyIsInVzZXJfaWQiOjF9.y-Z2kk5nHfGcPxixI3WgNNLwCW9EcY2LVgJ3pxuWfmk",
  "access": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNzA0OTg3Mjc4LCJpYXQiOjE3MDQ5ODY5NzgsImp0aSI6IjExYjA4OWM0YWI1NjRhNThhMjBkM2Y5MGVkN2M1NjFmIiwidXNlcl9pZCI6MX0.SLT4YbmdLtzmx4zcrcxH9hKnN08TohM-eB4whDVCSCk",
  "user": {
    "first_name": "",
    "last_name": "",
    "username": "person.username_1",
    "email": "person1@gmail.com",
    "groups": [
      {
        "name": "client"
      },
      {
        "name": "group1"
      }
    ]
  }
}
Bouni commented 10 months ago

🤔 I have to check tomorrow. I'm almost sure I created a group and assigned the user but I might have missed something

Bouni commented 10 months ago

@eadwinCode I'm such an idiot and you were absolutely right. I prepared the group but did not assign it to the user 😅

eadwinCode commented 10 months ago

@Bouni You are not an idiot😅... Its just a human error... It happens