return token after registration with django-rest-framework-simplejwt

后端 未结 3 1923
独厮守ぢ
独厮守ぢ 2021-02-04 22:46

I\'m using django-rest-framework-simplejwt and was wondering if it\'s possible to return a token after registering a user?

This post has a solution for another jwt packa

3条回答
  •  情深已故
    2021-02-04 23:11

    another possible solution is:

    in your view

    from rest_framework_simplejwt.serializers import TokenObtainPairSerializer
    from rest_framework_simplejwt.tokens import AccessToken, RefreshToken
    
    
    @login_required
    def index(request):
        tokenr = TokenObtainPairSerializer().get_token(request.user)  
        tokena = AccessToken().for_user(request.user)
        return render(request,'myview/index.html', {"refresh" : str(tokenr),"access" : str(tokena)} )
    

    I used @login_required just to be sure we have a request.user authenticated, but you could pass a dict instead

提交回复
热议问题