Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 10 additions & 17 deletions project_funding_ledger/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import os
import flask
from flask import Flask, redirect, url_for
from dotenv import load_dotenv
from project_funding_ledger.auth import auth_bp
import project_funding_ledger.exceptions
from project_funding_ledger.profile import profile_bp
from project_funding_ledger.supabase_client import save_supabase_session
from project_funding_ledger.queue.webhooks import tasks_bp
from project_funding_ledger.routes.org_import import org_import_bp
from project_funding_ledger.routes.organization import org_bp
from project_funding_ledger.routes.public_org import public_org_bp

# Load environment variables from .env file
Expand All @@ -31,29 +34,19 @@ def create_app(test_config=None):
app.register_blueprint(profile_bp)
app.register_blueprint(tasks_bp)
app.register_blueprint(org_import_bp)
app.register_blueprint(org_bp)
app.register_blueprint(public_org_bp)

# After-request hook to persist refreshed Supabase tokens in session cookie
app.after_request(save_supabase_session)

@app.route('/')
def index():
# Redirect index to admin dashboard if System Administrator, else profile page
from project_funding_ledger.supabase_client import get_supabase_client
client = get_supabase_client()
try:
user_res = client.auth.get_user()
user = user_res.user if user_res else None
if user:
profile_res = client.table('user_profile').select('user_type').eq('auth_user_id', user.id).execute()
if profile_res.data and profile_res.data[0].get('user_type') == 'System Administrator':
return redirect(url_for('org_import.admin_dashboard'))
except Exception:
pass
return redirect(url_for('profile.profile_page'))

@app.route('/hello')
def hello():
return 'Hello, World!'
return redirect(url_for('public_org.organization_list'))

@app.errorhandler(project_funding_ledger.exceptions.AuthRequiredError)
def auth_required(error):
# TODO(tswast): allow auth.login to redirect to the desired page, with protections to avoid redirecting off of the current site.
return redirect(url_for('auth.login'))

return app
24 changes: 22 additions & 2 deletions project_funding_ledger/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,17 @@ def login():
if user_type == 'System Administrator':
return redirect(url_for('org_import.admin_dashboard'))
else:
return redirect(url_for('profile.profile_page'))
try:
profile_res = client.table('user_profile').select('id').eq('auth_user_id', res.user.id).single().execute()
if profile_res.data:
user_profile_id = profile_res.data['id']
perms_res = client.table('organization_permission').select('organization_id').eq('user_id', user_profile_id).eq('status', 'Active').execute()
perms = perms_res.data or []
if len(perms) == 1:
return redirect(url_for('org.org_detail', org_id=perms[0]['organization_id']))
except Exception:
pass
return redirect(url_for('org.dashboard'))
except Exception as e:
flash(f"Login failed: {str(e)}", "error")

Expand Down Expand Up @@ -206,7 +216,17 @@ def register():
if user_type == 'System Administrator':
return redirect(url_for('org_import.admin_dashboard'))
else:
return redirect(url_for('profile.profile_page'))
try:
profile_res = client.table('user_profile').select('id').eq('auth_user_id', signup_res.user.id).single().execute()
if profile_res.data:
user_profile_id = profile_res.data['id']
perms_res = client.table('organization_permission').select('organization_id').eq('user_id', user_profile_id).eq('status', 'Active').execute()
perms = perms_res.data or []
if len(perms) == 1:
return redirect(url_for('org.org_detail', org_id=perms[0]['organization_id']))
except Exception:
pass
return redirect(url_for('org.dashboard'))
else:
flash("Registration successful! Please check your email for a confirmation link.", "success")
return redirect(url_for('auth.login'))
Expand Down
3 changes: 3 additions & 0 deletions project_funding_ledger/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

class AuthRequiredError(Exception):
"Exception raised for pages that require login first."
Loading