|
|
@ -3,8 +3,8 @@ from flask import Blueprint, request, jsonify, current_app |
|
|
|
from werkzeug.utils import secure_filename |
|
|
|
from werkzeug.utils import secure_filename |
|
|
|
from datetime import datetime |
|
|
|
from datetime import datetime |
|
|
|
from db.model import db |
|
|
|
from db.model import db |
|
|
|
from db.model import User, UserRole # Adjust based on your model's location |
|
|
|
from db.model import User, UserRole, Session # Adjust based on your model's location |
|
|
|
from werkzeug.security import generate_password_hash |
|
|
|
from werkzeug.security import generate_password_hash,check_password_hash |
|
|
|
import uuid |
|
|
|
import uuid |
|
|
|
import os |
|
|
|
import os |
|
|
|
from config import * |
|
|
|
from config import * |
|
|
@ -99,19 +99,73 @@ def register(): |
|
|
|
db.session.rollback() |
|
|
|
db.session.rollback() |
|
|
|
return jsonify({"error": "Registration failed, please try again later."}), 500 |
|
|
|
return jsonify({"error": "Registration failed, please try again later."}), 500 |
|
|
|
|
|
|
|
|
|
|
|
# TODO: Implement laters |
|
|
|
|
|
|
|
@profile.route('/update', methhods=['UPDATE', 'DELETE']) |
|
|
|
@profile.route('/login', methods=['POST']) |
|
|
|
def update(): |
|
|
|
def login(): |
|
|
|
if request.method == 'DELETE': |
|
|
|
""" |
|
|
|
pass |
|
|
|
Handle user login. |
|
|
|
if request.method == 'UPDATE': |
|
|
|
""" |
|
|
|
pass |
|
|
|
data = request.form # Expecting JSON body |
|
|
|
|
|
|
|
|
|
|
|
@profile.route('/me') |
|
|
|
# Extract credentials from request |
|
|
|
def my_profile(): |
|
|
|
# username = data.get('username') |
|
|
|
pass |
|
|
|
email = data.get('email') |
|
|
|
|
|
|
|
password = data.get('password') |
|
|
|
@profile.route('/info/<str:uuid>') |
|
|
|
user_agent = request.headers.get('User-Agent', 'Unknown') |
|
|
|
def profile_info(user_uuid): |
|
|
|
|
|
|
|
return user_uuid |
|
|
|
# Validate required fields |
|
|
|
|
|
|
|
if not email or not password: |
|
|
|
|
|
|
|
return jsonify({"error": "email and password are required"}), 400 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Find the user by username |
|
|
|
|
|
|
|
# user = User.query.filter_by(username=username).first() |
|
|
|
|
|
|
|
user = User.query.filter_by(email=email).first() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if not user: |
|
|
|
|
|
|
|
return jsonify({"error": "Invalid email or password"}), 401 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Verify the password |
|
|
|
|
|
|
|
if not check_password_hash(user.hash_password, password): |
|
|
|
|
|
|
|
return jsonify({"error": "Invalid email or password"}), 401 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Create a new session |
|
|
|
|
|
|
|
session_key = str(uuid.uuid4()) # Generate a unique session key |
|
|
|
|
|
|
|
new_session = Session( |
|
|
|
|
|
|
|
userID=user.id, |
|
|
|
|
|
|
|
user=user, # Pass the user object here |
|
|
|
|
|
|
|
key=session_key, |
|
|
|
|
|
|
|
ua=user_agent, |
|
|
|
|
|
|
|
creationDate=datetime.utcnow(), |
|
|
|
|
|
|
|
lastUsed=datetime.utcnow(), |
|
|
|
|
|
|
|
isValid=True |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try: |
|
|
|
|
|
|
|
db.session.add(new_session) |
|
|
|
|
|
|
|
db.session.commit() |
|
|
|
|
|
|
|
return jsonify({ |
|
|
|
|
|
|
|
"message": "Login successful", |
|
|
|
|
|
|
|
"session_key": session_key, |
|
|
|
|
|
|
|
"user_id": str(user.id) |
|
|
|
|
|
|
|
}), 200 |
|
|
|
|
|
|
|
except Exception as e: |
|
|
|
|
|
|
|
db.session.rollback() |
|
|
|
|
|
|
|
return jsonify({"error": "Login failed, please try again later."}), 500 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#Implement laters |
|
|
|
|
|
|
|
# @profile.route('/update', methhods=['UPDATE', 'DELETE']) |
|
|
|
|
|
|
|
# def update(): |
|
|
|
|
|
|
|
# if request.method == 'DELETE': |
|
|
|
|
|
|
|
# pass |
|
|
|
|
|
|
|
# if request.method == 'UPDATE': |
|
|
|
|
|
|
|
# pass |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# @profile.route('/me') |
|
|
|
|
|
|
|
# def my_profile(): |
|
|
|
|
|
|
|
# pass |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# @profile.route('/info/<str:uuid>') |
|
|
|
|
|
|
|
# def profile_info(user_uuid): |
|
|
|
|
|
|
|
# return user_uuid |
|
|
|
|
|
|
|
|
|
|
|