Skip to content

Commit 92d3b35

Browse files
committed
fix: user data issue
1 parent 68b8136 commit 92d3b35

3 files changed

Lines changed: 87 additions & 74 deletions

File tree

src/client/components/LayoutWrapper.js

Lines changed: 29 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,28 @@ function urlBase64ToUint8Array(base64String) {
4343
return outputArray
4444
}
4545

46+
const refreshSessionAndReload = async () => {
47+
try {
48+
const res = await fetch("/api/auth/refresh-session");
49+
if (!res.ok) {
50+
throw new Error("Failed to refresh session");
51+
}
52+
// Once the session cookie is updated on the server, reload the page.
53+
// The browser will send the new cookie, and the server will render with the updated user role.
54+
window.location.reload();
55+
} catch (error) {
56+
console.error("Session refresh failed:", error);
57+
// Optionally, handle the error, e.g., show a message to the user.
58+
// For simplicity, we can still fall back to the old method if refresh fails.
59+
const logoutUrl = new URL("/auth/logout", window.location.origin);
60+
logoutUrl.searchParams.set(
61+
"returnTo",
62+
`${process.env.NEXT_PUBLIC_APP_BASE_URL}`
63+
);
64+
window.location.assign(logoutUrl.toString());
65+
}
66+
};
67+
4668
export default function LayoutWrapper({ children }) {
4769
// ... (keep all your existing state declarations)
4870
const [isNotificationsOpen, setNotificationsOpen] = useState(false)
@@ -56,7 +78,6 @@ export default function LayoutWrapper({ children }) {
5678
const wsRef = useRef(null)
5779
const pathname = usePathname()
5880
const router = useRouter()
59-
const searchParams = useSearchParams() // Hook to read URL query parameters
6081
const posthog = usePostHog()
6182

6283
const { user, error: authError, isLoading: isAuthLoading } = useUser()
@@ -76,58 +97,18 @@ export default function LayoutWrapper({ children }) {
7697
}, [user, posthog])
7798

7899
useEffect(() => {
79-
const paymentStatus = searchParams.get("payment_status")
80-
const needsRefresh = searchParams.get("refresh_session")
100+
const urlParams = new URLSearchParams(window.location.search)
81101

82-
if (paymentStatus === "success" && posthog) {
83-
posthog.capture("plan_upgraded", {
84-
plan_name: "pro"
85-
// MRR and billing_cycle are not available on the client
86-
})
87-
}
88-
// Check for either trigger
89-
if (paymentStatus === "success" || needsRefresh === "true") {
90-
// CRITICAL FIX: Clean the URL synchronously *before* doing anything else.
91-
// This prevents the refresh loop.
92-
window.history.replaceState(null, "", pathname)
102+
// Handle post-upgrade: force a session refresh
103+
if (urlParams.get("refresh_session") === "true") {
93104
const toastId = toast.loading("Updating your session...", {
94105
duration: 4000
95106
})
96-
97-
// const refreshSession = async () => {
98-
// const toastId = toast.loading("Updating your session...", {
99-
// duration: 4000
100-
// })
101-
// try {
102-
// // Call the API to get a new session cookie
103-
// const res = await fetch("/api/auth/refresh-session")
104-
// if (!res.ok) {
105-
// const errorData = await res.json()
106-
// throw new Error(
107-
// errorData.error || "Session refresh failed."
108-
// )
109-
// }
110-
111-
// // Now that the cookie is updated and the URL is clean, reload the page.
112-
// // This will re-run server components and hooks with the new session data.
113-
// window.location.reload()
114-
// } catch (error) {
115-
// toast.error(
116-
// `Failed to refresh session: ${error.message}. Please log in again to see your new plan.`,
117-
// { id: toastId }
118-
// )
119-
// }
120-
// }
121-
// refreshSession()
122-
123-
const logoutUrl = new URL("/auth/logout", window.location.origin)
124-
logoutUrl.searchParams.set(
125-
"returnTo",
126-
process.env.NEXT_PUBLIC_APP_BASE_URL
127-
)
128-
window.location.assign(logoutUrl.toString())
107+
// Remove the query params from the URL to avoid re-triggering on reload
108+
window.history.replaceState(null, "", pathname)
109+
refreshSessionAndReload()
129110
}
130-
}, [searchParams, router, pathname]) // Dependencies are correct
111+
}, [])
131112

