From 9cd11261f9401532a8b7f377ffbebabad8833087 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Sat, 19 Apr 2025 11:15:07 +0800 Subject: [PATCH] [API Node] Set auth persistence in local stoarge (#3514) --- src/stores/firebaseAuthStore.ts | 5 +++++ tests-ui/tests/store/firebaseAuthStore.test.ts | 11 ++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/stores/firebaseAuthStore.ts b/src/stores/firebaseAuthStore.ts index 89924dba8..57456e239 100644 --- a/src/stores/firebaseAuthStore.ts +++ b/src/stores/firebaseAuthStore.ts @@ -4,8 +4,10 @@ import { GoogleAuthProvider, type User, type UserCredential, + browserLocalPersistence, createUserWithEmailAndPassword, onAuthStateChanged, + setPersistence, signInWithEmailAndPassword, signInWithPopup, signOut @@ -33,6 +35,9 @@ export const useFirebaseAuthStore = defineStore('firebaseAuth', () => { // Get auth from VueFire and listen for auth state changes const auth = useFirebaseAuth() if (auth) { + // Set persistence to localStorage (works in both browser and Electron) + void setPersistence(auth, browserLocalPersistence) + onAuthStateChanged(auth, (user) => { currentUser.value = user isInitialized.value = true diff --git a/tests-ui/tests/store/firebaseAuthStore.test.ts b/tests-ui/tests/store/firebaseAuthStore.test.ts index 1050b2e58..c8c80cd95 100644 --- a/tests-ui/tests/store/firebaseAuthStore.test.ts +++ b/tests-ui/tests/store/firebaseAuthStore.test.ts @@ -16,7 +16,9 @@ vi.mock('firebase/auth', () => ({ onAuthStateChanged: vi.fn(), signInWithPopup: vi.fn(), GoogleAuthProvider: vi.fn(), - GithubAuthProvider: vi.fn() + GithubAuthProvider: vi.fn(), + browserLocalPersistence: 'browserLocalPersistence', + setPersistence: vi.fn().mockResolvedValue(undefined) })) describe('useFirebaseAuthStore', () => { @@ -64,6 +66,13 @@ describe('useFirebaseAuthStore', () => { expect(store.error).toBe(null) }) + it('should set persistence to local storage on initialization', () => { + expect(firebaseAuth.setPersistence).toHaveBeenCalledWith( + mockAuth, + firebaseAuth.browserLocalPersistence + ) + }) + it('should properly clean up error state between operations', async () => { // First, cause an error const mockError = new Error('Invalid password')