| 77 | }, |
| 78 | ) |
| 79 | def post(self, request, format=None): |
| 80 | try: |
| 81 | data = json.loads(request.body) |
| 82 | except json.JSONDecodeError: |
| 83 | return Response( |
| 84 | {"detail": "Invalid JSON."}, status=status.HTTP_400_BAD_REQUEST |
| 85 | ) |
| 86 | |
| 87 | if data is None: |
| 88 | return Response( |
| 89 | {"detail": "No data provided."}, status=status.HTTP_400_BAD_REQUEST |
| 90 | ) |
| 91 | |
| 92 | username = data.get("username") |
| 93 | password = data.get("password") |
| 94 | if username is None or password is None: |
| 95 | return Response( |
| 96 | {"detail": "Please provide username and password."}, |
| 97 | status=status.HTTP_400_BAD_REQUEST, |
| 98 | ) |
| 99 | user = authenticate(username=username, password=password) |
| 100 | |
| 101 | if user is None: |
| 102 | return JsonResponse( |
| 103 | {"detail": "Invalid credentials."}, status=status.HTTP_400_BAD_REQUEST |
| 104 | ) |
| 105 | |
| 106 | user_team = user.team |
| 107 | if not all( |
| 108 | [ |
| 109 | x != Organization.OrganizationType.EXTERNAL_DEMO |
| 110 | for x in user_team.organizations.all().values_list( |
| 111 | "organization_type", flat=True |
| 112 | ) |
| 113 | ] |
| 114 | ): |
| 115 | return JsonResponse( |
| 116 | {"detail": "Cannot login in to Lotus app with a demo account."}, |
| 117 | status=status.HTTP_400_BAD_REQUEST, |
| 118 | ) |
| 119 | |
| 120 | login(request, user) |
| 121 | posthog.capture( |
| 122 | POSTHOG_PERSON if POSTHOG_PERSON else username, |
| 123 | event="succesful login", |
| 124 | properties={"organization": user.organization.organization_name}, |
| 125 | ) |
| 126 | token = AuthToken.objects.create(user) |
| 127 | return Response( |
| 128 | { |
| 129 | "detail": "Successfully logged in.", |
| 130 | "token": token[1], |
| 131 | "user": UserSerializer(user).data, |
| 132 | } |
| 133 | ) |
| 134 | |
| 135 | |
| 136 | class DemoLoginView(LoginViewMixin, APIView): |