python - How to create Json Web Token to User login in Django Rest Framework? -


i want add jwt user login api authenticate. should according codes? create token manuel. must change. how can integrated? thank you.

serializers

class userloginserializer(modelserializer): token = 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):     user_obj = none     username = data.get("username", none)     password = data["password"]     if not username:         raise validationerror("kullanıcı adı gerekli.")      user = user.objects.filter(         q(username=username)         ).distinct()     user = user.exclude(email__isnull=true).exclude(email__iexact='')     if user.exists() , user.count() == 1:         user = user.first()     else:         raise validationerror("böyle bir kullanıcı adı yoktur.")      if user_obj:         if not user_obj.check_password(password):             raise validationerror("tekrar deneyiniz.")     data["token"] = "asdasdasdasd"     return data 

views

class userloginapiview(apiview): permission_classes = [allowany] serializer_class = userloginserializer  def post(self, request, *args, **kwargs):     data = request.data     serializer = userloginserializer(data=data)     if serializer.is_valid(raise_exception=true):         new_data = serializer.data         return response(new_data, status=http_200_ok)     return response(serializer.errors, status=http_400_bad_request) 

settings

rest_framework = { 'default_permission_classes': (     'rest_framework.permissions.isauthenticated', ), 'default_authentication_classes': (     'rest_framework.authentication.sessionauthentication',     'rest_framework.authentication.basicauthentication',     'rest_framework_jwt.authentication.jsonwebtokenauthentication', ), 

}

urls

urlpatterns = [ url(r'^login/$', userloginapiview.as_view(), name='login'), url(r'^api-token-auth/', obtain_jwt_token), url(r'^api-token-refresh/', refresh_jwt_token), url(r'^api-token-verify/', verify_jwt_token), url(r'^register/$', usercreateapiview.as_view(), name='register'), 

]

automatically, can use 'rest_framework_jwt.views.obtain_jwt_token' user login. create token. , then, need go restrictedview , use token authentication. basicly, that's all.


Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -