Example #1
0
def login():
    """ User login """
    user_data = request.json
    schema = UserSchema()
    try:
        user = schema.load(user_data).data
    except ValidationError as err:
        response.status = 400
        return schema.get_validation_errors(err)

    try:
        db_user = User.select().where(User.username == user.username).get()
    except User.DoesNotExist as err:
        return {
            "code": "user_does_not_exist",
            "description": "User does not exist"
        }

    if fn.check_password(user.password, db_user.password) == True:
        return get_token(user.username)
Example #2
0
def signup():
    """ Create User """
    user_data = request.json
    schema = UserSchema()
    try:
        user = schema.load(user_data).data
    except ValidationError as err:
        response.status = 400
        return schema.get_validation_errors(err)

    try:
        query = User.insert(username=user.username,
                            password=fn.make_password(
                                user.password)).execute()
    except IntegrityError as err:
        return {
            "code": "username_already_exist",
            "description": "User does not exist"
        }
    result = schema.dump(user).data
    return HTTPResponse(result, status=201)