@@ -11,14 +11,17 @@ def login_required(f):
1111 @functools .wraps (f )
1212 def decorated_function (* args , ** kwargs ):
1313 if 'user_id' not in session :
14- return redirect (url_for ('auth.login' ))
14+ # Capture the full path the user was trying to access
15+ return redirect (url_for ('auth.login' , next = request .full_path ))
1516 return f (* args , ** kwargs )
1617 return decorated_function
1718
18- # --- Routes ---
19-
19+ # --- Login Route ---
2020@auth_bp .route ("/login" , methods = ["GET" , "POST" ])
2121def login ():
22+ if 'user_id' in session :
23+ return redirect (url_for ('main.home' ))
24+
2225 if request .method == "POST" :
2326 email = request .form .get ("email" )
2427 password = request .form .get ("password" )
@@ -27,10 +30,20 @@ def login():
2730 # Exchange password for auth token via Google Identity Toolkit
2831 url = f"https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key={ FIREBASE_WEB_API_KEY } "
2932 resp = requests .post (url , json = {"email" : email , "password" : password , "returnSecureToken" : True })
33+
3034 if resp .status_code == 200 :
35+ # Activate the 30-day persistent session
3136 session .permanent = True
3237 session ['user_id' ] = resp .json ()['localId' ]
33- return redirect (url_for ('main.home' ))
38+
39+ # Retrieve the 'next' destination from the URL parameters
40+ next_page = request .args .get ('next' )
41+
42+ # Security Check: Ensure 'next_page' is a relative path (starts with /)
43+ if not next_page or not next_page .startswith ('/' ):
44+ next_page = url_for ('main.home' )
45+
46+ return redirect (next_page )
3447 else :
3548 return render_template ("auth/login.html" , mode = "login" , error = "Invalid email or password" )
3649 else :
@@ -40,6 +53,9 @@ def login():
4053
4154@auth_bp .route ("/register" , methods = ["GET" , "POST" ])
4255def register ():
56+ if 'user_id' in session :
57+ return redirect (url_for ('main.home' ))
58+
4359 if request .method == "POST" :
4460 email = request .form .get ("email" )
4561 password = request .form .get ("password" )
@@ -48,6 +64,7 @@ def register():
4864 url = f"https://identitytoolkit.googleapis.com/v1/accounts:signUp?key={ FIREBASE_WEB_API_KEY } "
4965 resp = requests .post (url , json = {"email" : email , "password" : password , "returnSecureToken" : True })
5066 if resp .status_code == 200 :
67+ session .permanent = True
5168 session ['user_id' ] = resp .json ()['localId' ]
5269 flash ("Registration Successful! Welcome to the Toolkit." , "success" )
5370 return redirect (url_for ('main.setup' ))
0 commit comments