Skip to content

Commit 9cd1126

Browse files
[API Node] Set auth persistence in local stoarge (#3514)
1 parent fbc6665 commit 9cd1126

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/stores/firebaseAuthStore.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import {
44
GoogleAuthProvider,
55
type User,
66
type UserCredential,
7+
browserLocalPersistence,
78
createUserWithEmailAndPassword,
89
onAuthStateChanged,
10+
setPersistence,
911
signInWithEmailAndPassword,
1012
signInWithPopup,
1113
signOut
@@ -33,6 +35,9 @@ export const useFirebaseAuthStore = defineStore('firebaseAuth', () => {
3335
// Get auth from VueFire and listen for auth state changes
3436
const auth = useFirebaseAuth()
3537
if (auth) {
38+
// Set persistence to localStorage (works in both browser and Electron)
39+
void setPersistence(auth, browserLocalPersistence)
40+
3641
onAuthStateChanged(auth, (user) => {
3742
currentUser.value = user
3843
isInitialized.value = true

tests-ui/tests/store/firebaseAuthStore.test.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ vi.mock('firebase/auth', () => ({
1616
onAuthStateChanged: vi.fn(),
1717
signInWithPopup: vi.fn(),
1818
GoogleAuthProvider: vi.fn(),
19-
GithubAuthProvider: vi.fn()
19+
GithubAuthProvider: vi.fn(),
20+
browserLocalPersistence: 'browserLocalPersistence',
21+
setPersistence: vi.fn().mockResolvedValue(undefined)
2022
}))
2123

2224
describe('useFirebaseAuthStore', () => {
@@ -64,6 +66,13 @@ describe('useFirebaseAuthStore', () => {
6466
expect(store.error).toBe(null)
6567
})
6668

69+
it('should set persistence to local storage on initialization', () => {
70+
expect(firebaseAuth.setPersistence).toHaveBeenCalledWith(
71+
mockAuth,
72+
firebaseAuth.browserLocalPersistence
73+
)
74+
})
75+
6776
it('should properly clean up error state between operations', async () => {
6877
// First, cause an error
6978
const mockError = new Error('Invalid password')

0 commit comments

Comments
 (0)