-
Notifications
You must be signed in to change notification settings - Fork 673
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
User Creation and Custom Responses #34
Comments
I have this working reasonably well, but the code could probably be a bit cleaner. If I post a from rest_framework import serializers, permissions, status
from rest_framework.generics import CreateAPIView
from rest_framework.response import Response
from rest_framework.validators import UniqueValidator
from rest_framework_simplejwt.serializers import TokenObtainPairSerializer
from bp_auth.models import CustomUser as User
class SignupSerializer(serializers.ModelSerializer):
password = serializers.CharField(write_only=True)
email = serializers.CharField(
validators=[UniqueValidator(queryset=User.objects.all())]
)
class Meta:
model = User
fields = ('email', 'password')
class MyTokenObtainPairSerializer(TokenObtainPairSerializer):
@classmethod
def get_token(cls, user):
token = super(MyTokenObtainPairSerializer, cls).get_token(user)
token['email'] = user.email
return token
class RegisterUsers(CreateAPIView):
serializer_class = SignupSerializer
permission_classes = (permissions.AllowAny,)
def post(self, request, *args, **kwargs):
serialized_user = SignupSerializer(data=request.data)
if serialized_user.is_valid():
User.objects.create_user(
serialized_user.initial_data['email'],
serialized_user.initial_data['password']
)
tokens = MyTokenObtainPairSerializer(request.data).validate(request.data)
return Response(tokens, status=status.HTTP_201_CREATED)
else:
return Response(serialized_user._errors, status=status.HTTP_400_BAD_REQUEST) |
I have try code from @ckeeney and then i have case , response become I think, that code must use table User Auth, becaus I use custom table user to this code.. how can i Fix that ? should i use custom JWT for this case ? thanks |
Hi, serializer.py
view.py
This is a copy/past of my prod code so feel free to ask for more details |
from rest_framework_simplejwt.tokens import RefreshToken def get_tokens_for_user(user):
The above function get_tokens_for_user will return the serialized representations of new refresh and access tokens for the given user. In general, a token for any subclass of rest_framework_simplejwt.tokens.Token can be created in this way. |
hello this example can help you |
David, thanks for the great package.
I had a couple of issues I was running into that hopefully have an easy fix.
First was authenticating Users upon account creation. I tried implementing a serializer that inherited from
TokenObtainPairSerializer
.Upon authentication, I would add the token to the payload I passed to the frontend. The problem with this function is that it returned a jti, expiration and token type, but the actual token always returned None.
The other issues I wanted to tackled was modifying the MyTokenObtainPair end point to include user information in the Response.
Thanks for the guidance.
The text was updated successfully, but these errors were encountered: