Refactor authentication and session management
- Removed session-based user retrieval from dependencies.py and main.py. - Eliminated SessionMiddleware and related session handling code. - Updated dashboard and team views to directly query user data from the database. - Simplified redeem and team management views to remove authentication checks. - Adjusted templates to reflect changes in user context handling. - Added camera selection feature in scan_qr.html for improved QR code scanning. - Cleaned up Docker configuration and requirements for better deployment.
This commit is contained in:
+22
-62
@@ -8,8 +8,6 @@ from sqlalchemy.orm import Session
|
||||
from ..db import SessionLocal
|
||||
from ..models import QRTicket, User, Team, TeamMembership
|
||||
from ..templates_config import templates
|
||||
from ..dependencies import get_user_from_session
|
||||
from ..auth import require_login
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@@ -24,7 +22,7 @@ def get_db():
|
||||
def redeem_code(code: str, request: Request, db: Session = Depends(get_db)):
|
||||
"""
|
||||
Display a page to let the user choose which team to apply points to.
|
||||
If not logged in, prompt them.
|
||||
No login required.
|
||||
"""
|
||||
ticket = db.query(QRTicket).filter_by(code=code, used=False).first()
|
||||
if not ticket:
|
||||
@@ -37,42 +35,24 @@ def redeem_code(code: str, request: Request, db: Session = Depends(get_db)):
|
||||
}
|
||||
)
|
||||
|
||||
# Get the authenticated user
|
||||
user = get_user_from_session(request, db)
|
||||
if not user:
|
||||
# Store the redeem URL for after login - with try/except
|
||||
try:
|
||||
if hasattr(request, "session") and isinstance(request.session, dict):
|
||||
request.session["redirect_after_login"] = f"/redeem/{code}"
|
||||
except Exception as e:
|
||||
print(f"Error setting redirect URL in session: {str(e)}")
|
||||
return RedirectResponse(url="/auth/login", status_code=302)
|
||||
|
||||
# Get user teams
|
||||
user_teams = [m.team for m in user.memberships] if hasattr(user, 'memberships') else []
|
||||
# Get all available teams
|
||||
all_teams = db.query(Team).all()
|
||||
|
||||
return templates.TemplateResponse("redeem.html", {
|
||||
"request": request,
|
||||
"ticket": ticket,
|
||||
"user_teams": user_teams
|
||||
"user_teams": all_teams # Now showing all teams
|
||||
})
|
||||
|
||||
@router.post("/apply/{code}")
|
||||
@require_login
|
||||
async def apply_code(
|
||||
request: Request,
|
||||
code: str,
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""
|
||||
Apply the QR code to a selected team (if user is a member),
|
||||
or set to pending if user isn't a member yet.
|
||||
Apply the QR code to a selected team (without authentication)
|
||||
"""
|
||||
# Get the authenticated user
|
||||
user = get_user_from_session(request, db)
|
||||
if not user:
|
||||
return RedirectResponse(url=f"/auth/login?next=/redeem/{code}", status_code=302)
|
||||
|
||||
# Get form data
|
||||
form_data = await request.form()
|
||||
team_id = int(form_data.get("team_id", 0))
|
||||
@@ -110,43 +90,28 @@ async def apply_code(
|
||||
}
|
||||
)
|
||||
|
||||
# Check membership
|
||||
membership = db.query(TeamMembership).filter_by(user_id=user.id, team_id=team.id).first()
|
||||
if membership:
|
||||
# Redeem
|
||||
ticket.redeemed_by = user.id
|
||||
ticket.redeemed_at_team = team.id
|
||||
ticket.used = True
|
||||
# Redeem without checking membership
|
||||
ticket.redeemed_at_team = team.id
|
||||
ticket.used = True
|
||||
|
||||
# If we have redeemed_at column, update it
|
||||
if hasattr(ticket, 'redeemed_at'):
|
||||
from datetime import datetime
|
||||
ticket.redeemed_at = datetime.now()
|
||||
# If we have redeemed_at column, update it
|
||||
if hasattr(ticket, 'redeemed_at'):
|
||||
from datetime import datetime
|
||||
ticket.redeemed_at = datetime.now()
|
||||
|
||||
db.commit()
|
||||
db.commit()
|
||||
|
||||
# Redirect to success page or dashboard
|
||||
return templates.TemplateResponse(
|
||||
"redeem_success.html",
|
||||
{
|
||||
"request": request,
|
||||
"points": ticket.points,
|
||||
"team": team
|
||||
}
|
||||
)
|
||||
else:
|
||||
# In real app, create a pending record or request flow
|
||||
return templates.TemplateResponse(
|
||||
"error.html",
|
||||
{
|
||||
"request": request,
|
||||
"error_title": "Not a Team Member",
|
||||
"error_message": "You are not a member of this team. Please join the team first or select another team."
|
||||
}
|
||||
)
|
||||
# Redirect to success page or dashboard
|
||||
return templates.TemplateResponse(
|
||||
"redeem_success.html",
|
||||
{
|
||||
"request": request,
|
||||
"points": ticket.points,
|
||||
"team": team
|
||||
}
|
||||
)
|
||||
|
||||
@router.post("/manual")
|
||||
@require_login
|
||||
async def manual_code_entry(
|
||||
request: Request,
|
||||
code: str = Form(...),
|
||||
@@ -156,11 +121,6 @@ async def manual_code_entry(
|
||||
Handle manual code entry from the form.
|
||||
This redirects to the normal redeem flow after validating the code.
|
||||
"""
|
||||
# Make sure the user is authenticated
|
||||
user = get_user_from_session(request, db)
|
||||
if not user:
|
||||
return RedirectResponse(url="/auth/login?next=/dashboard/", status_code=302)
|
||||
|
||||
# Check if the code exists
|
||||
ticket = db.query(QRTicket).filter_by(code=code, used=False).first()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user