Answers for "how to get token of jwt in django"


create jwt token in django

@permission_classes([AllowAny, ])
def authenticate_user(request):
        email =['email']
        password =['password']
        user = User.objects.get(email=email, password=password)
        if user:
                payload = jwt_payload_handler(user)
                token = jwt.encode(payload, settings.SECRET_KEY)
                user_details = {}
                user_details['name'] = "%s %s" % (
                    user.first_name, user.last_name)
                user_details['token'] = token
                                    request=request, user=user)
                return Response(user_details, status=status.HTTP_200_OK)
            except Exception as e:
                raise e
            res = {
                'error': 'can not authenticate with the given credentials or the account has been deactivated'}
            return Response(res, status=status.HTTP_403_FORBIDDEN)
    except KeyError:
        res = {'error': 'please provide a email and a password'}
        return Response(res)
Posted by: Guest on August-13-2020

Python Answers by Framework

Browse Popular Code Answers by Language