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

@ -1,69 +0,0 @@
import {
BareClient,
registerRemoteListener,
setBareClientImplementation,
} from "bare-client-custom";
import { RTCTransport } from "./RTCTransport";
//import "./firebase";
import { AdriftBareClient } from "./AdriftClient";
import Connection from "./Connection";
import { DevWsTransport } from "./DevWsTransport";
const rtcEnable = false;
let rtc;
let connection;
if (rtcEnable) {
rtc = new RTCTransport(
console.log,
() => {
// rtc.dataChannel.send("test message");
// let client = new AdriftBareClient;
// setBareClientImplementation(client);
//
},
console.log,
console.log,
console.log
);
connection = new Connection(rtc);
} else {
connection = new Connection(
new DevWsTransport(
() => console.log("onopen"),
() => console.log("onclose")
)
);
}
window["co"] = connection;
// connection.httprequest({ a: 1, b: 2 });
let bare = new AdriftBareClient(connection);
setBareClientImplementation(bare);
registerRemoteListener();
// if (import.meta.env.VITE_APP_ENV === 'development') {
// if (rtcEnable) {
// let offer = await this.rtc.createOffer();
// console.log("offer created", offer);
// console.log(JSON.stringify(offer));
//
// const r = await fetch("http://localhost:3000/connect", {
// method: "POST",
// headers: { "Content-Type": "application/json" },
// body: JSON.stringify(offer),
// });
// if (r.status != 200) {
// throw new Error("connect: " + r.status + " " + r.statusText);
// }
// const { answer, candidates } = await r.json();
// await this.rtc.answer(answer, candidates);
// alert("connected");
// } else {
// window["bare"].fetch("https://httpbin.org/get");
// }
//

View file

@ -1,6 +1,7 @@
import "../firebase-config"; import "../firebase-config";
import { getDatabase, ref, onValue, set } from "firebase/database"; import { getDatabase, ref, onValue, set } from "firebase/database";
const db = getDatabase(); const db = getDatabase();
console.log(db); console.log(db);
let reff = ref(db, "/peers/demo"); let reff = ref(db, "/peers/demo");

View file

@ -16,7 +16,10 @@ const firebaseConfig = {
messagingSenderId: "175846512414", messagingSenderId: "175846512414",
appId: "1:175846512414:web:5c6e06d231ab58e9029b0f", appId: "1:175846512414:web:5c6e06d231ab58e9029b0f",
measurementId: "G-L0P2EF6Q72" measurementId: "G-L0P2EF6Q72"
}; };
console.warn("firebase is initializing");
// Initialize Firebase // Initialize Firebase
export const app = initializeApp(firebaseConfig); export const app = initializeApp(firebaseConfig);
export const auth = getAuth(app); export const auth = getAuth(app);

View file

@ -4,11 +4,15 @@
import { DevWsTransport } from "../client/DevWsTransport"; import { DevWsTransport } from "../client/DevWsTransport";
import { RTCTransport } from "../client/RTCTransport"; import { RTCTransport } from "../client/RTCTransport";
import type Transport from "../client/Transport"; 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 { import {
BareClient, BareClient,
registerRemoteListener, registerRemoteListener,
setBareClientImplementation, setBareClientImplementation,
} from "bare-client-custom"; } from "bare-client-custom";
import { signInWithEmailAndPassword } from "firebase/auth";
import { getDatabase, onValue, ref, set } from "firebase/database";
let transport: Transport; let transport: Transport;
@ -50,6 +54,38 @@
console.log("registering bare-client-custom"); console.log("registering bare-client-custom");
registerRemoteListener(); 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> </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>