diff --git a/app/backend/routes/auth.py b/app/backend/routes/auth.py index fdebe6c0faf860b7a429a9d1d48dd577eedff9fa..ed0d1e94d8cf56ca4a9649f0602b3f2863b12758 100644 --- a/app/backend/routes/auth.py +++ b/app/backend/routes/auth.py @@ -1,7 +1,7 @@ from fastapi import APIRouter, Depends, HTTPException from fastapi.security import OAuth2PasswordBearer from backend.models.models import User -from backend.schemas.user import UserCreate , UserLogin +from backend.schemas.user import UserCreate, UserLogin from backend.database import get_session from sqlmodel import Session, select from backend.utils.hashing import hash_password, verify_password @@ -56,4 +56,4 @@ def login(user_data: UserLogin, session: Session = Depends(get_session)): "user_id": user.id, "access_token": access_token, "token_type": "bearer", - } \ No newline at end of file + } diff --git a/app/frontend/components/login.py b/app/frontend/components/login.py index 088e4cad72e63b40ef00dfc4f681a038a16f3f0f..bedad7b7de9b45cd2dce7b3f3332d30427001d23 100644 --- a/app/frontend/components/login.py +++ b/app/frontend/components/login.py @@ -2,11 +2,11 @@ import ttkbootstrap as tb import ttkbootstrap.constants from tkinter import messagebox import requests -import json # Global variable to store the access token access_token = None + def login_frame(parent, switch_func, api_url): frame = tb.Frame(parent) @@ -26,15 +26,11 @@ def login_frame(parent, switch_func, api_url): try: response_data = response.json() if response.status_code == 200: - access_token = response_data.get("access_token") + access_token = response_data["access_token"] print(f"Access Token: {access_token}") # Debugging line - # Save the access token to a file - with open("access_token.json", "w") as token_file: - json.dump({"access_token": access_token}, token_file) - messagebox.showinfo("Login Successful", f"Welcome back, {email}!") - switch_func("create_shop") + switch_func("create_shop", access_token) else: messagebox.showerror( "Login Failed", response_data.get("detail", "Invalid credentials") @@ -62,4 +58,4 @@ def login_frame(parent, switch_func, api_url): command=lambda: switch_func("register"), ).pack() - return frame \ No newline at end of file + return frame diff --git a/app/frontend/components/create_shop.py b/app/frontend/components/shop/create_shop.py similarity index 81% rename from app/frontend/components/create_shop.py rename to app/frontend/components/shop/create_shop.py index 63d0238c5da638f5a8ff3952984acab641a97d8f..b4f818653b5aad140810b7b096821ca6570fc1fc 100644 --- a/app/frontend/components/create_shop.py +++ b/app/frontend/components/shop/create_shop.py @@ -3,11 +3,11 @@ import ttkbootstrap.constants from tkinter import messagebox, filedialog import requests import os -import json -def create_shop_frame(parent, switch_func, api_url): + +def create_shop_frame(parent, switch_func, api_url, token): frame = tb.Frame(parent) - + selected_file_path = [None] def select_file(): @@ -38,17 +38,14 @@ def create_shop_frame(parent, switch_func, api_url): messagebox.showerror("File Error", f"Unable to open file: {str(e)}") return - # Read the access token from the file - try: - with open("access_token.json", "r") as token_file: - token_data = json.load(token_file) - access_token = token_data.get("access_token") - except FileNotFoundError: - messagebox.showerror("Token Error", "Access token not found. Please log in.") + if not token: + messagebox.showerror( + "Token Error", "Access token not found. Please log in." + ) return - headers = {"Authorization": f"Bearer {access_token}"} - print(f"Access Token in create_shop: {access_token}") # Debugging line + headers = {"Authorization": f"Bearer {token}"} + print(f"Access Token in create_shop: {token}") # Debugging line try: response = requests.post(url, data=data, files=files, headers=headers) @@ -94,4 +91,4 @@ def create_shop_frame(parent, switch_func, api_url): command=lambda: switch_func("login"), ).pack(pady=5) - return frame \ No newline at end of file + return frame diff --git a/app/frontend/main.py b/app/frontend/main.py index 5fff2ea686d51b8927ed0d1cc4475fc25cb859ed..938dbbb5ccc6b8f3758518b53df9b00c435ca596 100644 --- a/app/frontend/main.py +++ b/app/frontend/main.py @@ -1,21 +1,28 @@ import ttkbootstrap as tb from components.login import login_frame from components.register import register_frame -from components.create_shop import ( - create_shop_frame, -) # Import the create_shop frame +from components.shop.create_shop import create_shop_frame # Backend API URL API_URL = "http://127.0.0.1:8000" +# Global variable to store the access token +access_token = None + # Function to switch between frames -def switch_frame(frame_name): +def switch_frame(frame_name, token=None): + global access_token + if token: + access_token = token + if frame_name == "login": login.tkraise() elif frame_name == "register": register.tkraise() elif frame_name == "create_shop": + create_shop = create_shop_frame(root, switch_frame, API_URL, access_token) + create_shop.place(relx=0, rely=0.2, relwidth=1, relheight=0.8) create_shop.tkraise() @@ -27,7 +34,7 @@ root.geometry("900x800") # Create Frames inside the main window login = login_frame(root, switch_frame, API_URL) register = register_frame(root, switch_frame, API_URL) -create_shop = create_shop_frame(root, switch_frame, API_URL) +create_shop = create_shop_frame(root, switch_frame, API_URL, access_token) # Place all frames responsively within the window. # Adjust relx, rely, relwidth, and relheight as needed for your layout.