Fix: User login on production.

This commit is contained in:
2024-11-15 16:02:54 +00:00
parent f837672b37
commit a7e344a2e7
3 changed files with 24 additions and 0 deletions

View File

@@ -31,6 +31,12 @@ class Config:
# MySQL # MySQL
SQLALCHEMY_DATABASE_URI = os.getenv('SQLALCHEMY_DATABASE_URI') SQLALCHEMY_DATABASE_URI = os.getenv('SQLALCHEMY_DATABASE_URI')
SQLALCHEMY_TRACK_MODIFICATIONS = False SQLALCHEMY_TRACK_MODIFICATIONS = False
SQLALCHEMY_ENGINE_OPTIONS = {
'pool_size': 10,
'pool_recycle': 280,
'pool_pre_ping': True,
'pool_timeout': 30,
}
# Auth0 # Auth0
SESSION_COOKIE_SECURE = True SESSION_COOKIE_SECURE = True
SESSION_COOKIE_HTTPONLY = True SESSION_COOKIE_HTTPONLY = True

View File

@@ -21,6 +21,7 @@ import lib.argument_validation as av
# external # external
from flask import Flask, render_template, jsonify, request, render_template_string, send_from_directory, redirect, url_for, session, Blueprint, current_app from flask import Flask, render_template, jsonify, request, render_template_string, send_from_directory, redirect, url_for, session, Blueprint, current_app
from flask_sqlalchemy import SQLAlchemy from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import exc
from flask_wtf.csrf import generate_csrf from flask_wtf.csrf import generate_csrf
from werkzeug.exceptions import BadRequest from werkzeug.exceptions import BadRequest
from extensions import oauth # db, from extensions import oauth # db,
@@ -34,6 +35,21 @@ db = SQLAlchemy()
routes_user = Blueprint('routes_user', __name__) routes_user = Blueprint('routes_user', __name__)
def handle_db_disconnect(f):
@wraps(f)
def decorated_function(*args, **kwargs):
try:
return f(*args, **kwargs)
except exc.OperationalError as e:
if "MySQL server has gone away" in str(e):
# Close the session and create a new connection
db.session.remove()
db.session.rollback()
# Retry the operation
return f(*args, **kwargs)
raise
return decorated_function
# User authentication # User authentication
@routes_user.route("/login", methods=['POST', 'OPTIONS']) @routes_user.route("/login", methods=['POST', 'OPTIONS'])
def login(): def login():
@@ -109,6 +125,7 @@ def login():
@routes_user.route("/login_callback") # <path:subpath>/<code> @routes_user.route("/login_callback") # <path:subpath>/<code>
@handle_db_disconnect
def login_callback(): def login_callback():
try: try:
error_state = request.args.get(Model_View_User.FLAG_ERROR_OAUTH) error_state = request.args.get(Model_View_User.FLAG_ERROR_OAUTH)
@@ -185,6 +202,7 @@ def logout():
return redirect(url_logout) return redirect(url_logout)
@routes_user.route("/logout_callback") # <path:subpath>/<code> @routes_user.route("/logout_callback") # <path:subpath>/<code>
@handle_db_disconnect
def logout_callback(): def logout_callback():
return redirect(url_for('routes_core.home')) return redirect(url_for('routes_core.home'))
try: try: