svelte-firekit 0.1.2 → 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.
@@ -1,8 +1,8 @@
1
1
  <script lang="ts">
2
2
  import { firekitAuth } from '../services/auth.js';
3
- import { firebaseService } from '../firebase.js';
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,33 +36,51 @@
36
36
  fallback?: Snippet<[]>;
37
37
  } = $props();
38
38
 
39
- // Get Firebase Auth instance with error handling
40
- let auth: Auth | null = null;
41
- try {
42
- auth = firebaseService.getAuthInstance();
43
- } catch (error) {
44
- console.warn('Firebase Auth not available:', error);
45
- }
46
-
47
- if (!auth) {
48
- throw new Error('Firebase Auth instance not available');
49
- }
50
-
51
- // 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;
52
42
  let authState = $state(firekitAuth.getState());
53
43
 
54
- // Subscribe to auth state changes
55
- const unsubscribe = firekitAuth.onAuthStateChanged((state) => {
56
- authState = state;
57
- });
58
-
59
44
  // Sign out function
60
45
  async function signOut() {
61
46
  await firekitAuth.signOut();
62
47
  }
63
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
+
64
82
  // Check if current auth state matches requirements
65
- $effect(() => {
83
+ function checkAuthState() {
66
84
  if (authState.loading) return;
67
85
 
68
86
  const isAuthenticated = firekitAuth.isAuthenticated();
@@ -71,11 +89,18 @@
71
89
  if (shouldRedirect) {
72
90
  goto(redirectTo);
73
91
  }
92
+ }
93
+
94
+ // Watch for auth state changes
95
+ $effect(() => {
96
+ checkAuthState();
74
97
  });
75
98
 
76
99
  // Cleanup subscription on component destruction
77
100
  onDestroy(() => {
78
- unsubscribe();
101
+ if (unsubscribe) {
102
+ unsubscribe();
103
+ }
79
104
  });
80
105
  </script>
81
106
 
@@ -90,6 +115,6 @@
90
115
  </div>
91
116
  </div>
92
117
  {/if}
93
- {:else if firekitAuth.isAuthenticated() === requireAuth}
118
+ {:else if auth && firekitAuth.isAuthenticated() === requireAuth}
94
119
  {@render children(authState.user!, auth, signOut)}
95
120
  {/if}
@@ -1,8 +1,8 @@
1
1
  <script lang="ts">
2
2
  import { firekitAuth } from '../services/auth.js';
3
- import { firebaseService } from '../firebase.js';
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
- // Get Firebase Auth instance
46
- const auth = firebaseService.getAuthInstance();
47
- if (!auth) {
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
- $effect(() => {
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
- Promise.all(verificationChecks.map((check) => check(authState.user!, auth)))
82
- .then((results) => {
83
- verificationPassed = results.every((result) => result === true);
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
- .finally(() => {
94
- isVerifying = false;
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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "svelte-firekit",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "license": "MIT",
5
5
  "scripts": {
6
6
  "dev": "vite dev",