From 1b5eb57de012d0e4d95cb8d9da960d8e39392a66 Mon Sep 17 00:00:00 2001 From: Christian Mundreanu <155108015+Mundreanuc223@users.noreply.github.com> Date: Mon, 14 Oct 2024 12:44:30 -0400 Subject: [PATCH 1/3] Create .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1cc4572 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea/workspace.xml From aa439e84b304090f9a11ce1a93142895f8c7f11b Mon Sep 17 00:00:00 2001 From: Christian Mundreanu <155108015+Mundreanuc223@users.noreply.github.com> Date: Mon, 14 Oct 2024 12:49:13 -0400 Subject: [PATCH 2/3] Update .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 1cc4572..5f99e9b 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .idea/workspace.xml +.idea From 378c91eb84e5163a7833b17ff1d4d3f119679c34 Mon Sep 17 00:00:00 2001 From: yoaaronw <117215469+yoaaronw@users.noreply.github.com> Date: Tue, 15 Oct 2024 19:19:32 -0400 Subject: [PATCH 3/3] Added backend implementation of login and registration forms --- vault/src/Components/LoginForm/LoginForm.css | 6 ++ vault/src/Components/LoginForm/LoginForm.jsx | 43 +++++++++++-- vault/src/backend/README.md | 2 +- vault/src/backend/app.py | 62 ++++++++++++++++++- vault/src/backend/vault_database.db | Bin 32768 -> 45056 bytes 5 files changed, 105 insertions(+), 8 deletions(-) diff --git a/vault/src/Components/LoginForm/LoginForm.css b/vault/src/Components/LoginForm/LoginForm.css index a12de62..574f972 100644 --- a/vault/src/Components/LoginForm/LoginForm.css +++ b/vault/src/Components/LoginForm/LoginForm.css @@ -85,4 +85,10 @@ .register-link p a:hover{ text-decoration: underline; +} + +.wrapper .error-message{ + font-size: 15px; + text-align: center; + margin-top: 20px; } \ No newline at end of file diff --git a/vault/src/Components/LoginForm/LoginForm.jsx b/vault/src/Components/LoginForm/LoginForm.jsx index 73749b0..63a1ded 100644 --- a/vault/src/Components/LoginForm/LoginForm.jsx +++ b/vault/src/Components/LoginForm/LoginForm.jsx @@ -1,18 +1,50 @@ -import React from 'react'; +import React, { useState } from 'react'; import './LoginForm.css'; import { FaUser } from "react-icons/fa"; import { FaLock } from "react-icons/fa"; -const LoginForm = () =>{ + +const LoginForm = () => { + // State to store the username and password + const [username, setUsername] = useState(''); + const [password, setPassword] = useState(''); + const [errorMessage, setErrorMessage] = useState(''); + + // Function to handle form submission + const handleSubmit = async (e) => { + e.preventDefault(); // Prevents page reload or default form submission + try { + // Send a POST request to backend + const response = await fetch('http://127.0.0.1:5000/login', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ username, password }), // Send username and password + }); + + const data = await response.json(); + + if (response.ok) { + console.log('Login successful:', data.message); + } else { + setErrorMessage(data.message); // Show error message to the user + } + } catch (error) { + console.error('Error during login:', error); + setErrorMessage('Something went wrong. Please try again.'); + } + }; + return (
-
+

Login

- + setUsername(e.target.value)} />
- + setPassword(e.target.value)} />
@@ -23,6 +55,7 @@ const LoginForm = () =>{

Need an account?Register

+ {errorMessage &&

{errorMessage}

}
); diff --git a/vault/src/backend/README.md b/vault/src/backend/README.md index 877478a..1e3ce30 100644 --- a/vault/src/backend/README.md +++ b/vault/src/backend/README.md @@ -1,6 +1,6 @@ # Backend Guide -## 1. Ensure you have SQLite and Flask downloaded +## 1. Ensure you have SQLite, Flask, and Flask-cors downloaded ### Table Format Example: diff --git a/vault/src/backend/app.py b/vault/src/backend/app.py index 5cb6da2..887b838 100644 --- a/vault/src/backend/app.py +++ b/vault/src/backend/app.py @@ -1,8 +1,10 @@ from flask import Flask, jsonify, request import sqlite3 from datetime import datetime +from flask_cors import CORS app = Flask(__name__) +CORS(app) # Creates the database with multiple tables def init_db(): @@ -14,7 +16,7 @@ def init_db(): CREATE TABLE IF NOT EXISTS users ( user_id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT UNIQUE NOT NULL, - email TEXT UNIQUE NOT NULL, + email TEXT UNIQUE, password TEXT NOT NULL, profile_pic TEXT, bio TEXT, @@ -52,9 +54,21 @@ def init_db(): connection.close() def get_db_connection(): - connection = sqlite3.connect('vault_database.db') + connection = sqlite3.connect('vault_database.db', timeout=10.0) return connection +def insert_new_user(username, password): + conn = get_db_connection() + cursor = conn.cursor() + + cursor.execute(''' + INSERT INTO users (username, password) VALUES (?, ?) + ''', (username, password)) + + conn.commit() + conn.close() + + # Initializes the database (will only need to be run when first creating the DB and anytime we add new fields/schemas to the tables @app.route('/init', methods=['GET']) def initialize_database(): @@ -71,6 +85,50 @@ def get_users(): conn.close() return jsonify(users) +# Login handling +@app.route('/login', methods=['POST']) +def login(): + data = request.json + username = data['username'] + password = data['password'] + + conn = get_db_connection() + cursor = conn.cursor() + + # Checking if username is in database and if password is correct + cursor.execute("SELECT username, password FROM users WHERE username = ? AND password = ?", (username, password)) + user = cursor.fetchone() + conn.close() + + if user: + return jsonify({"status": "success", "message": "Login successful!"}), 200 + else: + return jsonify({"status": "failure", "message": "Username or password incorrect."}), 400 + +# Registration handling +@app.route('/register', methods=['POST']) +def register(): + data = request.json + username = data['username'] + password = data['password'] + + conn = get_db_connection() + cursor = conn.cursor() + + cursor.execute("SELECT * FROM users WHERE username = ?", (username,)) + existing_user = cursor.fetchone() + + if existing_user: + # Username is already in database/is taken + conn.close() + return jsonify({"error": "Username already taken"}), 409 + + # Adding username and password into the database + cursor.execute("INSERT INTO users (username, password) VALUES (?, ?)", (username, password)) + conn.commit() + conn.close() + return jsonify({"status": "success", "message": "Account created!"}), 201 + if __name__ == '__main__': init_db() app.run(debug=True) diff --git a/vault/src/backend/vault_database.db b/vault/src/backend/vault_database.db index 4197e1804af5e3d0151f69f39b5bdcc282faab6b..b64ec474fe04d26d37f4e1375c57e24bcdc96df0 100644 GIT binary patch delta 1052 zcmcIi!E4h{9DXmaY|TrPw;>Wnt%<#C#WqdSPF)n~lpsRsm}|jX$+i>&wVml|!OQB{ zAE4$x*ngnKqo5v!w_QaT?BHd16URIV`jW2cobAzsU-J0g@Au{VzL##7=^ipK;?pqz z07olA3(=Z*na*8_0p-~fp8o+RF#`NSM{jP=Mkhr{f;)*# zYqf5-TejP(HtbHgVDCOK7t5wxUc6H@NMT;K3ZBT%cV-ONO8AWbLw_=!&wWq(s(8A zp<1eX_*stniJB4wDc0L3a}f6wyoY?>AH1LZJ0^uGoV?K=;ITgrY z089QyO9D>uR3k&GfV?5q-t^Q8*;-~;AY~H(TF|>J)dIgHu6# z530YVU3uzNA%Ypw1>_^?k^=41$<}Sef;b5LHXQe<)v!k7rRNNF-pDVU$!h=={3Gbk zhpxsl==ynvB}3P5`H}Z2f+)s}=M7CgH||4c6D9}L)IQNT6o;p7(3rgYV<&+8qDS+c Rj(sIU1ObTr2ZIn`{{Zsw5=#I8 delta 226 zcmZp8z|_#dG(lRBnSp_U1BhXOd7_T7G&6%({h1>l03n#^qCw&Ih|amw);l@_NK6|-nE!9=(X!6INUZ2l9^qOhre OMPM^az#sWV2?_w=B{UrX