deploy: update habadashi_login
Browse files
app.py
CHANGED
|
@@ -136,7 +136,7 @@ class RequestLoggingMiddleware(BaseHTTPMiddleware):
|
|
| 136 |
|
| 137 |
@staticmethod
|
| 138 |
def _resolve_user(request: Request):
|
| 139 |
-
"""User resolution from cookie.
|
| 140 |
token = request.cookies.get("sb_access_token")
|
| 141 |
if not token:
|
| 142 |
return None
|
|
@@ -148,18 +148,33 @@ class RequestLoggingMiddleware(BaseHTTPMiddleware):
|
|
| 148 |
if user_id in _user_profile_cache:
|
| 149 |
return _user_profile_cache[user_id]
|
| 150 |
|
| 151 |
-
# 初回のみ profiles から org_name を取得
|
| 152 |
email = res.user.email
|
|
|
|
| 153 |
org_name = None
|
|
|
|
|
|
|
| 154 |
try:
|
| 155 |
profile_res = supabase.from_("profiles").select(
|
| 156 |
-
"organizations(name)"
|
| 157 |
).eq("id", user_id).single().execute()
|
| 158 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
except Exception as pe:
|
| 160 |
print(f"[ORG_CONTEXT] _resolve_user: profile fetch failed: {pe}")
|
| 161 |
-
print(f"[ORG_CONTEXT] _resolve_user: first fetch user_id={user_id} email={email} org_name={org_name!r}")
|
| 162 |
-
user_info = {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
_user_profile_cache[user_id] = user_info
|
| 164 |
return user_info
|
| 165 |
except Exception:
|
|
@@ -197,31 +212,35 @@ def handle_login(email, password):
|
|
| 197 |
|
| 198 |
# --- Authentication Dependency ---
|
| 199 |
def get_current_user(request: Request):
|
| 200 |
-
"""Verify token from cookie and fetch user profile"""
|
| 201 |
token = request.cookies.get("sb_access_token")
|
| 202 |
|
| 203 |
if not token:
|
| 204 |
return None
|
| 205 |
|
| 206 |
try:
|
| 207 |
-
# Verify token with Supabase
|
| 208 |
res = supabase.auth.get_user(token)
|
| 209 |
-
user_id = res.user.id
|
| 210 |
|
| 211 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 212 |
profile_res = supabase.from_("profiles").select(
|
| 213 |
"email, org_id, role, display_name, organizations(name)"
|
| 214 |
).eq("id", user_id).single().execute()
|
| 215 |
|
| 216 |
-
|
| 217 |
user_dict = {
|
| 218 |
-
"user_id":
|
| 219 |
-
"email":
|
| 220 |
-
"display_name":
|
| 221 |
-
"role":
|
| 222 |
-
"
|
|
|
|
| 223 |
}
|
| 224 |
-
|
| 225 |
return user_dict
|
| 226 |
|
| 227 |
except Exception as e:
|
|
@@ -263,22 +282,26 @@ if private_app_dir:
|
|
| 263 |
print(f"[LOGGING] Could not import lib.logging or set_logger_callback: {e}")
|
| 264 |
# -------------------------------
|
| 265 |
|
| 266 |
-
# --- Inject Org
|
| 267 |
try:
|
| 268 |
-
from lib.hf_storage import
|
| 269 |
from supabase_logger import get_user_context
|
| 270 |
|
| 271 |
def get_org_for_storage():
|
| 272 |
-
"""現在のリクエストユーザーの
|
| 273 |
user_ctx = get_user_context()
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 280 |
except ImportError as e:
|
| 281 |
-
print(f"[ORG_CONTEXT] Could not inject
|
| 282 |
# ---------------------------------------------------------
|
| 283 |
|
| 284 |
ver20_app = ver20_blocks
|
|
|
|
| 136 |
|
| 137 |
@staticmethod
|
| 138 |
def _resolve_user(request: Request):
|
| 139 |
+
"""User resolution from cookie. Full profile (incl. org_id) fetched once and cached per user_id."""
|
| 140 |
token = request.cookies.get("sb_access_token")
|
| 141 |
if not token:
|
| 142 |
return None
|
|
|
|
| 148 |
if user_id in _user_profile_cache:
|
| 149 |
return _user_profile_cache[user_id]
|
| 150 |
|
| 151 |
+
# 初回のみ profiles から org_id/org_name/role/display_name を全取得
|
| 152 |
email = res.user.email
|
| 153 |
+
org_id = None
|
| 154 |
org_name = None
|
| 155 |
+
role = None
|
| 156 |
+
display_name = None
|
| 157 |
try:
|
| 158 |
profile_res = supabase.from_("profiles").select(
|
| 159 |
+
"email, org_id, role, display_name, organizations(name)"
|
| 160 |
).eq("id", user_id).single().execute()
|
| 161 |
+
d = profile_res.data or {}
|
| 162 |
+
org_id = d.get("org_id")
|
| 163 |
+
org_name = (d.get("organizations") or {}).get("name")
|
| 164 |
+
role = d.get("role")
|
| 165 |
+
display_name = d.get("display_name")
|
| 166 |
+
email = d.get("email") or email
|
| 167 |
except Exception as pe:
|
| 168 |
print(f"[ORG_CONTEXT] _resolve_user: profile fetch failed: {pe}")
|
| 169 |
+
print(f"[ORG_CONTEXT] _resolve_user: first fetch user_id={user_id} email={email} org_id={org_id!r} org_name={org_name!r}")
|
| 170 |
+
user_info = {
|
| 171 |
+
"user_id": user_id,
|
| 172 |
+
"email": email,
|
| 173 |
+
"display_name": display_name,
|
| 174 |
+
"role": role,
|
| 175 |
+
"org_id": org_id,
|
| 176 |
+
"org_name": org_name,
|
| 177 |
+
}
|
| 178 |
_user_profile_cache[user_id] = user_info
|
| 179 |
return user_info
|
| 180 |
except Exception:
|
|
|
|
| 212 |
|
| 213 |
# --- Authentication Dependency ---
|
| 214 |
def get_current_user(request: Request):
|
| 215 |
+
"""Verify token from cookie and fetch user profile (uses _user_profile_cache)"""
|
| 216 |
token = request.cookies.get("sb_access_token")
|
| 217 |
|
| 218 |
if not token:
|
| 219 |
return None
|
| 220 |
|
| 221 |
try:
|
|
|
|
| 222 |
res = supabase.auth.get_user(token)
|
| 223 |
+
user_id = str(res.user.id)
|
| 224 |
|
| 225 |
+
# キャッシュがあればそれを返す(ミドルウェアが先に取得済みのはず)
|
| 226 |
+
if user_id in _user_profile_cache:
|
| 227 |
+
return _user_profile_cache[user_id]
|
| 228 |
+
|
| 229 |
+
# キャッシュ未作成の場合(直接アクセス等)はここで取得してキャッシュする
|
| 230 |
profile_res = supabase.from_("profiles").select(
|
| 231 |
"email, org_id, role, display_name, organizations(name)"
|
| 232 |
).eq("id", user_id).single().execute()
|
| 233 |
|
| 234 |
+
d = profile_res.data or {}
|
| 235 |
user_dict = {
|
| 236 |
+
"user_id": user_id,
|
| 237 |
+
"email": d.get("email"),
|
| 238 |
+
"display_name": d.get("display_name"),
|
| 239 |
+
"role": d.get("role"),
|
| 240 |
+
"org_id": d.get("org_id"),
|
| 241 |
+
"org_name": (d.get("organizations") or {}).get("name"),
|
| 242 |
}
|
| 243 |
+
_user_profile_cache[user_id] = user_dict
|
| 244 |
return user_dict
|
| 245 |
|
| 246 |
except Exception as e:
|
|
|
|
| 282 |
print(f"[LOGGING] Could not import lib.logging or set_logger_callback: {e}")
|
| 283 |
# -------------------------------
|
| 284 |
|
| 285 |
+
# --- Inject Org Context Getter (for HF dataset namespace) ---
|
| 286 |
try:
|
| 287 |
+
from lib.hf_storage import set_org_context_getter
|
| 288 |
from supabase_logger import get_user_context
|
| 289 |
|
| 290 |
def get_org_for_storage():
|
| 291 |
+
"""現在のリクエストユーザーの org_id/org_name を返す(HF dataset のネームスペース用)"""
|
| 292 |
user_ctx = get_user_context()
|
| 293 |
+
if user_ctx and isinstance(user_ctx, dict):
|
| 294 |
+
org_id = user_ctx.get("org_id")
|
| 295 |
+
org_name = user_ctx.get("org_name")
|
| 296 |
+
print(f"[ORG_CONTEXT] get_org_for_storage: org_id={org_id!r} org_name={org_name!r}")
|
| 297 |
+
return {"org_id": org_id, "org_name": org_name}
|
| 298 |
+
print(f"[ORG_CONTEXT] get_org_for_storage: no user_ctx")
|
| 299 |
+
return None
|
| 300 |
+
|
| 301 |
+
set_org_context_getter(get_org_for_storage)
|
| 302 |
+
print("[ORG_CONTEXT] Connected org_context getter to hf_storage")
|
| 303 |
except ImportError as e:
|
| 304 |
+
print(f"[ORG_CONTEXT] Could not inject org_context getter: {e}")
|
| 305 |
# ---------------------------------------------------------
|
| 306 |
|
| 307 |
ver20_app = ver20_blocks
|