132113
// ... (keep the rest of your useEffects and functions exactly as they were)
133114
useEffect(() => {

src/server/Dockerfile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,20 @@ RUN mkdir -p /etc/supervisor/conf.d /var/log/supervisor
5656

5757
# Copy configuration files and server code
5858
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
59+
COPY nginx.conf /etc/nginx/sites-available/sentient
5960
COPY . .
6061

6162
# Fix line endings for .env and start.sh (if present)
6263
RUN if [ -f /app/.env ]; then dos2unix /app/.env; fi \
6364
&& dos2unix /app/start.sh \
6465
&& chmod +x /app/start.sh
6566

67+
# Remove default Nginx site configuration and enable our custom one
68+
RUN rm -f /etc/nginx/sites-enabled/default
69+
70+
# Enable custom Nginx site
71+
RUN ln -s /etc/nginx/sites-available/sentient /etc/nginx/sites-enabled/sentient
72+
6673
# Expose port
6774
EXPOSE 5000
6875

src/server/nginx.conf

Lines changed: 51 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,67 @@
1-
server {
2-
if ($host = <DOMAIN_NAME>) {
3-
return 301 https://$host$request_uri;
4-
} # managed by Certbot
5-
6-
listen 80;
7-
server_name <DOMAIN_NAME>;
1+
# This is a new file: src/server/nginx.conf
82

9-
# This block is for handling Let's Encrypt certificate challenges
10-
location /.well-known/acme-challenge/ {
11-
root <WEB_ROOT_PATH>;
12-
}
13-
14-
# For all other traffic, redirect to HTTPS
15-
location / {
16-
return 301 https://$host$request_uri;
17-
}
3+
upstream backend_app {
4+
# This points to your FastAPI/Uvicorn server running on localhost inside the container.
5+
server 127.0.0.1:5000;
186
}
197

208
server {
21-
listen 443 ssl http2;
22-
server_name <DOMAIN_NAME>;
23-
24-
# SSL cert paths will be filled in automatically by Certbot later
25-
ssl_certificate <SSL_CERT_PATH>; # managed by Certbot
26-
ssl_certificate_key <SSL_CERT_KEY_PATH>; # managed by Certbot
9+
# Nginx will listen on port 80 inside the container.
10+
# An external load balancer (like on GCP) or Docker port mapping will handle public-facing traffic.
11+
listen 80;
12+
server_name _; # Catch all domains
2713

14+
# Increase client body size for file uploads
2815
client_max_body_size 10M;
2916

30-
location / {
31-
proxy_pass http://<BACKEND_HOST>:<BACKEND_PORT>;
17+
# Location block for your chat stream (and other streaming endpoints)
18+
location ~ ^/(chat/message|api/search/unified)$ {
19+
proxy_pass http://backend_app;
3220
proxy_set_header Host $host;
3321
proxy_set_header X-Real-IP $remote_addr;
3422
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
3523
proxy_set_header X-Forwarded-Proto $scheme;
3624

37-
# WebSocket Headers - CRITICAL for wss:// to work
25+
proxy_read_timeout 86400s; # Prevent timeout during long LLM responses
26+
proxy_send_timeout 86400s;
27+
28+
# Crucial for Server-Sent Events (SSE) and streaming
29+
proxy_buffering off;
30+
proxy_cache off;
31+
proxy_request_buffering off;
32+
proxy_http_version 1.1;
33+
proxy_set_header Connection '';
34+
chunked_transfer_encoding on;
35+
36+
# Headers to prevent buffering by intermediate proxies/CDNs
37+
add_header Cache-Control "no-cache";
38+
add_header X-Accel-Buffering "no" always;
39+
}
40+
41+
# Location block for WebSocket connections (notifications and voice)
42+
location ~ ^/(api/ws/notifications|voice) {
43+
proxy_pass http://backend_app;
3844
proxy_http_version 1.1;
3945
proxy_set_header Upgrade $http_upgrade;
40-
proxy_set_header Connection "upgrade";
46+
proxy_set_header Connection "Upgrade";
47+
proxy_set_header Host $host;
48+
proxy_set_header X-Real-IP $remote_addr;
49+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
50+
proxy_set_header X-Forwarded-Proto $scheme;
51+
proxy_read_timeout 86400s; # Keep WebSocket connections alive
52+
proxy_send_timeout 86400s;
53+
}
54+
55+
# General location block for all other API requests
56+
location / {
57+
proxy_pass http://backend_app;
58+
proxy_set_header Host $host;
59+
proxy_set_header X-Real-IP $remote_addr;
60+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
61+
proxy_set_header X-Forwarded-Proto $scheme;
62+
proxy_connect_timeout 600s;
63+
proxy_send_timeout 600s;
64+
proxy_read_timeout 600s;
4165
}
4266
}
67+

0 commit comments

Comments
 (0)