Migrate firebase to npm package

This commit is contained in:
Andrei Andreev 2022-02-06 20:22:06 +03:00
parent ec57c3527d
commit dbf1e6f70a
4 changed files with 706 additions and 84 deletions

View File

@ -1,6 +1,9 @@
import pako from "pako/dist/pako.esm.mjs";
import { decodeBase64Binary } from "./base64-binary";
import { ProgressChecker } from "./progress-checker.js";
import { initializeApp } from "firebase/app";
import { getAuth, signInWithPopup, signOut, GoogleAuthProvider } from "firebase/auth";
import { getDatabase, ref, get, set } from "firebase/database";
const firebaseConfig = {
apiKey: "AIzaSyDuRTTluAFufmvw1zxGH6fsyEHmmbu8IHI",
@ -12,12 +15,12 @@ const firebaseConfig = {
appId: "1:904798020003:web:d1448dcb2dedd8b5",
};
firebase.initializeApp(firebaseConfig);
initializeApp(firebaseConfig);
export const Cloud = {
provider: new firebase.auth.GoogleAuthProvider(),
auth: firebase.auth(),
db: firebase.database(),
provider: new GoogleAuthProvider(),
auth: getAuth(),
db: getDatabase(),
user: null,
hasSeenSavingConflict: false,
shouldOverwriteCloudSave: true,
@ -27,12 +30,12 @@ export const Cloud = {
},
async login() {
await this.auth.signInWithPopup(this.provider);
await signInWithPopup(this.auth, this.provider);
},
async loadMobile() {
if (!this.user) return;
const snapshot = await this.db.ref(`users/${this.user.id}/player`).get();
const snapshot = await get(ref(this.db, `users/${this.user.id}/player`));
if (snapshot.exists) {
const encoded = snapshot.val();
const uintArray = decodeBase64Binary(encoded.replace(/-/gu, "+").replace(/_/gu, "/"));
@ -83,7 +86,7 @@ export const Cloud = {
saves: GameStorage.saves,
};
this.db.ref(`users/${this.user.id}/web`).set(GameSaveSerializer.serialize(root));
set(ref(this.db, `users/${this.user.id}/web`), GameSaveSerializer.serialize(root));
GameUI.notify.info(`Game saved (slot ${slot + 1}) to cloud with user ${this.user.displayName}`);
},
@ -119,18 +122,18 @@ export const Cloud = {
},
async load() {
const snapshot = await this.db.ref(`users/${this.user.id}/web`).get();
const snapshot = await get(ref(this.db, `users/${this.user.id}/web`));
if (snapshot.exists) return snapshot.val();
return null;
},
logout() {
this.auth.signOut();
signOut(this.auth);
},
init() {
firebase.auth().onAuthStateChanged(user => {
getAuth().onAuthStateChanged(user => {
if (user) {
this.user = {
id: user.uid,

761
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -11,6 +11,7 @@
"chevrotain": "^4.8.1",
"core-js": "^3.6.5",
"drag-drop-touch": "^1.3.1",
"firebase": "^9.6.6",
"gamma": "^1.0.0",
"js-sha512": "^0.8.0",
"lz-string": "^1.4.4",

View File

@ -59,11 +59,6 @@
</div>
<div id="performance-stats" class="c-performance-stats" style="display: none;"></div>
<!-- Here's some firebase stuff -->
<script src="https://www.gstatic.com/firebasejs/8.8.0/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.8.0/firebase-auth.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.8.0/firebase-database.js"></script>
<script type="text/javascript" src="lib/vue.js"></script>
<script type="text/javascript" src="lib/codemirror.js"></script>
<script type="text/javascript" src="lib/simple.js"></script>