"""Auth API endpoints."""


from flask import Blueprint, g, jsonify
from flask_httpauth import HTTPBasicAuth, HTTPTokenAuth

from bookmarks import csrf
from bookmarks.users.models import User


auth_api = Blueprint('api', __name__)

basic_auth = HTTPBasicAuth()
token_auth = HTTPTokenAuth(scheme='Token')


@basic_auth.verify_password
def verify_password(email, password):
    """Verify user by email address and password."""
    g.user = User.query.filter_by(email=email).scalar()
    return g.user is not None and g.user.is_password_correct(password)


@token_auth.verify_token
def verify_token(token):
    """Verify user by token."""
    data = User.verify_auth_token(token)
    return data and User.query.get(data['id']) is not None


@auth_api.route('/api/auth/request-token', methods=['POST'])
@csrf.exempt
@basic_auth.login_required
def request_token():
    """Return new token for the user."""
    return jsonify({'token': g.user.generate_auth_token()}), 200

@@ -548,16 +548,17 @@
 word)%0A%0A%0A
+%0A
 @token_a
@@ -680,23 +680,14 @@
     
-return data and
+user =
  Use
@@ -711,25 +711,103 @@
 'id'%5D) i
-s not Non
+f data else None%0A    if user:%0A        g.user = user%0A        return True%0A    return Fals
 e%0A%0A%0A@aut

save user to g after verifying his token in a API request