Ejemplo n.º 1
0
def login():
    data = request.get_json()
    username = data.get("username", None)
    password = data.get("password", None)
    if not username or not password:
        return {"message": "Please enter username and password"}

    user = User.query.filter_by(username=username).first()

    if user and user.password == password:
        access_token = create_access_token(identity=user.id,
                                           expires_delta=ACCESS_EXPIRES)
        refresh_token = create_refresh_token(identity=user.id,
                                             expires_delta=REFRESH_EXPIRES)
        # print(get_datetime_now_s())
        # decoded_token = decode_token(access_token)
        # print(decoded_token['exp'])
        TokenBlacklist.add_token_to_database(access_token, user.id)
        TokenBlacklist.add_token_to_database(refresh_token, user.id)
        return {
            "access_token": access_token,
            "refresh_token": refresh_token,
            "user": user_schema.dump(user)
        }
    else:
        return {"message": "Wrong username or password"}
Ejemplo n.º 2
0
def delete_user(_id):
    user = User.get_by_id(_id)
    if not user:
        return {"message": "User not found"}

    db.session.delete(user)
    db.session.commit()
    user = user_schema.dump(user)
    return {"message": "Success", "user": user}
Ejemplo n.º 3
0
def update_user(_id):
    user = User.get_by_id(_id)
    if not user:
        return {"message": "User not found"}
    data = request.get_json()

    password = data.get("password", None)
    user.password = password

    db.session.commit()
    user = user_schema.dump(user)
    return {"message": "Success", "user": user}
Ejemplo n.º 4
0
def create_user():
    data = request.get_json()
    username = data.get("username", None)
    password = data.get("password", None)
    if not username or not password:
        return {"message": "Please enter username and password"}

    user = User.query.filter_by(username=username).first()

    if user:
        return {"message": "Username is existed"}
    else:
        _id = str(uuid.uuid1())
        user = User(id=_id, username=username, password=password)
        db.session.add(user)
        db.session.commit()
        user = user_schema.dump(user)
        return {"message": "Success", "user": user}
Ejemplo n.º 5
0
def get_user_by_id(_id):
    user = User.get_by_id(_id)
    if not user:
        return {"message": "User not found"}
    user = user_schema.dump(user)
    return {"user": user}