Skip to content

Commit 2327303

Browse files
committed
Update Firestore rules for user data access and adjust Firestore port; refactor document timestamp handling for consistency
1 parent 2a7ef37 commit 2327303

File tree

3 files changed

+31
-41
lines changed

3 files changed

+31
-41
lines changed

firebase.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
"port": 5001
4141
},
4242
"firestore": {
43-
"port": 8080
43+
"port": 8090
4444
},
4545
"hosting": {
4646
"port": 5010

firestore.rules

Lines changed: 21 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,34 @@
11
rules_version = '2';
2-
32
service cloud.firestore {
43
match /databases/{database}/documents {
5-
// Helper functions
6-
function isAuthenticated() {
7-
return request.auth != null;
8-
}
9-
10-
function isOwner(userId) {
11-
return isAuthenticated() && request.auth.uid == userId;
4+
// Base rule - deny access by default
5+
match /{document=**} {
6+
allow read, write: if false;
127
}
138

14-
// Users collection
9+
// User data - allow users to access their own data
1510
match /users/{userId} {
16-
// Users can read and update their own profiles
17-
allow read, update: if isOwner(userId);
18-
// Only allow creation through Cloud Functions (triggered by Auth)
19-
allow create: if false;
20-
// Only allow deletion through Cloud Functions
21-
allow delete: if false;
11+
allow read, write: if request.auth != null && request.auth.uid == userId;
2212

23-
// Add rules for documents subcollection
24-
match /documents/{documentType} {
25-
allow read: if isOwner(userId);
26-
// Allow Cloud Functions to write to this collection
27-
allow write: if false;
13+
// Allow access to documents subcollection
14+
match /documents/{documentId} {
15+
allow read, write: if request.auth != null && request.auth.uid == userId;
2816
}
2917

30-
// Add rules for predictions subcollection
18+
// Allow access to data subcollection
19+
match /data/{dataId} {
20+
allow read, write: if request.auth != null && request.auth.uid == userId;
21+
}
22+
23+
// Allow access to predictions subcollection
3124
match /predictions/{predictionId} {
32-
allow read: if isOwner(userId);
33-
allow write: if false;
25+
allow read, write: if request.auth != null && request.auth.uid == userId;
26+
}
27+
28+
// Allow access to calculations subcollection
29+
match /calculations/{calculationId} {
30+
allow read, write: if request.auth != null && request.auth.uid == userId;
3431
}
35-
}
36-
37-
// Courses collection - will be implemented later
38-
match /courses/{courseId} {
39-
// Users can read courses they're enrolled in
40-
allow read: if isAuthenticated() &&
41-
exists(/databases/$(database)/documents/users/$(request.auth.uid)/courses/$(courseId));
42-
// Write operations will be handled by Cloud Functions
43-
allow write: if false;
44-
}
45-
46-
// Default deny all
47-
match /{document=**} {
48-
allow read, write: if false;
4932
}
5033
}
51-
}
34+
}

functions-node/formatDocumentsData.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const OpenAI = require('openai');
22
const functions = require('firebase-functions');
33
const admin = require('firebase-admin');
44
const { DOCUMENT_TYPES, normalizeDocumentType } = require('./constants/documentTypes');
5+
const { FieldValue } = require('firebase-admin/firestore');
56

67
/**
78
* Formats all document data using a single OpenAI API call to ensure consistent structure
@@ -140,6 +141,12 @@ exports.formatDocumentsData = async (userId, force = false) => {
140141
console.error('Error formatting data with OpenAI:', error);
141142
console.error(error.stack);
142143

144+
// Make sure documentsByType exists before using it
145+
if (typeof documentsByType === 'undefined') {
146+
console.log('documentsByType is undefined, creating empty object');
147+
return createFallbackFormattedData({});
148+
}
149+
143150
// Return a fallback format in case of error
144151
return createFallbackFormattedData(documentsByType);
145152
}
@@ -257,7 +264,7 @@ async function storeFormattedData(userId, formattedData) {
257264
try {
258265
await db.collection('users').doc(userId).collection('data').doc('formatted_data').set({
259266
formatted_data: formattedData,
260-
lastUpdated: admin.firestore.FieldValue.serverTimestamp()
267+
lastUpdated: FieldValue.serverTimestamp()
261268
});
262269

263270
console.log('Successfully stored formatted data');
@@ -296,7 +303,7 @@ async function updateDocumentStatus(userId, documents) {
296303
if (currentStatus === 'extracted') {
297304
batch.update(docRef, {
298305
status: 'processed',
299-
processedAt: admin.firestore.FieldValue.serverTimestamp()
306+
processedAt: FieldValue.serverTimestamp()
300307
});
301308
updateCount++;
302309
console.log(`Marking document ${doc.id} as processed (type: ${docData.documentType})`);

0 commit comments

Comments
 (0)