svelte-firekit 0.1.1 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/auth-guard.svelte +48 -17
- package/dist/components/custom-guard.svelte +72 -31
- package/dist/firebase.js +15 -3
- package/dist/services/auth.js +19 -9
- package/package.json +2 -2
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
2
|
import { firekitAuth } from '../services/auth.js';
|
|
3
|
-
import {
|
|
3
|
+
import { getContext } from 'svelte';
|
|
4
4
|
import { goto } from '$app/navigation';
|
|
5
|
-
import { onDestroy } from 'svelte';
|
|
5
|
+
import { onMount, onDestroy } from 'svelte';
|
|
6
6
|
import type { UserProfile } from '../types/auth.js';
|
|
7
7
|
import type { Auth } from 'firebase/auth';
|
|
8
8
|
import type { Snippet } from 'svelte';
|
|
@@ -36,27 +36,51 @@
|
|
|
36
36
|
fallback?: Snippet<[]>;
|
|
37
37
|
} = $props();
|
|
38
38
|
|
|
39
|
-
//
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
throw new Error('Firebase Auth instance not available');
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
// Reactive auth state
|
|
39
|
+
// Try to get Firebase Auth from context first, fallback to service
|
|
40
|
+
let auth: Auth | null = $state(null);
|
|
41
|
+
let unsubscribe: (() => void) | null = null;
|
|
46
42
|
let authState = $state(firekitAuth.getState());
|
|
47
43
|
|
|
48
|
-
// Subscribe to auth state changes
|
|
49
|
-
const unsubscribe = firekitAuth.onAuthStateChanged((state) => {
|
|
50
|
-
authState = state;
|
|
51
|
-
});
|
|
52
|
-
|
|
53
44
|
// Sign out function
|
|
54
45
|
async function signOut() {
|
|
55
46
|
await firekitAuth.signOut();
|
|
56
47
|
}
|
|
57
48
|
|
|
49
|
+
onMount(async () => {
|
|
50
|
+
try {
|
|
51
|
+
// Try to get auth from context first
|
|
52
|
+
auth = getContext<Auth>('firebase/auth');
|
|
53
|
+
|
|
54
|
+
// If context doesn't exist, get from service
|
|
55
|
+
if (!auth) {
|
|
56
|
+
console.warn('Firebase Auth not found in context, using service directly');
|
|
57
|
+
const { firebaseService } = await import('../firebase.js');
|
|
58
|
+
auth = firebaseService.getAuthInstance();
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (!auth) {
|
|
62
|
+
throw new Error('Firebase Auth instance not available');
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Subscribe to auth state changes
|
|
66
|
+
unsubscribe = firekitAuth.onAuthStateChanged((state) => {
|
|
67
|
+
authState = state;
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
// Initial auth check
|
|
71
|
+
checkAuthState();
|
|
72
|
+
} catch (error) {
|
|
73
|
+
console.error('Failed to initialize AuthGuard:', error);
|
|
74
|
+
authState = {
|
|
75
|
+
user: null,
|
|
76
|
+
loading: false,
|
|
77
|
+
initialized: true
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
|
|
58
82
|
// Check if current auth state matches requirements
|
|
59
|
-
|
|
83
|
+
function checkAuthState() {
|
|
60
84
|
if (authState.loading) return;
|
|
61
85
|
|
|
62
86
|
const isAuthenticated = firekitAuth.isAuthenticated();
|
|
@@ -65,11 +89,18 @@
|
|
|
65
89
|
if (shouldRedirect) {
|
|
66
90
|
goto(redirectTo);
|
|
67
91
|
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Watch for auth state changes
|
|
95
|
+
$effect(() => {
|
|
96
|
+
checkAuthState();
|
|
68
97
|
});
|
|
69
98
|
|
|
70
99
|
// Cleanup subscription on component destruction
|
|
71
100
|
onDestroy(() => {
|
|
72
|
-
unsubscribe
|
|
101
|
+
if (unsubscribe) {
|
|
102
|
+
unsubscribe();
|
|
103
|
+
}
|
|
73
104
|
});
|
|
74
105
|
</script>
|
|
75
106
|
|
|
@@ -84,6 +115,6 @@
|
|
|
84
115
|
</div>
|
|
85
116
|
</div>
|
|
86
117
|
{/if}
|
|
87
|
-
{:else if firekitAuth.isAuthenticated() === requireAuth}
|
|
118
|
+
{:else if auth && firekitAuth.isAuthenticated() === requireAuth}
|
|
88
119
|
{@render children(authState.user!, auth, signOut)}
|
|
89
120
|
{/if}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
2
|
import { firekitAuth } from '../services/auth.js';
|
|
3
|
-
import {
|
|
3
|
+
import { getContext } from 'svelte';
|
|
4
4
|
import { goto } from '$app/navigation';
|
|
5
|
-
import { onDestroy } from 'svelte';
|
|
5
|
+
import { onMount, onDestroy } from 'svelte';
|
|
6
6
|
import type { UserProfile } from '../types/auth.js';
|
|
7
7
|
import type { Auth } from 'firebase/auth';
|
|
8
8
|
import type { Snippet } from 'svelte';
|
|
@@ -42,29 +42,37 @@
|
|
|
42
42
|
verificationChecks?: ((user: UserProfile, auth: Auth) => boolean | Promise<boolean>)[];
|
|
43
43
|
} = $props();
|
|
44
44
|
|
|
45
|
-
//
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
throw new Error('Firebase Auth instance not available');
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
// Reactive auth state
|
|
45
|
+
// Try to get Firebase Auth from context first, fallback to service
|
|
46
|
+
let auth: Auth | null = $state(null);
|
|
47
|
+
let unsubscribe: (() => void) | null = null;
|
|
52
48
|
let authState = $state(firekitAuth.getState());
|
|
53
49
|
let verificationPassed = $state(true);
|
|
54
50
|
let isVerifying = $state(false);
|
|
55
51
|
|
|
56
|
-
// Subscribe to auth state changes
|
|
57
|
-
const unsubscribe = firekitAuth.onAuthStateChanged((state) => {
|
|
58
|
-
authState = state;
|
|
59
|
-
});
|
|
60
|
-
|
|
61
52
|
// Sign out function
|
|
62
53
|
async function signOut() {
|
|
63
54
|
await firekitAuth.signOut();
|
|
64
55
|
}
|
|
65
56
|
|
|
57
|
+
// Check verification functions
|
|
58
|
+
async function runVerificationChecks(): Promise<boolean> {
|
|
59
|
+
if (!auth || !authState.user || verificationChecks.length === 0) {
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
try {
|
|
64
|
+
const results = await Promise.all(
|
|
65
|
+
verificationChecks.map((check) => check(authState.user!, auth!))
|
|
66
|
+
);
|
|
67
|
+
return results.every((result) => result === true);
|
|
68
|
+
} catch (error) {
|
|
69
|
+
console.error('Verification check failed:', error);
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
66
74
|
// Check if current auth state and verification checks pass
|
|
67
|
-
|
|
75
|
+
async function checkAccess() {
|
|
68
76
|
if (authState.loading) return;
|
|
69
77
|
|
|
70
78
|
const isAuthenticated = firekitAuth.isAuthenticated();
|
|
@@ -78,29 +86,62 @@
|
|
|
78
86
|
// If authenticated and verification checks exist, run them
|
|
79
87
|
if (isAuthenticated && verificationChecks.length > 0) {
|
|
80
88
|
isVerifying = true;
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
if (!verificationPassed) {
|
|
85
|
-
goto(redirectTo);
|
|
86
|
-
}
|
|
87
|
-
})
|
|
88
|
-
.catch((error) => {
|
|
89
|
-
console.error('Verification check failed:', error);
|
|
90
|
-
verificationPassed = false;
|
|
89
|
+
try {
|
|
90
|
+
verificationPassed = await runVerificationChecks();
|
|
91
|
+
if (!verificationPassed) {
|
|
91
92
|
goto(redirectTo);
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
93
|
+
}
|
|
94
|
+
} finally {
|
|
95
|
+
isVerifying = false;
|
|
96
|
+
}
|
|
96
97
|
} else {
|
|
97
98
|
verificationPassed = true;
|
|
98
99
|
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
onMount(async () => {
|
|
103
|
+
try {
|
|
104
|
+
// Try to get auth from context first
|
|
105
|
+
auth = getContext<Auth>('firebase/auth');
|
|
106
|
+
|
|
107
|
+
// If context doesn't exist, get from service
|
|
108
|
+
if (!auth) {
|
|
109
|
+
console.warn('Firebase Auth not found in context, using service directly');
|
|
110
|
+
const { firebaseService } = await import('../firebase.js');
|
|
111
|
+
auth = firebaseService.getAuthInstance();
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (!auth) {
|
|
115
|
+
throw new Error('Firebase Auth instance not available');
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// Subscribe to auth state changes
|
|
119
|
+
unsubscribe = firekitAuth.onAuthStateChanged((state) => {
|
|
120
|
+
authState = state;
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
// Initial access check
|
|
124
|
+
await checkAccess();
|
|
125
|
+
} catch (error) {
|
|
126
|
+
console.error('Failed to initialize CustomGuard:', error);
|
|
127
|
+
authState = {
|
|
128
|
+
user: null,
|
|
129
|
+
loading: false,
|
|
130
|
+
initialized: true
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
// Watch for auth state changes
|
|
136
|
+
$effect(() => {
|
|
137
|
+
checkAccess();
|
|
99
138
|
});
|
|
100
139
|
|
|
101
140
|
// Cleanup subscription on component destruction
|
|
102
141
|
onDestroy(() => {
|
|
103
|
-
unsubscribe
|
|
142
|
+
if (unsubscribe) {
|
|
143
|
+
unsubscribe();
|
|
144
|
+
}
|
|
104
145
|
});
|
|
105
146
|
</script>
|
|
106
147
|
|
|
@@ -117,6 +158,6 @@
|
|
|
117
158
|
</div>
|
|
118
159
|
</div>
|
|
119
160
|
{/if}
|
|
120
|
-
{:else if firekitAuth.isAuthenticated() === requireAuth && verificationPassed}
|
|
161
|
+
{:else if auth && firekitAuth.isAuthenticated() === requireAuth && verificationPassed}
|
|
121
162
|
{@render children(authState.user!, auth, signOut)}
|
|
122
163
|
{/if}
|
package/dist/firebase.js
CHANGED
|
@@ -129,9 +129,21 @@ class FirebaseService {
|
|
|
129
129
|
*/
|
|
130
130
|
getDbInstance() {
|
|
131
131
|
if (!this.db) {
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
132
|
+
try {
|
|
133
|
+
this.getFirebaseApp();
|
|
134
|
+
if (!this.db) {
|
|
135
|
+
// If we're not in a browser environment, Firestore won't be available
|
|
136
|
+
if (!this.isBrowser) {
|
|
137
|
+
throw new FirebaseServiceError('Firestore is not available in server environment', 'firestore');
|
|
138
|
+
}
|
|
139
|
+
throw new FirebaseServiceError('Firestore instance not available', 'firestore');
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
catch (error) {
|
|
143
|
+
if (error instanceof FirebaseServiceError) {
|
|
144
|
+
throw error;
|
|
145
|
+
}
|
|
146
|
+
throw new FirebaseServiceError('Failed to initialize Firestore', 'firestore');
|
|
135
147
|
}
|
|
136
148
|
}
|
|
137
149
|
return this.db;
|
package/dist/services/auth.js
CHANGED
|
@@ -65,7 +65,14 @@ class FirekitAuth {
|
|
|
65
65
|
return;
|
|
66
66
|
try {
|
|
67
67
|
this.auth = firebaseService.getAuthInstance();
|
|
68
|
-
|
|
68
|
+
// Try to get Firestore instance, but don't fail if it's not available
|
|
69
|
+
try {
|
|
70
|
+
this.firestore = firebaseService.getDbInstance();
|
|
71
|
+
}
|
|
72
|
+
catch (firestoreError) {
|
|
73
|
+
console.warn('Firestore not available, continuing without Firestore integration:', firestoreError);
|
|
74
|
+
this.firestore = null;
|
|
75
|
+
}
|
|
69
76
|
this._servicesInitialized = true;
|
|
70
77
|
this.initializeAuthStateListener();
|
|
71
78
|
}
|
|
@@ -125,7 +132,8 @@ class FirekitAuth {
|
|
|
125
132
|
*/
|
|
126
133
|
async updateUserInFirestore(user) {
|
|
127
134
|
if (!this.firestore) {
|
|
128
|
-
|
|
135
|
+
console.warn('Firestore not available, skipping user update in Firestore');
|
|
136
|
+
return;
|
|
129
137
|
}
|
|
130
138
|
await updateUserInFirestore(this.firestore, user);
|
|
131
139
|
}
|
|
@@ -735,13 +743,15 @@ class FirekitAuth {
|
|
|
735
743
|
if (currentPassword) {
|
|
736
744
|
await this.reauthenticateUser(currentPassword);
|
|
737
745
|
}
|
|
738
|
-
// Delete user data from Firestore first
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
746
|
+
// Delete user data from Firestore first (if available)
|
|
747
|
+
if (this.firestore) {
|
|
748
|
+
try {
|
|
749
|
+
const userRef = doc(this.firestore, 'users', user.uid);
|
|
750
|
+
await setDoc(userRef, { deleted: true, deletedAt: serverTimestamp() }, { merge: true });
|
|
751
|
+
}
|
|
752
|
+
catch (firestoreError) {
|
|
753
|
+
console.warn('Failed to update Firestore before account deletion:', firestoreError);
|
|
754
|
+
}
|
|
745
755
|
}
|
|
746
756
|
// Delete the user account
|
|
747
757
|
await deleteUser(user);
|