from flask import Blueprint, jsonify, request
from flask_jwt_extended import (
    create_access_token, jwt_refresh_token_required, create_refresh_token,
    get_jwt_identity, set_access_cookies, set_refresh_cookies, unset_jwt_cookies,
)

from models import User
from common import hash_password, verify_password



token = Blueprint('token', __name__)



@token.route('/token/auth', methods=['POST'])
def login():
    username = request.json.get('username', None)
    password = request.json.get('password', None)
    user = User.objects(email=username)
    if not user or not verify_password(user.first().password, password):
        return jsonify({'login': False}), 401

    # Create the tokens we will be sending back to the user
    access_token = create_access_token(identity=username)
    refresh_token = create_refresh_token(identity=username)

    # Set the JWT cookies in the response
    resp = jsonify({'login': True})
    set_access_cookies(resp, access_token)
    set_refresh_cookies(resp, refresh_token)
    return resp, 200


@token.route('/token/refresh', methods=['POST'])
@jwt_refresh_token_required
def refresh():
    # Create the new access token
    current_user = get_jwt_identity()
    access_token = create_access_token(identity=current_user)

    # Set the JWT access cookie in the response
    resp = jsonify({'refresh': True})
    set_access_cookies(resp, access_token)
    return resp, 200


@token.route('/token/remove', methods=['POST'])
def logout():
    resp = jsonify({'logout': True})
    unset_jwt_cookies(resp)
    return resp, 200
from flask import Blueprint, jsonify, request
from flask_jwt_extended import (
    create_access_token, jwt_refresh_token_required, create_refresh_token,
    get_jwt_identity, set_access_cookies, set_refresh_cookies, unset_jwt_cookies,
)

from models import User
from common import hash_password, verify_password



token = Blueprint('token', __name__)



@token.route('/token/auth', methods=['POST'])
def login():
    username = request.json.get('username', None)
    password = request.json.get('password', None)
    user = User.objects(email=username)
    if not user or not verify_password(user.first().password, password):
        return jsonify({'login': False}), 401

    # Create the tokens we will be sending back to the user
    access_token = create_access_token(identity=username)
    refresh_token = create_refresh_token(identity=username)

    # Set the JWT cookies in the response
    resp = jsonify({'login': True})
    set_access_cookies(resp, access_token)
    set_refresh_cookies(resp, refresh_token)
    return resp, 200


@token.route('/token/refresh', methods=['POST'])
@jwt_refresh_token_required
def refresh():
    # Create the new access token
    current_user = get_jwt_identity()
    access_token = create_access_token(identity=current_user)

    # Set the JWT access cookie in the response
    resp = jsonify({'refresh': True})
    set_access_cookies(resp, access_token)
    return resp, 200


@token.route('/token/remove', methods=['POST'])
def logout():
    resp = jsonify({'logout': True})
    unset_jwt_cookies(resp)
    return resp, 200
# Create the tokens we will be sending back to the user # Set the JWT cookies in the response # Create the new access token # Set the JWT access cookie in the response