コード例 #1
0
    def post(self):
        """Login user"""
        post_data = request.get_json()
        email = post_data.get("email")
        password = post_data.get("password")

        res = {"status": False, "message": "Invalid payload"}

        if email is None or password is None:
            return res, 400

        valid_email = EMAIL_REGEX.match(email)

        if valid_email is None:
            res["message"] = "Please provide a valid email address"
            return res, 400

        current_user = get_user_by_email(email)
        if current_user is None or not bcrypt.check_password_hash(
            current_user.password, password
        ):
            res["message"] = "User does not exist."
            return res, 404

        access_token = current_user.encode_token(current_user.id, "access")
        refresh_token = current_user.encode_token(current_user.id, "refresh")

        res = {
            "access_token": access_token.decode(),
            "refresh_token": refresh_token.decode(),
        }

        return res, 200
コード例 #2
0
ファイル: views.py プロジェクト: nicholaspretorius/testmaps
    def post(self):
        """Create user"""
        post_data = request.get_json()
        email = post_data.get("email")
        password = post_data.get("password")
        res = {"status": False, "message": "Invalid payload"}

        if email is None or password is None:
            return res, 400

        valid_email = EMAIL_REGEX.match(email)

        if valid_email is None:
            res["message"] = "Please provide a valid email address"
            return res, 400

        current_user = get_user_by_email(email)
        if current_user:
            res["message"] = "Sorry, that email already exists."
            return res, 400

        new_user = create_user(email, password)
        res["status"] = True
        res["message"] = f"{email} was added!"
        res["user"] = new_user.to_json()
        return res, 201
コード例 #3
0
    def post(self):
        """Register user"""
        post_data = request.get_json()
        print("post_data", post_data)
        print("post_data type", type(post_data))
        print("email", post_data.get("email"))
        print("password", post_data.get("password"))
        email = post_data.get("email")
        password = post_data.get("password")
        res = {"status": False, "message": "Invalid payload"}

        if email is None or password is None:
            return res, 400

        valid_email = EMAIL_REGEX.match(email)

        if valid_email is None:
            res["message"] = "Please provide a valid email address."
            return res, 400

        current_user = get_user_by_email(email)
        print("User:"******"message"] = "Sorry, that email already exists."
            return res, 400

        new_user = create_user(email, password)
        return new_user.to_json(), 201