Skip to content
Snippets Groups Projects
Commit afdec9a2 authored by duyanhehe's avatar duyanhehe
Browse files

fix request json

parent 195c340b
Branches
No related tags found
No related merge requests found
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
}
......@@ -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
......@@ -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
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.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment