auth persistence

This commit is contained in:
CoolElectronics 2023-08-19 20:00:56 -04:00
parent 5e7cb49361
commit 2476f9259a
No known key found for this signature in database
GPG key ID: F63593D168636C50
2 changed files with 37 additions and 17 deletions

View file

@ -3,7 +3,7 @@ import { getDatabase, onValue, ref, set, remove } from "firebase/database";
import { v4 as uuid } from "uuid"; import { v4 as uuid } from "uuid";
import { Answer } from "./RTCTransport"; import { Answer } from "./RTCTransport";
import { getAuth, signInWithEmailAndPassword } from "firebase/auth"; import { browserLocalPersistence, getAuth, setPersistence, signInWithEmailAndPassword } from "firebase/auth";
export async function signalSwarm(offer: string): Promise<Answer> { export async function signalSwarm(offer: string): Promise<Answer> {
@ -40,13 +40,16 @@ export async function signalSwarm(offer: string): Promise<Answer> {
}); });
} }
export async function signalAccount(offer: string, email: string, password: string): Promise<Answer> { export async function signalAccount(offer: string): Promise<Answer> {
let auth = getAuth(); let auth = getAuth();
let creds = await signInWithEmailAndPassword(auth, email, password);
if (!auth.currentUser)
throw new Error("not signed in");
const db = getDatabase(); const db = getDatabase();
let peer = ref(db, `/peers/${creds.user.uid}`); let peer = ref(db, `/peers/${auth.currentUser!.uid}`);

View file

@ -13,8 +13,6 @@
CircularProgressIndeterminate, CircularProgressIndeterminate,
Dialog, Dialog,
RadioAnim3, RadioAnim3,
SegmentedButtonContainer,
SegmentedButtonItem,
StyleFromScheme, StyleFromScheme,
TextField, TextField,
} from "m3-svelte"; } from "m3-svelte";
@ -30,6 +28,12 @@
import { initializeApp } from "firebase/app"; import { initializeApp } from "firebase/app";
import TrackerList from "tracker-list"; import TrackerList from "tracker-list";
import {
browserLocalPersistence,
getAuth,
setPersistence,
signInWithEmailAndPassword,
} from "firebase/auth";
enum ReadyState { enum ReadyState {
Idle, Idle,
@ -43,8 +47,8 @@
let rtctransport: RTCTransport | undefined; let rtctransport: RTCTransport | undefined;
let email = "test@test.com"; let email = "";
let password = "123456"; let password = "";
let connectionState = ""; let connectionState = "";
@ -98,17 +102,12 @@
} }
async function connectAccount() { async function connectAccount() {
await initFirebase();
rtctransport = transport = createRTCTransport(); rtctransport = transport = createRTCTransport();
state = ReadyState.Connecting; state = ReadyState.Connecting;
let offer = await rtctransport.createOffer(); let offer = await rtctransport.createOffer();
connectionState = "Finding your node..."; connectionState = "Finding your node...";
let answer = await SignalFirebase.signalAccount( let answer = await SignalFirebase.signalAccount(JSON.stringify(offer));
JSON.stringify(offer),
email,
password
);
connectionState = "Linking to node..."; connectionState = "Linking to node...";
await new Promise((r) => { await new Promise((r) => {
setTimeout(r, 1000); setTimeout(r, 1000);
@ -280,8 +279,20 @@
<Button type="elevated" on:click={() => (showSwarmWarning = true)} <Button type="elevated" on:click={() => (showSwarmWarning = true)}
>Connect to the swarm</Button >Connect to the swarm</Button
> >
<Button type="filled" on:click={() => (showLogin = true)} <Button
>Connect with login</Button type="filled"
on:click={async () => {
await initFirebase();
let auth = getAuth();
await setPersistence(auth, browserLocalPersistence);
if (!auth.currentUser) {
showLogin = true;
} else {
await connectAccount();
}
}}>Connect with login</Button
> >
{/if} {/if}
</div> </div>
@ -319,7 +330,13 @@
<Button type="outlined" on:click={() => (showLogin = false)} <Button type="outlined" on:click={() => (showLogin = false)}
>Cancel</Button >Cancel</Button
> >
<Button type="filled" on:click={connectAccount}>Connect</Button> <Button
type="filled"
on:click={async () => {
await signInWithEmailAndPassword(getAuth(), email, password);
connectAccount();
}}>Connect</Button
>
</div> </div>
</Dialog> </Dialog>
</Card> </Card>