Skip to content

Commit 230a582

Browse files
prevent accidental user data override (#3241)
1 parent 2f81c09 commit 230a582

File tree

3 files changed

+29
-14
lines changed

3 files changed

+29
-14
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,5 @@ build.css
8686

8787
**/*.icloud
8888
*.icloud
89+
90+
.runtimeconfig.json

firebase.json

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
],
77
"source": "src/functions"
88
},
9+
"firestore": {
10+
"rules": "firestore.rules"
11+
},
912
"emulators": {
1013
"auth": {
1114
"port": 9099

src/context/UserDataContext/UserDataContext.tsx

+24-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import * as Sentry from '@sentry/browser';
22
import { getAuth, onAuthStateChanged, signOut, User } from 'firebase/auth';
3-
import { doc, getFirestore, onSnapshot, setDoc } from 'firebase/firestore';
3+
import {
4+
doc,
5+
getFirestore,
6+
onSnapshot,
7+
serverTimestamp,
8+
setDoc,
9+
} from 'firebase/firestore';
410
import * as React from 'react';
511
import { createContext, ReactNode, useReducer, useState } from 'react';
612
import toast from 'react-hot-toast';
@@ -275,23 +281,27 @@ export const UserDataProvider = ({
275281
const localDataIsNotEmpty = lastViewedModule !== null;
276282

277283
if (localDataIsNotEmpty) {
278-
if (
279-
confirm(
280-
`Override server data with local progress? (You'll lose your local progress if you choose no.)`
281-
)
282-
) {
283-
// sync all local data with firebase if the firebase account doesn't exist yet
284-
setDoc(
285-
userDoc,
286-
UserDataContextAPIs.reduce((acc, cur) => {
284+
// sync all local data with firebase if the firebase account doesn't exist yet
285+
setDoc(
286+
userDoc,
287+
UserDataContextAPIs.reduce(
288+
(acc, cur) => {
287289
return {
288290
...acc,
289291
...cur.exportValue(),
290292
};
291-
}, {}),
292-
{ merge: true }
293-
);
294-
}
293+
},
294+
{
295+
// this is to prevent us from accidentally overriding the user data
296+
// firebase security rules will have a check to make sure that this is actually the first time
297+
// the user has logged in. occasionally, with poor internet, firebase will glitch and
298+
// we will accidentally override user data.
299+
// see https://github.com/cpinitiative/usaco-guide/issues/534
300+
CREATING_ACCOUNT_FOR_FIRST_TIME: serverTimestamp(),
301+
}
302+
),
303+
{ merge: true }
304+
);
295305
}
296306
}
297307
data = data || {};

0 commit comments

Comments
 (0)