cyberai-1 commited on
Commit
49fb46d
·
1 Parent(s): 6bf9e00

Fix login redirect on Hugging Face

Browse files
Files changed (1) hide show
  1. app.py +22 -3
app.py CHANGED
@@ -7,6 +7,7 @@ from uuid import uuid4
7
  import numpy as np
8
  from flask import Flask, flash, redirect, render_template, request, session, url_for
9
  from PIL import Image
 
10
  from werkzeug.security import check_password_hash, generate_password_hash
11
  from werkzeug.utils import secure_filename
12
 
@@ -72,6 +73,14 @@ TRAFFIC_SIGN_CLASSES = [
72
  app = Flask(__name__)
73
  app.config["SECRET_KEY"] = os.environ.get("SECRET_KEY", "dev-change-me")
74
  app.config["MAX_CONTENT_LENGTH"] = 8 * 1024 * 1024
 
 
 
 
 
 
 
 
75
  UPLOAD_DIR.mkdir(parents=True, exist_ok=True)
76
  DATABASE_PATH.parent.mkdir(parents=True, exist_ok=True)
77
 
@@ -138,7 +147,7 @@ def login_required(view):
138
  def wrapped(*args, **kwargs):
139
  if not current_user():
140
  flash("Please log in to access the classifier.", "warning")
141
- return redirect(url_for("login"))
142
  return view(*args, **kwargs)
143
 
144
  wrapped.__name__ = view.__name__
@@ -178,6 +187,9 @@ def welcome():
178
 
179
  @app.route("/register", methods=["GET", "POST"])
180
  def register():
 
 
 
181
  if request.method == "POST":
182
  name = request.form.get("name", "").strip()
183
  email = request.form.get("email", "").strip().lower()
@@ -201,8 +213,10 @@ def register():
201
  (name, email, generate_password_hash(password), datetime.utcnow().isoformat()),
202
  )
203
  session["user_id"] = cursor.lastrowid
 
 
204
  flash("Account created. Welcome to the classifier.", "success")
205
- return redirect(url_for("predict"))
206
  except sqlite3.IntegrityError:
207
  flash("This email is already registered.", "danger")
208
 
@@ -211,6 +225,9 @@ def register():
211
 
212
  @app.route("/login", methods=["GET", "POST"])
213
  def login():
 
 
 
214
  if request.method == "POST":
215
  email = request.form.get("email", "").strip().lower()
216
  password = request.form.get("password", "")
@@ -218,8 +235,10 @@ def login():
218
  user = conn.execute("SELECT * FROM users WHERE email = ?", (email,)).fetchone()
219
  if user and check_password_hash(user["password_hash"], password):
220
  session["user_id"] = user["id"]
 
 
221
  flash("Connection established.", "success")
222
- return redirect(url_for("predict"))
223
  flash("Invalid email or password.", "danger")
224
  return render_template("login.html")
225
 
 
7
  import numpy as np
8
  from flask import Flask, flash, redirect, render_template, request, session, url_for
9
  from PIL import Image
10
+ from werkzeug.middleware.proxy_fix import ProxyFix
11
  from werkzeug.security import check_password_hash, generate_password_hash
12
  from werkzeug.utils import secure_filename
13
 
 
73
  app = Flask(__name__)
74
  app.config["SECRET_KEY"] = os.environ.get("SECRET_KEY", "dev-change-me")
75
  app.config["MAX_CONTENT_LENGTH"] = 8 * 1024 * 1024
76
+ app.wsgi_app = ProxyFix(app.wsgi_app, x_for=1, x_proto=1, x_host=1, x_prefix=1)
77
+
78
+ if os.environ.get("SPACE_ID"):
79
+ app.config["SESSION_COOKIE_SECURE"] = True
80
+ app.config["SESSION_COOKIE_SAMESITE"] = "None"
81
+ else:
82
+ app.config["SESSION_COOKIE_SAMESITE"] = "Lax"
83
+
84
  UPLOAD_DIR.mkdir(parents=True, exist_ok=True)
85
  DATABASE_PATH.parent.mkdir(parents=True, exist_ok=True)
86
 
 
147
  def wrapped(*args, **kwargs):
148
  if not current_user():
149
  flash("Please log in to access the classifier.", "warning")
150
+ return redirect(url_for("login", next=request.path))
151
  return view(*args, **kwargs)
152
 
153
  wrapped.__name__ = view.__name__
 
187
 
188
  @app.route("/register", methods=["GET", "POST"])
189
  def register():
190
+ if current_user():
191
+ return redirect(url_for("predict"))
192
+
193
  if request.method == "POST":
194
  name = request.form.get("name", "").strip()
195
  email = request.form.get("email", "").strip().lower()
 
213
  (name, email, generate_password_hash(password), datetime.utcnow().isoformat()),
214
  )
215
  session["user_id"] = cursor.lastrowid
216
+ session.permanent = True
217
+ session.modified = True
218
  flash("Account created. Welcome to the classifier.", "success")
219
+ return redirect(request.args.get("next") or url_for("predict"))
220
  except sqlite3.IntegrityError:
221
  flash("This email is already registered.", "danger")
222
 
 
225
 
226
  @app.route("/login", methods=["GET", "POST"])
227
  def login():
228
+ if current_user():
229
+ return redirect(url_for("predict"))
230
+
231
  if request.method == "POST":
232
  email = request.form.get("email", "").strip().lower()
233
  password = request.form.get("password", "")
 
235
  user = conn.execute("SELECT * FROM users WHERE email = ?", (email,)).fetchone()
236
  if user and check_password_hash(user["password_hash"], password):
237
  session["user_id"] = user["id"]
238
+ session.permanent = True
239
+ session.modified = True
240
  flash("Connection established.", "success")
241
+ return redirect(request.args.get("next") or url_for("predict"))
242
  flash("Invalid email or password.", "danger")
243
  return render_template("login.html")
244