add firebase login and offering to the client

This commit is contained in:
CoolElectronics 2023-08-12 11:44:46 -04:00
parent d1bef00d93
commit 7315350858
No known key found for this signature in database
GPG key ID: F63593D168636C50
4 changed files with 41 additions and 70 deletions

View file

@ -4,11 +4,15 @@
import { DevWsTransport } from "../client/DevWsTransport";
import { RTCTransport } from "../client/RTCTransport";
import type Transport from "../client/Transport";
// note: even though we import firebase, due to the tree shaking, it will only run if we use "auth" so if ADRIFT_DEV is set it won't import
import { auth } from "../firebase-config";
import {
BareClient,
registerRemoteListener,
setBareClientImplementation,
} from "bare-client-custom";
import { signInWithEmailAndPassword } from "firebase/auth";
import { getDatabase, onValue, ref, set } from "firebase/database";
let transport: Transport;
@ -50,6 +54,38 @@
console.log("registering bare-client-custom");
registerRemoteListener();
}
async function connectFirebase() {
if (!rtctransport) return;
let creds = await signInWithEmailAndPassword(
auth,
"test@test.com",
"123456"
);
const db = getDatabase();
let peer = ref(db, `/peers/${creds.user.uid}`);
let offer = await rtctransport.createOffer();
set(peer, JSON.stringify(offer));
onValue(peer, (snapshot) => {
const data = snapshot.val();
console.log(data);
if (data && data.answer && data.candidates) {
set(peer, null);
const { answer, candidates } = JSON.parse(data);
rtctransport?.answer(answer, candidates);
}
});
}
</script>
<h1>testa</h1>
<h1>
{#if !import.meta.env.VITE_ADRIFT_DEV}
<button on:click={connectFirebase}>Connect with firebase </button>
{:else}
connected to dev server
{/if}
</h1>