from cryptography.fernet import Fernet
import os
import httpx

KEY_FILE = ".key"
REMEMBER_FILE = ".auth_data"

def generate_key():
    if not os.path.exists(KEY_FILE):
        key = Fernet.generate_key()
        with open(KEY_FILE, "wb") as f:
            f.write(key)


async def load_fernet():
    with open(KEY_FILE, "rb") as f:
        key = f.read()
    return Fernet(key)


async def login(name: str, password: str):
    async with httpx.AsyncClient() as client:
        r = await client.post(f'http://127.0.0.1:7535/login?name={name}&password={password}')

        if r.is_success:
            return True
        else:
            return False