2017-09-23 2 views
0

Actuellement, je génère des jetons manuellement, mais je veux utiliser les jetons jwt, j'ai suivi les documents officiels et d'autres références mais je n'arrive toujours pas à comprendre le problème.Obtention du jeton DRF-jwt

serializers.py, dans lequel le jeton d'authentification est généré manuellement.

class UserLoginSerializer(serializers.ModelSerializer): 
    token = serializers.CharField(allow_blank=True, read_only=True) 

    class Meta: 
     model = User 
     fields = [ 
      'username', 
      'password', 
      'token', 
     ] 
     extra_kwargs = {"password": 
          {"write_only": True} 
         } 

    def validate(self, data): 
     username = data.get('username', None) 
     password = data.get('password', None) 

     try: 
      usern = Account.objects.get(username=username) 
     except ObjectDoesNotExist: 
      raise serializers.ValidationError("User does not exists") 

     if usern.check_password(password): 
      data["token"] = "asdasdasdasd" 
     else: 
      raise serializers.ValidationError("password invalid") 

     return data 

urls.py

from django.conf.urls import url 
from .views import AuthRegister, AuthLogin 
from rest_framework_jwt.views import obtain_jwt_token 

urlpatterns = [ 
    url(r'^register/$', AuthRegister.as_view()), 
    url(r'^login/$', AuthLogin.as_view()), 
    url(r'^api-token-auth/', obtain_jwt_token), 
] 

Dans les paramètres que j'ai inclus 'rest_framework_jwt.authentication.JSONWebTokenAuthentication',

Je l'ai utilisé url(r'^api-token-auth/', obtain_jwt_token), mais je suis incapable de comprendre comment vais-je générer jwt jetons. S'il vous plaît, aidez-moi!

Répondre

0

Vous devez effectuer un appel HTTP POST à ​​l'API sur le noeud final /api-token-auth/. Dans cet appel, le corps contiendra deux clés - username et password.

Exemple d'utilisation cURL:

curl -X POST -d username=abcd -d password=1234 http://ip:port/api-token-auth/