from flask import jsonify
from app import db, log
from app.api import api_bp as bp
from app.auth.logic import basic_auth, token_auth


@bp.route("/tokens", methods=["POST"])
@basic_auth.login_required
def get_token():
    "renvoie un jeton jwt pour l'utilisateur courant"
    token = basic_auth.current_user().get_token()
    log(f"API: giving token to {basic_auth.current_user()}")
    db.session.commit()
    return jsonify({"token": token})


@bp.route("/tokens", methods=["DELETE"])
@token_auth.login_required
def revoke_token():
    "révoque le jeton de l'utilisateur courant"
    token_auth.current_user().revoke_token()
    db.session.commit()
    return "", 204