|
1 | 1 | import * as Sentry from '@sentry/browser';
|
2 | 2 | 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'; |
4 | 10 | import * as React from 'react';
|
5 | 11 | import { createContext, ReactNode, useReducer, useState } from 'react';
|
6 | 12 | import toast from 'react-hot-toast';
|
@@ -275,23 +281,27 @@ export const UserDataProvider = ({
|
275 | 281 | const localDataIsNotEmpty = lastViewedModule !== null;
|
276 | 282 |
|
277 | 283 | 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) => { |
287 | 289 | return {
|
288 | 290 | ...acc,
|
289 | 291 | ...cur.exportValue(),
|
290 | 292 | };
|
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 | + ); |
295 | 305 | }
|
296 | 306 | }
|
297 | 307 | data = data || {};
|
|
0 commit comments