From 241ab8529b455380f2beefc3999672b1447550a4 Mon Sep 17 00:00:00 2001 From: Lucas Mathews Date: Sun, 26 May 2024 13:14:46 +0200 Subject: [PATCH] Added login app page added, dashboard app page added with logout --- api.py | 1 - application/app.ini | 9 +++++- application/connection.py | 42 ++++++++++++++++++++++++++-- application/dashboard.py | 37 +++++++++++++++++++++++++ application/login.py | 49 +++++++++++++++++++++++++++------ application/session_cookie.json | 1 + 6 files changed, 127 insertions(+), 12 deletions(-) create mode 100644 application/dashboard.py create mode 100644 application/session_cookie.json diff --git a/api.py b/api.py index d858f90..563e040 100644 --- a/api.py +++ b/api.py @@ -8,7 +8,6 @@ import connexion # Imports connexion module from config import CONFIG # Imports the configuration file from manager import * # Imports the Manager file that contains the functions for the API -from flask import Flask, session, jsonify, request # Imports the Flask module from flask_session import Session # Imports the session module ################# diff --git a/application/app.ini b/application/app.ini index 1474943..f0864e5 100644 --- a/application/app.ini +++ b/application/app.ini @@ -1,3 +1,10 @@ [server] ip=0.0.0.0 -port=81 \ No newline at end of file +port=81 +url=http://127.0.0.1:81 + +[preferences] +dark_theme=system +# Modes: system, light, dark +theme=dark-blue +# Themes: blue, dark-blue, green diff --git a/application/connection.py b/application/connection.py index db1afbf..a0845bb 100644 --- a/application/connection.py +++ b/application/connection.py @@ -2,7 +2,45 @@ # Banking System App Connection file import requests +from requests.models import PreparedRequest, Response from config import CONFIG +import json -def get_connection(): - return requests.get(CONFIG["api"]["url"]) # Returns the connection to the API \ No newline at end of file + +def authenticate_client(client_id, client_password): + try: + # Send a POST request to the /Client/Login endpoint with the client_id and password + response = requests.post(CONFIG["server"]["url"] + "/Client/Login", params={'client_id': client_id, 'password': client_password}) + + # Return the response from the API + return response + except requests.exceptions.RequestException as e: + # If a RequestException is raised, print the exception message + print(f"RequestException: {e}") + + # Create a new Response object with a status code of 500 and the error message in the JSON body + response = Response() + response.status_code = 500 + response._content = b'{"success": false, "message": "Could not connect to the server. Please try again later."}' + return response + +def logout_client(): + try: + # Load the session cookie from the file + with open('application\\session_cookie.json', 'r') as f: + cookies = json.load(f) + + # Send a POST request to the /Client/Logout endpoint + response = requests.post(CONFIG["server"]["url"] + "/Client/Logout", cookies=cookies) + + # Return the response from the API + return response + except requests.exceptions.RequestException as e: + # If a RequestException is raised, print the exception message + print(f"RequestException: {e}") + + # Create a new Response object with a status code of 500 and the error message in the JSON body + response = Response() + response.status_code = 500 + response._content = b'{"success": false, "message": "Could not connect to the server. Please try again later."}' + return response \ No newline at end of file diff --git a/application/dashboard.py b/application/dashboard.py new file mode 100644 index 0000000..61fcf55 --- /dev/null +++ b/application/dashboard.py @@ -0,0 +1,37 @@ +# Lucas Mathews - Fontys Student ID: 5023572 +# Banking System Dashboard Page + +import tkinter as tk +from tkinter import messagebox +import customtkinter + + +from connection import logout_client + +def logout(): + response = logout_client() # Call the logout_client function + json_response = response.json() # Convert the response content to JSON + if json_response['success'] == True: + messagebox.showinfo("Logout", "You have been logged out.") + root.destroy() + else: + messagebox.showerror("Logout failed", json_response['message']) + +# Create the main window +root = customtkinter.CTk() + +# Set the window title, icon, and size +root.title("Luxbank Dashboard") +root.iconbitmap("application/luxbank.ico") +root.geometry("800x600") + +# Create a label with a welcome message +welcome_label = customtkinter.CTkLabel(root, text="Welcome to the Luxbank Dashboard!", font=("Helvetica", 24)) +welcome_label.pack(pady=20) + +# Create a Logout button +logout_button = customtkinter.CTkButton(root, text="Logout", command=logout) +logout_button.pack(pady=15) + +# Start the main loop +root.mainloop() \ No newline at end of file diff --git a/application/login.py b/application/login.py index 3e07133..9ce315f 100644 --- a/application/login.py +++ b/application/login.py @@ -1,12 +1,41 @@ # Lucas Mathews - Fontys Student ID: 5023572 # Banking System App Login Page -from tkinter import * +from tkinter import messagebox import customtkinter +import os +import json +from connection import * + +################# +### Functions ### +################# + +def login(): + client_id = entry_username.get() + client_password = entry_password.get() + try: + response = authenticate_client(client_id, client_password) # Authenticate the client + json_response = response.json() # Convert the response content to JSON + if json_response["success"] == True: # If the authentication is successful, open the dashboard + # Save the session cookie to a file + with open('application\\session_cookie.json', 'w') as f: + json.dump(response.cookies.get_dict(), f) + root.destroy() + os.system("python application\\dashboard.py") + else: + messagebox.showerror("Login failed", json_response["message"]) # If the authentication fails, show an error message + except requests.exceptions.RequestException as e: + # If a RequestException is raised, show an error message that includes the exception message + messagebox.showerror("Login failed", "Could not connect to the server. Please try again later. Error: " + str(e)) -customtkinter.set_appearance_mode("System") # Modes: system (default), light, dark -customtkinter.set_default_color_theme("dark-blue") # Themes: blue (default), dark-blue, green +############## +### Layout ### +############## + +customtkinter.set_appearance_mode((CONFIG["preferences"]["dark_theme"])) # Modes: system (default), light, dark +customtkinter.set_default_color_theme((CONFIG["preferences"]["theme"])) # Themes: blue (default), dark-blue, green root = customtkinter.CTk() @@ -17,12 +46,16 @@ root.geometry("400x300") label = customtkinter.CTkLabel(root, text="Luxbank", font=("Helvetica", 24)) label.pack(pady=20) -username = customtkinter.CTkEntry(root, placeholder_text="Client ID") -username.pack(pady=20) -password = customtkinter.CTkEntry(root, placeholder_text="Password", show="*") -password.pack(pady=10) +entry_username = customtkinter.CTkEntry(root, placeholder_text="Client ID") +entry_username.pack(pady=20) +entry_password = customtkinter.CTkEntry(root, placeholder_text="Password", show="*") +entry_password.pack(pady=10) -login_button= customtkinter.CTkButton(root, text="Login", command=lambda: print("Login")) +login_button= customtkinter.CTkButton(root, text="Login", command=login) login_button.pack(pady=15) +########### +### Run ### +########### + root.mainloop() \ No newline at end of file diff --git a/application/session_cookie.json b/application/session_cookie.json new file mode 100644 index 0000000..7df7fc8 --- /dev/null +++ b/application/session_cookie.json @@ -0,0 +1 @@ +{"session": "rZS5tQOS4nXGJu-WXEg6_Ls5q8njy3GNiZ3s8N3YHEA"} \ No newline at end of file