Пример #1
0
    def get(self, request):
        public_address = self.request.query_params.get('public_address')
        user_details = User.objects.get(public_address=public_address)

        user = authenticate(request,
                            username=user_details.username,
                            password="******")
        if user is not None:
            # login saves the user’s ID in the session,
            # using Django’s session framework.
            login(request, user)

            token_serializer = TokenSerializer(
                data={
                    # using drf jwt utility functions to generate a token
                    "token": jwt_encode_handler(jwt_payload_handler(user))
                })
            token_serializer.is_valid()
            data_dict = {
                "status": 200,
                "data": {
                    "token": token_serializer.data
                }
            }
            return Response(data_dict, status=status.HTTP_200_OK)
        return Response(
            {
                "status": 404,
                "error": "username or password incorrect"
            },
            status=status.HTTP_404_NOT_FOUND)
Пример #2
0
 def post(self, request, *args, **kwargs):
     username = request.data.get("username", "")
     password = request.data.get("password", "")
     user = authenticate(request, username=username, password=password)
     if user is not None:
         # login saves the user’s ID in the session,
         # using Django’s session framework.
         login(request, user)
         token_serializer = TokenSerializer(
             data={
                 # using drf jwt utility functions to generate a token
                 "token": jwt_encode_handler(jwt_payload_handler(user))
             })
         token_serializer.is_valid()
         user_serializer = UserDetailSerializer(user)
         if user.is_institution_administrator == True:
             try:
                 related_institution = Institution.objects.get(
                     created_by=user)
                 institution = {
                     "id":
                     related_institution.pk,
                     "institution_status":
                     related_institution.is_institution_active
                 }
             except:
                 institution = {"status": "No institution created yet"}
         elif user.is_loan_officer == True:
             institution_staff = InstitutionStaff.objects.get(user_id=user)
             assigned_institution = Institution.objects.get(
                 id=institution_staff.institution_id.pk)
             institution = {
                 "id":
                 assigned_institution.pk,
                 "institution_status":
                 assigned_institution.is_institution_active
             }
         elif user.is_teller == True:
             institution_staff = InstitutionStaff.objects.get(user_id=user)
             assigned_institution = Institution.objects.get(
                 id=institution_staff.institution_id.pk)
             institution = {
                 "id":
                 assigned_institution.pk,
                 "institution_status":
                 assigned_institution.is_institution_active
             }
         login_data = {
             "user_data": user_serializer.data,
             "token_data": token_serializer.data,
             "institution": institution
         }
         return Response(login_data)
     return Response(status=status.HTTP_404_NOT_FOUND)
Пример #3
0
 def post(self, request, *args, **kwargs):
     username = request.data.get("username", "")
     password = request.data.get("password", "")
     user = authenticate(request, username=username, password=password)
     if user is not None:
         # login saves the user’s ID in the session,
         # using Django’s session framework.
         login(request, user)
         token_serializer = TokenSerializer(data={
             # using drf jwt utility functions to generate a token
             "token": jwt_encode_handler(
                 jwt_payload_handler(user)
             )})
         token_serializer.is_valid()
         return Response(token_serializer.data)
     return Response(status=status.HTTP_404_NOT_FOUND)
Пример #4
0
    def post(self, request, *args, **kwargs):
        email = request.data.get("email", "")
        password = request.data.get("password", "")

        try:
            user = User.objects.get(email=email)
        except User.DoesNotExist:
            return Response(status=status.HTTP_401_UNAUTHORIZED)

        if user is not None and user.check_password(password):
            login(request, user)
            serializer = TokenSerializer(
                data={"token": jwt_encode_handler(jwt_payload_handler(user))})
            serializer.is_valid()
            return Response(serializer.data)
        return Response(status=status.HTTP_401_UNAUTHORIZED)
 def test_serialized_and_then_deserialized_data_is_valid(self):
     token = Token(email=TEST_EMAIL)
     token.save()
     serializer = TokenSerializer(token)
     content = JSONRenderer().render(serializer.data)
     stream = BytesIO(content)
     data = JSONParser().parse(stream)
     serializer = TokenSerializer(data=data)
     self.assertTrue(serializer.is_valid())
Пример #6
0
 def test_serialized_and_then_deserialized_data_is_valid(self):
     token = Token(email=TEST_EMAIL)
     token.save()
     serializer = TokenSerializer(token)
     content = JSONRenderer().render(serializer.data)
     stream = BytesIO(content)
     data = JSONParser().parse(stream)
     serializer = TokenSerializer(data=data)
     self.assertTrue(serializer.is_valid())
Пример #7
0
    def post(self, request, *args, **kwargs):
        username = request.data.get("username", "")
        password = request.data.get("password", "")
        user = authenticate(request, username=username, password=password)
        if user is not None:
            # login saves the user’s ID in the session,
            # using Django’s session framework.
            login(request, user)

            token_serializer = TokenSerializer(
                data={
                    # using drf jwt utility functions to generate a token
                    "token": jwt_encode_handler(jwt_payload_handler(user))
                })
            token_serializer.is_valid()
            if user.is_administrator == True:
                data_dict = {
                    "status": 200,
                    "data": {
                        "token": token_serializer.data,
                        "role": "admin"
                    }
                }
                return Response(data_dict, status=status.HTTP_200_OK)
            elif user.is_store_owner == True:
                data_dict = {
                    "status": 200,
                    "data": {
                        "token": token_serializer.data,
                        "role": "store_owner"
                    }
                }
                return Response(data_dict, status=status.HTTP_200_OK)
        return Response(
            {
                "status": 404,
                "error": "username or password incorrect"
            },
            status=status.HTTP_404_NOT_FOUND)