diff --git a/.gitignore b/.gitignore index 36bbd5f..8908ab8 100644 --- a/.gitignore +++ b/.gitignore @@ -17,6 +17,9 @@ firestore.rules pnpm-lock.yaml package-lock.json +# emulators +.firebaserc + # Editor directories and files .vscode/* !.vscode/extensions.json diff --git a/src/composables/useElapsedTime.ts b/src/composables/useElapsedTime.ts index 04bc0c1..32899bb 100644 --- a/src/composables/useElapsedTime.ts +++ b/src/composables/useElapsedTime.ts @@ -12,6 +12,11 @@ export function useElapsedTime( const calculateElapsedTime = () => { let date; + if (!input) { + elapsedTime.value = "No date provided"; + return; + } + if (typeof input === "string") { date = new Date(input); } else { diff --git a/src/services/firebase/firebase.config.ts b/src/services/firebase/firebase.config.ts index be4f40f..74f5cc6 100644 --- a/src/services/firebase/firebase.config.ts +++ b/src/services/firebase/firebase.config.ts @@ -1,7 +1,7 @@ // Import the functions you need from the SDKs you need import { initializeApp } from "firebase/app"; import { getAnalytics } from "firebase/analytics"; -import { getFirestore } from "firebase/firestore"; +import { connectFirestoreEmulator, getFirestore } from "firebase/firestore"; import { getAuth, connectAuthEmulator } from "firebase/auth"; const firebaseConfig = { @@ -21,7 +21,8 @@ if (process.env.NODE_ENV !== "test") { export const db = getFirestore(app); export const auth = getAuth(app); -if (process.env.NODE_ENV === "test") { +if (process.env.NODE_ENV === "test" || process.env.NODE_ENV === "development") { connectAuthEmulator(auth, "http://localhost:9099"); + connectFirestoreEmulator(db, "localhost", 8080); } export default app; diff --git a/src/store/authStore.ts b/src/store/authStore.ts index 716319d..4ce4200 100644 --- a/src/store/authStore.ts +++ b/src/store/authStore.ts @@ -1,10 +1,11 @@ import { defineStore } from "pinia"; -import { onAuthStateChanged } from "firebase/auth"; +import { onAuthStateChanged, User } from "firebase/auth"; import { auth } from "../services/firebase/firebase.config"; export const useAuthStore = defineStore("auth", { state: () => ({ isLoggedIn: false, + user: null as User | null, }), actions: { @@ -14,11 +15,17 @@ export const useAuthStore = defineStore("auth", { } }, + getUserId(): string | null { + return this.user ? this.user.uid : null; + }, + listenToAuthChanges() { onAuthStateChanged(auth, async (user) => { if (user) { + this.user = user; await this.login(); } else { + this.user = null; this.logout(); } }); @@ -26,8 +33,7 @@ export const useAuthStore = defineStore("auth", { async login() { try { - const idToken = await auth.currentUser?.getIdToken(); - if (idToken) { + if (this.user) { this.isLoggedIn = true; } else { this.isLoggedIn = false; diff --git a/src/views/CreateSnippetView.vue b/src/views/CreateSnippetView.vue index 34cbe09..caeba9d 100644 --- a/src/views/CreateSnippetView.vue +++ b/src/views/CreateSnippetView.vue @@ -1,5 +1,5 @@