def post(self):
        """
        POST /login
        Expected: AuthDto.credentials
        """
        user = UserService.get_by_username(request.json.get("username"))

        if user is None:
            return self.format_failure(401, "Login Failed")

        password_valid = user.verify_password(request.json.get("password"))
        if not password_valid:
            return self.format_failure(401, "Login Failed")

        access, refresh = generate_jwt_keypair(user.id, user.tribe_id,
                                               user.role)

        return self.format_success(
            200, {
                "user": user.dictionary,
                "tokens": {
                    "access": access,
                    "refresh": refresh
                }
            })
    def postvalidation(self):
        """
        Ensure that the username is unique and the tribe exists if provided
        """
        existing_user = UserService.get_by_username(
            request.json.get("username"))

        if existing_user is not None:
            self.add_error("username", "Username already exists")
            return

        tribe_id = request.json.get("tribe_id")
        if tribe_id is None:
            self.lookup_cache.add("tribe", None)
            return

        tribe = TribeService.get_by_public_id(tribe_id)
        if tribe is None:
            self.add_error("tribe_id", "Tribe not found")
        self.lookup_cache.add("tribe", tribe)
Esempio n. 3
0
 def get(self, username):
     # Fetching the user id
     return UserService.get_by_username(username)