· 7 years ago · Sep 26, 2018, 08:20 AM
1{
2 "detail": "Authentication credentials were not provided."
3}
4
5REST_FRAMEWORK = {
6 'DEFAULT_PERMISSION_CLASSES': (
7 'rest_framework.permissions.IsAuthenticated',
8 ),
9 'DEFAULT_AUTHENTICATION_CLASSES': (
10 'rest_framework.authentication.SessionAuthentication',
11 'rest_framework.authentication.BasicAuthentication',
12 ),
13}
14
15JWT_AUTH = {
16 'JWT_ALLOW_REFRESH': True,
17 'JWT_EXPIRATION_DELTA': datetime.timedelta(hours=1),
18 'JWT_REFRESH_EXPIRATION_DELTA': datetime.timedelta(days=7),
19}
20
21def post(self, request, format=None):
22
23 if not request.data:
24 return Response({'Error': "Please provide username/password"}, status=status.HTTP_400_BAD_REQUEST)
25
26 username = request.data['username']
27 password = request.data['password']
28
29 try:
30 user = User.objects.get(email=username, password=password)
31 except User.DoesNotExist:
32 return Response({'Error': "Invalid username/password"}, status=status.HTTP_400_BAD_REQUEST)
33
34 if user:
35 payload = {
36 'id': user.id,
37 'email': user.email,
38 'first_name': user.first_name
39 }
40
41 jwt_token = jwt.encode(payload, "SECRET_KEY") # to be changed
42
43 return Response({'token': jwt_token}, status=status.HTTP_200_OK)
44
45authentication_class = (JSONWebTokenAuthentication,)
46 permission_classes = (IsAuthenticated,)