Currently I am generating tokens manually, but I want to to use jwt
tokens, I followed the official docs and other references but I am still unable to figure out the problem.
serializers.py , in which after authenticating token is generated manually.
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),
]
In settings I have included 'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
I have used url(r'^api-token-auth/', obtain_jwt_token)
, but I am unable to figure out how will I generate jwt
tokens. Please anyone help me out!
You need to make a HTTP POST call to the API on the endpoint /api-token-auth/
. In this call, the body will contain two keys - username
and password
.
Example using cURL :
curl -X POST -d username=abcd -d password=1234 http://ip:port/api-token-auth/
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.