MJ-Prod commited on
Commit
e6489d5
·
1 Parent(s): 89b5336

added Delete Account_6

Browse files
Files changed (1) hide show
  1. app.py +32 -28
app.py CHANGED
@@ -3,6 +3,7 @@ load_dotenv()
3
 
4
  from fastapi.responses import StreamingResponse
5
  import json
 
6
  from fastapi import FastAPI, HTTPException, Depends
7
  from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
8
  from fastapi.middleware.cors import CORSMiddleware
@@ -10,7 +11,6 @@ from pydantic import BaseModel
10
  from fiscal import get_answer, clear_memory, stream_answer
11
  from auth import verify_token
12
  from plaid_client import get_financial_snapshot
13
- from supabase import create_client
14
  import os
15
 
16
  app = FastAPI(title="FISCAL API")
@@ -22,20 +22,8 @@ app.add_middleware(
22
  allow_headers=["*"],
23
  )
24
 
25
- SANDBOX_ACCESS_TOKEN = os.environ["PLAID_ACCESS_TOKEN"]
26
-
27
- # Initialize Supabase admin client once at startup
28
- supabase_url = os.environ.get("SUPABASE_URL", "")
29
- service_key = os.environ.get("SUPABASE_SERVICE_KEY", "")
30
-
31
- if supabase_url and service_key:
32
- supabase_admin = create_client(supabase_url, service_key)
33
- else:
34
- supabase_admin = None
35
- print("WARNING: SUPABASE_SERVICE_KEY not set - account deletion disabled")
36
-
37
  security = HTTPBearer()
38
-
39
 
40
  class ChatRequest(BaseModel):
41
  message: str
@@ -107,31 +95,47 @@ def chat_stream(
107
 
108
 
109
 
 
 
110
  @app.delete("/account")
111
  def delete_account(
112
  credentials: HTTPAuthorizationCredentials = Depends(security),
113
  ):
114
- if supabase_admin is None:
115
- raise HTTPException(status_code=503, detail="Account deletion not configured")
116
-
117
  user = verify_token(credentials.credentials)
118
  if not user:
119
  raise HTTPException(status_code=401, detail="Unauthorized")
120
 
121
  user_id = user["sub"]
 
 
122
 
123
- try:
124
- supabase_admin.table("bank_connections")\
125
- .delete()\
126
- .eq("user_id", user_id)\
127
- .execute()
128
-
129
- supabase_admin.table("profiles")\
130
- .delete()\
131
- .eq("id", user_id)\
132
- .execute()
133
 
134
- supabase_admin.auth.admin.delete_user(user_id)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
135
 
136
  return {"status": "account deleted"}
137
 
 
3
 
4
  from fastapi.responses import StreamingResponse
5
  import json
6
+ import requests
7
  from fastapi import FastAPI, HTTPException, Depends
8
  from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
9
  from fastapi.middleware.cors import CORSMiddleware
 
11
  from fiscal import get_answer, clear_memory, stream_answer
12
  from auth import verify_token
13
  from plaid_client import get_financial_snapshot
 
14
  import os
15
 
16
  app = FastAPI(title="FISCAL API")
 
22
  allow_headers=["*"],
23
  )
24
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  security = HTTPBearer()
26
+ SANDBOX_ACCESS_TOKEN = os.environ["PLAID_ACCESS_TOKEN"]
27
 
28
  class ChatRequest(BaseModel):
29
  message: str
 
95
 
96
 
97
 
98
+
99
+
100
  @app.delete("/account")
101
  def delete_account(
102
  credentials: HTTPAuthorizationCredentials = Depends(security),
103
  ):
 
 
 
104
  user = verify_token(credentials.credentials)
105
  if not user:
106
  raise HTTPException(status_code=401, detail="Unauthorized")
107
 
108
  user_id = user["sub"]
109
+ supabase_url = os.environ.get("SUPABASE_URL", "")
110
+ service_key = os.environ.get("SUPABASE_SERVICE_KEY", "")
111
 
112
+ headers = {
113
+ "apikey": service_key,
114
+ "Authorization": f"Bearer {service_key}",
115
+ "Content-Type": "application/json"
116
+ }
 
 
 
 
 
117
 
118
+ try:
119
+ # 1. Delete bank connections
120
+ requests.delete(
121
+ f"{supabase_url}/rest/v1/bank_connections?user_id=eq.{user_id}",
122
+ headers=headers
123
+ )
124
+
125
+ # 2. Delete profile
126
+ requests.delete(
127
+ f"{supabase_url}/rest/v1/profiles?id=eq.{user_id}",
128
+ headers=headers
129
+ )
130
+
131
+ # 3. Delete auth user via Admin API
132
+ response = requests.delete(
133
+ f"{supabase_url}/auth/v1/admin/users/{user_id}",
134
+ headers=headers
135
+ )
136
+
137
+ if response.status_code not in [200, 204]:
138
+ raise Exception(f"Auth delete failed: {response.text}")
139
 
140
  return {"status": "account deleted"}
141