|
|
@ -85,7 +85,8 @@ def register(): |
|
|
|
quizzes=[], |
|
|
|
quizzes=[], |
|
|
|
quiz_attempts=[], |
|
|
|
quiz_attempts=[], |
|
|
|
chats=[], |
|
|
|
chats=[], |
|
|
|
notifications=[] |
|
|
|
notifications=[], |
|
|
|
|
|
|
|
publications=[] |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
# Save the user to the database |
|
|
|
# Save the user to the database |
|
|
@ -101,76 +102,6 @@ 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 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@profile.route('/login', methods=['POST']) |
|
|
|
|
|
|
|
def login(): |
|
|
|
|
|
|
|
""" |
|
|
|
|
|
|
|
Handle user login. |
|
|
|
|
|
|
|
""" |
|
|
|
|
|
|
|
data = request.form # Expecting JSON body |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Extract credentials from request |
|
|
|
|
|
|
|
# username = data.get('username') |
|
|
|
|
|
|
|
email = data.get('email') |
|
|
|
|
|
|
|
password = data.get('password') |
|
|
|
|
|
|
|
user_agent = request.headers.get('User-Agent', 'Unknown') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 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 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#make a get request to get json on hello word |
|
|
|
#make a get request to get json on hello word |
|
|
|
@profile.route('/hello') |
|
|
|
@profile.route('/hello') |
|
|
|
@auth_required() |
|
|
|
@auth_required() |
|
|
|