Beispiel #1
0
def generate_token(auth_payload):
    """
        API: POST /authenticate
    """
    username = auth_payload['username']
    password = auth_payload['password']

    user = UserModel.query.filter_by(
        username=username,
        password=lazy_hashing(password)) \
        .first()

    if user is not None:
        timestamp = _current_timestamp()
        payload = {
            "iss": JWT_ISSUER,
            "iat": int(timestamp),
            "exp": int(timestamp + JWT_LIFETIME_SECONDS),
            "user_id": user.id,
            "username": user.username,
            "role": user.role,
            "fullname": user.fullname
        }
        # return { "token": jwt.encode(payload, JWT_SECRET, algorithm=JWT_ALGORITHM) }
        return {
            "token": jwt.encode(payload, JWT_SECRET, algorithm=JWT_ALGORITHM),
            "fullname": user.fullname
        }
    else:
        return Response('The username or password is invaild!', status=400)
def create(user_data):
    try:
        schema = UserSchema()
        new_item = schema.make(user_data)
        new_item.password = lazy_hashing(user_data['password'])
        db.session.add(new_item)
        # commit
        db.session.commit()
        # dump data
        result = schema.dump(new_item, many=False)
        print("items-------------", result)
        return HTTP_201_CREATED, result
    except Exception as error:
        print("Error----------------", error)
        db.session.rollback()
        return HTTP_400_BAD_REQUEST, None
def update(user_id, user_data):
    try:
        schema = UserSchema()
        item = UserModel.query.filter_by(id=user_id).first()
        print(item)
        if item is None:
            return HTTP_404_NOT_FOUND, None
        if user_data.get('password') is not None:
            user_data['password'] = lazy_hashing(user_data['password'])
        new_item = UserModel.query.filter_by(id=user_id).update(user_data)
        db.session.commit()
        item = UserModel.query.filter_by(id=user_id).first()
        result = schema.dump(item, many=False)
        return HTTP_201_CREATED, result
    except Exception as error:
        print("Error-------------", error)
        db.session.rollback()
        return HTTP_400_BAD_REQUEST, None
Beispiel #4
0
def login(auth_payload):
    """
            API: POST /authenticate
        """
    username = auth_payload['username']
    password = auth_payload['password']

    user = UserModel.query.filter_by(
        username=username,
        password=lazy_hashing(password)) \
        .first()
    print("User::User==============", user)
    if user is not None:
        schema = UserSchema()
        # dump data
        result = schema.dump(user, many=False)
        return send_response(code=200, data=result)
    else:
        return send_response(code=400, message="Sai tài khoản hoặc mật khẩu")
def register(user_data):
    try:
        schema = UserSchema()
        new_item = schema.make(user_data)
        new_item.password = lazy_hashing(user_data['password'])
        new_item.role = 1
        new_item.danger = False
        new_item.point = 0
        new_item.status = 1
        new_item.member_id = 0
        db.session.add(new_item)
        # commit
        db.session.commit()
        # dump data
        result = schema.dump(new_item, many=False)
        print("items-------------", result)
        return HTTP_201_CREATED, result
    except Exception as error:
        print("Error----------------", error)
        db.session.rollback()
        return HTTP_400_BAD_REQUEST, None