-
-
+{#if loading}
+
+ Loading...
+
+{:else}
+
+ {#if screen === 'worldmap'}
+
-
-
- {:else}
-
+ {:else}
+
+ {/if}
+
+
+ {#if !user}
+
+ {:else if needsCountry}
+
{/if}
-
+{/if}
diff --git a/src/lib/auth/LoginOverlay.svelte b/src/lib/auth/LoginOverlay.svelte
new file mode 100644
index 0000000..18ea4d0
--- /dev/null
+++ b/src/lib/auth/LoginOverlay.svelte
@@ -0,0 +1,87 @@
+
+
+
+
+

+
Map Journal
+
Sign in to start your journey
+
+
+
+
+
diff --git a/src/lib/auth/userStore.svelte.js b/src/lib/auth/userStore.svelte.js
new file mode 100644
index 0000000..329967b
--- /dev/null
+++ b/src/lib/auth/userStore.svelte.js
@@ -0,0 +1,72 @@
+import { auth, db, googleProvider } from '../firebase.js';
+import { onAuthStateChanged, signInWithPopup, signOut as fbSignOut } from 'firebase/auth';
+import { doc, getDoc, setDoc, serverTimestamp } from 'firebase/firestore';
+import { initSelectionListener } from '../layout/selection.svelte.js';
+import { initEntriesListener } from '../stores/entriesStore.svelte.js';
+
+let _initialized = false;
+
+let user = $state(null);
+let userProfile = $state(null);
+let loading = $state(true);
+let needsCountry = $state(false);
+
+export function getUser() { return user; }
+export function getUserProfile() { return userProfile; }
+export function getLoading() { return loading; }
+export function getNeedsCountry() { return needsCountry; }
+
+export async function signInWithGoogle() {
+ await signInWithPopup(auth, googleProvider);
+}
+
+export async function signOut() {
+ await fbSignOut(auth);
+ user = null;
+ userProfile = null;
+ needsCountry = false;
+}
+
+export async function setHomeCountry(country) {
+ if (!user) return;
+ await setDoc(doc(db, 'users', user.uid), {
+ displayName: user.displayName,
+ photoURL: user.photoURL,
+ email: user.email,
+ homeCountry: country,
+ visitedCountries: [],
+ createdAt: serverTimestamp(),
+ });
+ userProfile = { ...userProfile, homeCountry: country, visitedCountries: [] };
+ needsCountry = false;
+}
+
+export function initAuth() {
+ if (_initialized) return;
+ _initialized = true;
+ onAuthStateChanged(auth, async (fbUser) => {
+ if (fbUser) {
+ user = fbUser;
+ initSelectionListener(fbUser.uid);
+ initEntriesListener(fbUser.uid);
+ const docRef = doc(db, 'users', fbUser.uid);
+ const docSnap = await getDoc(docRef);
+ if (docSnap.exists()) {
+ userProfile = docSnap.data();
+ needsCountry = false;
+ } else {
+ userProfile = {
+ displayName: fbUser.displayName,
+ photoURL: fbUser.photoURL,
+ email: fbUser.email,
+ };
+ needsCountry = true;
+ }
+ } else {
+ user = null;
+ userProfile = null;
+ needsCountry = false;
+ }
+ loading = false;
+ });
+}
diff --git a/src/lib/firebase.js b/src/lib/firebase.js
new file mode 100644
index 0000000..758573c
--- /dev/null
+++ b/src/lib/firebase.js
@@ -0,0 +1,18 @@
+import { initializeApp } from "firebase/app";
+import { getAuth, GoogleAuthProvider } from "firebase/auth";
+import { getFirestore } from "firebase/firestore";
+
+const firebaseConfig = {
+ apiKey: import.meta.env.VITE_FIREBASE_API_KEY,
+ authDomain: import.meta.env.VITE_FIREBASE_AUTH_DOMAIN,
+ projectId: import.meta.env.VITE_FIREBASE_PROJECT_ID,
+ storageBucket: import.meta.env.VITE_FIREBASE_STORAGE_BUCKET,
+ messagingSenderId: import.meta.env.VITE_FIREBASE_MESSAGING_SENDER_ID,
+ appId: import.meta.env.VITE_FIREBASE_APP_ID,
+};
+
+const app = initializeApp(firebaseConfig);
+
+export const auth = getAuth(app);
+export const db = getFirestore(app);
+export const googleProvider = new GoogleAuthProvider();
diff --git a/src/lib/layout/TopBar.svelte b/src/lib/layout/TopBar.svelte
index f700cb7..e080f43 100644
--- a/src/lib/layout/TopBar.svelte
+++ b/src/lib/layout/TopBar.svelte
@@ -1,5 +1,21 @@
@@ -20,10 +36,34 @@
-

+ {#if user}
+
+
+ {#if menuOpen}
+
+ {/if}
+
+ {/if}
+{#if menuOpen}
+