diff --git a/JWT/JWT-Frontend/.idea/.gitignore b/JWT/JWT-Frontend/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/JWT/JWT-Frontend/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/JWT/JWT-Frontend/.idea/JWT-Frontend.iml b/JWT/JWT-Frontend/.idea/JWT-Frontend.iml new file mode 100644 index 0000000..d6ebd48 --- /dev/null +++ b/JWT/JWT-Frontend/.idea/JWT-Frontend.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/JWT/JWT-Frontend/.idea/misc.xml b/JWT/JWT-Frontend/.idea/misc.xml new file mode 100644 index 0000000..6f29fee --- /dev/null +++ b/JWT/JWT-Frontend/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/JWT/JWT-Frontend/.idea/modules.xml b/JWT/JWT-Frontend/.idea/modules.xml new file mode 100644 index 0000000..899d49e --- /dev/null +++ b/JWT/JWT-Frontend/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/JWT/JWT-Frontend/.idea/vcs.xml b/JWT/JWT-Frontend/.idea/vcs.xml new file mode 100644 index 0000000..b2bdec2 --- /dev/null +++ b/JWT/JWT-Frontend/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Python-MySQL-Integration/.idea/.gitignore b/Python-MySQL-Integration/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/Python-MySQL-Integration/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/Python-MySQL-Integration/.idea/Python-MySQL-Integration.iml b/Python-MySQL-Integration/.idea/Python-MySQL-Integration.iml new file mode 100644 index 0000000..9b09070 --- /dev/null +++ b/Python-MySQL-Integration/.idea/Python-MySQL-Integration.iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Python-MySQL-Integration/.idea/dataSources.xml b/Python-MySQL-Integration/.idea/dataSources.xml new file mode 100644 index 0000000..256f380 --- /dev/null +++ b/Python-MySQL-Integration/.idea/dataSources.xml @@ -0,0 +1,12 @@ + + + + + mysql.8 + true + com.mysql.cj.jdbc.Driver + jdbc:mysql://localhost:3306/se10sessions + $ProjectFileDir$ + + + \ No newline at end of file diff --git a/Python-MySQL-Integration/.idea/inspectionProfiles/Project_Default.xml b/Python-MySQL-Integration/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..45f4b3a --- /dev/null +++ b/Python-MySQL-Integration/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,7 @@ + + + + \ No newline at end of file diff --git a/Python-MySQL-Integration/.idea/inspectionProfiles/profiles_settings.xml b/Python-MySQL-Integration/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/Python-MySQL-Integration/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/Python-MySQL-Integration/.idea/misc.xml b/Python-MySQL-Integration/.idea/misc.xml new file mode 100644 index 0000000..756d868 --- /dev/null +++ b/Python-MySQL-Integration/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/Python-MySQL-Integration/.idea/modules.xml b/Python-MySQL-Integration/.idea/modules.xml new file mode 100644 index 0000000..cba1fd1 --- /dev/null +++ b/Python-MySQL-Integration/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/Python-MySQL-Integration/.idea/vcs.xml b/Python-MySQL-Integration/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/Python-MySQL-Integration/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Python-MySQL-Integration/__pycache__/app.cpython-312.pyc b/Python-MySQL-Integration/__pycache__/app.cpython-312.pyc new file mode 100644 index 0000000..307ccd0 Binary files /dev/null and b/Python-MySQL-Integration/__pycache__/app.cpython-312.pyc differ diff --git a/Python-MySQL-Integration/app.py b/Python-MySQL-Integration/app.py new file mode 100644 index 0000000..188a7a2 --- /dev/null +++ b/Python-MySQL-Integration/app.py @@ -0,0 +1,29 @@ +from flask import Flask, render_template, request +from sqlalchemy import null + +from entity.User import User +from model.UserModel import UserModel + +app = Flask(__name__) + + +@app.route('/') +def home(): # put application's code here + return render_template('index.html') + +@app.route('/register', methods=['POST']) +def register(): + name = request.form.get('name') + email = request.form.get('email') + password = request.form.get('password') + + # Save the user details to the database + user = User( + name = name, + email = email, + password = User.hash_password(password) + ) + return UserModel.save_user(user) + +if __name__ == '__main__': + app.run() diff --git a/Python-MySQL-Integration/entity/User.py b/Python-MySQL-Integration/entity/User.py new file mode 100644 index 0000000..7e6efb4 --- /dev/null +++ b/Python-MySQL-Integration/entity/User.py @@ -0,0 +1,34 @@ +import uuid +from sqlalchemy import Column, String, create_engine +from sqlalchemy.ext.declarative import declarative_base +from sqlalchemy.orm import validates, sessionmaker +import bcrypt + +DATABASE_URL = 'mysql+pymysql://root:1234@localhost/se10sessions' +engine = create_engine(DATABASE_URL) +SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) +Base = declarative_base() + +class User(Base): + __tablename__ = 'user' + + # Use String type for the ID to store UUIDs + id = Column(String(36), primary_key=True, default=lambda: str(uuid.uuid4())) + name = Column(String(255), index=True) + email = Column(String(255), unique=True, index=True) + password = Column(String(255)) + + @staticmethod + def hash_password(password): + # Hash password before storing it + if password: + hashed = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()) + return hashed.decode('utf-8') + return password + + def check_password(self, password): + # Verify password + return bcrypt.checkpw(password.encode('utf-8'), self.password.encode('utf-8')) + +# Create tables +Base.metadata.create_all(bind=engine) diff --git a/Python-MySQL-Integration/entity/__pycache__/User.cpython-312.pyc b/Python-MySQL-Integration/entity/__pycache__/User.cpython-312.pyc new file mode 100644 index 0000000..78d832b Binary files /dev/null and b/Python-MySQL-Integration/entity/__pycache__/User.cpython-312.pyc differ diff --git a/Python-MySQL-Integration/model/UserModel.py b/Python-MySQL-Integration/model/UserModel.py new file mode 100644 index 0000000..4e71006 --- /dev/null +++ b/Python-MySQL-Integration/model/UserModel.py @@ -0,0 +1,16 @@ +from entity.User import SessionLocal + + +class UserModel: + @staticmethod + def save_user(user): + db = SessionLocal() + try: + db.add(user) + db.commit() + return "User saved successfully" + except Exception as e: + print(f"Failed to save user. Rolling back. Error: {e}") + db.rollback() + finally: + db.close() diff --git a/Python-MySQL-Integration/model/__pycache__/UserModel.cpython-312.pyc b/Python-MySQL-Integration/model/__pycache__/UserModel.cpython-312.pyc new file mode 100644 index 0000000..a85d3b2 Binary files /dev/null and b/Python-MySQL-Integration/model/__pycache__/UserModel.cpython-312.pyc differ diff --git a/Python-MySQL-Integration/static/js/index.js b/Python-MySQL-Integration/static/js/index.js new file mode 100644 index 0000000..cf578f5 --- /dev/null +++ b/Python-MySQL-Integration/static/js/index.js @@ -0,0 +1,29 @@ +function userRegistration() { + // Get form values + const name = document.getElementById('name').value; + const email = document.getElementById('email').value; + const password = document.getElementById('password').value; + + // Prepare the data to be sent + const data = new URLSearchParams(); + data.append('name', name); + data.append('email', email); + data.append('password', password); + + // Send POST request + fetch('http://127.0.0.1:5000/register', { + method: 'POST', + headers: { + 'Content-Type': 'application/x-www-form-urlencoded', + }, + body: data.toString(), + }) + .then(response => response.text()) + .then(result => { + alert(result); + }) + .catch(error => { + console.error('Error:', error); + alert('Failed to register user'); + }); +} diff --git a/Python-MySQL-Integration/templates/index.html b/Python-MySQL-Integration/templates/index.html new file mode 100644 index 0000000..409b7ab --- /dev/null +++ b/Python-MySQL-Integration/templates/index.html @@ -0,0 +1,67 @@ + + + + + + + Python-MySQL Intergration + + + + + + +
+

User Registration

+
+
+ + +
+
+ + +
+
+ + +
+ + +
+
+ + + + + +