示例#1
0
    def post(self):
        data = self.user_parser.parse_args()
        user = UserModel.find_by_username(data['username'])

        if not user:
            return {"message": "username does not exist."}, 404

        if check_password_hash(user.password_hash, data['password']):
            identity = {"role": user.role, "id": user.id}
            access_token = create_access_token(identity=identity,
                                               fresh=True,
                                               expires_delta=self.expires)
            refresh_token = create_refresh_token(identity=identity)
            return {
                "message": "Succesfully logged in",
                "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
            }
        else:
            return {"message": "wrong credentials."}, 401
示例#2
0
    def post(self):
        data = UserRegister.parser.parse_args()

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

        user = UserModel(**data)
        user.save_to_db()
        return {"message": f"User '{username}' created successfully"}, 201
示例#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
示例#4
0
def authenticate(username, password):
    user = UserModel.find_by_username(username)
    if user and safe_str_cmp(user.password, password):
        return user