Beispiel #1
0
    def post(self):
        data = self.parser.parse_args()
        if data['email'] not in email_confirm_table.keys():
            return {
                "message":
                "no reset code associates with email {}".format(data['email'])
            }, 404
        if data['reset_code'] == email_confirm_table[data['email']]:
            # del email_confirm_table[data['email']]
            user = UserModel.find_by_email(data['email'])
            if user:
                user.password_hash = generate_password_hash(
                    data['new_password'])
                user.save_to_db()
                return {
                    "message":
                    "password updated successfully for {}".format(
                        user.username)
                }, 200
            else:
                return {
                    "message":
                    "user with email {} not found.".format(data['email'])
                }, 404

        else:
            return {"message": "Incorrect reset code."}, 401
Beispiel #2
0
    def post(self):
        email = self.parser.parse_args()['email']
        user = UserModel.find_by_email(email=email)
        if user:
            confirm_email_owner(username=user.username, recipient=email)
            return {
                "message": "password reset code emailed to {}".format(email)
            }, 200

        else:
            return {
                "message": "no user with email {} can be found.".format(email)
            }, 404
Beispiel #3
0
    def post(self):
        data = self.parser.parse_args()
        role = "USER"
        profile_img = "0"  # str type, 0~99 preset images
        password_hash = generate_password_hash(data["password"])

        user = UserModel.find_by_username(data["username"])
        if user:
            return {"message": "username already exists."}, 400

        user = UserModel.find_by_email(data["email"])
        if user:
            return {"message": "email already exists."}, 400

        user = UserModel(role=role,
                         username=data["username"],
                         password_hash=password_hash,
                         email=data["email"],
                         profile_img=profile_img)
        try:
            user.save_to_db()
            identity = {"role": user.role, "id": user.id}

            registration_confirmation(username=user.username,
                                      recipient=user.email)

            access_token = create_access_token(identity=identity,
                                               fresh=True,
                                               expires_delta=self.expires)
            refresh_token = create_refresh_token(identity=identity)
        except:
            return {
                "message": "something went wrong during user registration."
            }, 500

        return {
            "message": "user registered!",
            "role": user.role,
            "id": user.id,
            "username": user.username,
            "email": user.email,
            "profile_img": user.profile_img,
            "reg_date": str(user.date),
            "access_token": access_token,
            "refresh_token": refresh_token
        }, 201
Beispiel #4
0
 def validate_email(self, email):
     if UserModel.find_by_email(email.data):
         raise ValidationError("your email has been registered.")