Skip to content

Commit d314050

Browse files
committed
Bugs fixed, cleanup remains
1 parent 6adbfa3 commit d314050

File tree

9 files changed

+115
-93
lines changed

9 files changed

+115
-93
lines changed

apps/react/self-hosting/forms/page-mode-demo/app/api/host-app/README.md

Lines changed: 0 additions & 23 deletions
This file was deleted.

apps/react/self-hosting/forms/page-mode-demo/app/api/host-app/users/save/route.ts

Lines changed: 0 additions & 68 deletions
This file was deleted.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"""
2+
Host App module - Application-specific endpoints (separate from Velt)
3+
"""
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"""
2+
Host App URL configuration
3+
Handles application-specific functionality separate from Velt API
4+
"""
5+
from django.urls import path
6+
from .views import save_user
7+
8+
urlpatterns = [
9+
path('users/save', save_user, name='save_user'),
10+
]
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
"""
2+
Host App Views - Application-specific functionality
3+
"""
4+
import json
5+
import os
6+
from django.http import JsonResponse
7+
from django.views.decorators.csrf import csrf_exempt
8+
from django.views.decorators.http import require_http_methods
9+
from pymongo import MongoClient
10+
11+
# MongoDB client cache
12+
_mongo_client = None
13+
14+
def get_mongo_client():
15+
"""Get cached MongoDB client"""
16+
global _mongo_client
17+
if _mongo_client is None:
18+
connection_string = os.environ.get('VELT_MONGODB_CONNECTION_STRING')
19+
if not connection_string:
20+
raise ValueError('VELT_MONGODB_CONNECTION_STRING not configured')
21+
_mongo_client = MongoClient(connection_string)
22+
return _mongo_client
23+
24+
25+
@csrf_exempt
26+
@require_http_methods(["POST"])
27+
def save_user(request):
28+
"""
29+
Save user to MongoDB
30+
31+
This is part of the HOST APP, not Velt implementation.
32+
Saves users to MongoDB when they log in or are created.
33+
34+
Request body:
35+
{
36+
"user": {
37+
"userId": "string",
38+
"name": "string",
39+
"email": "string",
40+
"organizationId": "string",
41+
"photoUrl": "string"
42+
}
43+
}
44+
"""
45+
try:
46+
data = json.loads(request.body)
47+
user = data.get('user')
48+
49+
if not user or not user.get('userId'):
50+
return JsonResponse({
51+
'success': False,
52+
'error': 'userId is required',
53+
'statusCode': 400
54+
}, status=400)
55+
56+
# Get MongoDB connection
57+
client = get_mongo_client()
58+
db_name = os.environ.get('VELT_MONGODB_DATABASE', 'velt_comments')
59+
db = client[db_name]
60+
collection = db['users']
61+
62+
# Upsert user (update if exists, insert if not)
63+
collection.update_one(
64+
{'userId': user['userId']},
65+
{'$set': user},
66+
upsert=True
67+
)
68+
69+
print(f"[Host App] User saved to MongoDB: {user['userId']}")
70+
71+
return JsonResponse({
72+
'success': True,
73+
'statusCode': 200
74+
})
75+
76+
except json.JSONDecodeError:
77+
return JsonResponse({
78+
'success': False,
79+
'error': 'Invalid JSON',
80+
'statusCode': 400
81+
}, status=400)
82+
except Exception as e:
83+
print(f"[Host App] Error saving user: {str(e)}")
84+
return JsonResponse({
85+
'success': False,
86+
'error': str(e),
87+
'statusCode': 500
88+
}, status=500)

apps/react/self-hosting/forms/page-mode-demo/app/api/velt/backend/velt_test_project/urls.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66

77
urlpatterns = [
88
path('api/velt/', include('velt_api.urls')),
9+
path('api/host-app/', include('host_app.urls')),
910
]
1011

apps/react/self-hosting/forms/page-mode-demo/app/document/DocumentContext.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export function useCurrentDocument(): CurrentDocument {
5959

6060
// Update URL with document ID for shareability
6161
const newUrl = `${window.location.pathname}?documentId=${docId}`;
62-
window.history.pushState({}, '', newUrl);
62+
window.history.replaceState({}, '', newUrl);
6363

6464
setDocumentId(docId);
6565
}

apps/react/self-hosting/forms/page-mode-demo/app/userAuth/AppUserContext.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ function hashString(str: string): string {
6767
// [Host App] Save user to database via host-app API
6868
async function saveUserToDatabase(user: User): Promise<void> {
6969
try {
70-
const response = await fetch('/api/host-app/users/save', {
70+
// Use Django backend for user saving (avoids MongoDB native module issues in Next.js)
71+
const baseUrl = process.env.NEXT_PUBLIC_SELF_HOSTING_BASE_URL?.replace('/api/velt', '') || 'http://localhost:8000';
72+
const response = await fetch(`${baseUrl}/api/host-app/users/save`, {
7173
method: 'POST',
7274
headers: { 'Content-Type': 'application/json' },
7375
body: JSON.stringify({ user })

apps/react/self-hosting/forms/page-mode-demo/next.config.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ const EMBED_CSP = `frame-ancestors ${allowList}`;
1313

1414
const nextConfig = {
1515
serverExternalPackages: ['mongodb'],
16+
// Disable Turbopack for API routes that use native modules (MongoDB)
17+
experimental: {
18+
turbo: {
19+
resolveAlias: {
20+
// Force mongodb to be resolved as external
21+
'mongodb': 'mongodb',
22+
},
23+
},
24+
},
1625
async headers() {
1726
return [
1827
{

0 commit comments

Comments
 (0)