svelte-firekit 0.0.20 → 0.0.22
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/README.md +218 -55
- package/dist/auth/auth.d.ts +84 -1
- package/dist/auth/auth.js +89 -3
- package/dist/auth/presence.svelte.d.ts +65 -0
- package/dist/auth/presence.svelte.js +42 -0
- package/dist/auth/user.svelte.d.ts +22 -4
- package/dist/auth/user.svelte.js +25 -4
- package/dist/config.d.ts +10 -0
- package/dist/config.js +63 -0
- package/dist/firebase.d.ts +69 -0
- package/dist/firebase.js +71 -3
- package/dist/firestore/awaitable-doc.svelte.d.ts +126 -0
- package/dist/firestore/awaitable-doc.svelte.js +126 -0
- package/dist/firestore/collection.svelte.d.ts +80 -1
- package/dist/firestore/collection.svelte.js +79 -0
- package/dist/firestore/doc.svelte.d.ts +75 -1
- package/dist/firestore/doc.svelte.js +74 -1
- package/dist/firestore/document-mutations.svelte.d.ts +137 -0
- package/dist/firestore/document-mutations.svelte.js +123 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/realtime/realtime.svelte.d.ts +145 -0
- package/dist/realtime/realtime.svelte.js +113 -14
- package/dist/storage/download-url.svelte.d.ts +69 -0
- package/dist/storage/download-url.svelte.js +69 -0
- package/dist/storage/storage-list.svelte.d.ts +72 -0
- package/dist/storage/storage-list.svelte.js +72 -0
- package/dist/storage/upload-task.svelte.d.ts +71 -0
- package/dist/storage/upload-task.svelte.js +71 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,58 +1,221 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
##
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
1
|
+
---
|
|
2
|
+
slug: docs
|
|
3
|
+
title: Welcome to Svelte Firekit
|
|
4
|
+
description: a comprehensive library integrating SvelteKit and Firebase for building robust micro SaaS applications.
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## Introduction to Svelte Firekit
|
|
8
|
+
|
|
9
|
+
Svelte Firekit is a powerful Firebase toolkit for SvelteKit applications, providing a comprehensive set of utilities, stores, and components for seamless Firebase integration. Whether you're building a micro SaaS, web application, or any Firebase-powered project, Svelte Firekit streamlines your development process.
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
11
13
|
```bash
|
|
12
|
-
|
|
13
|
-
npx sv create
|
|
14
|
-
|
|
15
|
-
# create a new project in my-app
|
|
16
|
-
npx sv create my-app
|
|
17
|
-
```
|
|
18
|
-
|
|
19
|
-
## Developing
|
|
20
|
-
|
|
21
|
-
Once you've created a project and installed dependencies with `npm install` (or `pnpm install` or `yarn`), start a development server:
|
|
22
|
-
|
|
23
|
-
```bash
|
|
24
|
-
npm run dev
|
|
25
|
-
|
|
26
|
-
# or start the server and open the app in a new browser tab
|
|
27
|
-
npm run dev -- --open
|
|
28
|
-
```
|
|
29
|
-
|
|
30
|
-
Everything inside `src/lib` is part of your library, everything inside `src/routes` can be used as a showcase or preview app.
|
|
31
|
-
|
|
32
|
-
## Building
|
|
33
|
-
|
|
34
|
-
To build your library:
|
|
35
|
-
|
|
36
|
-
```bash
|
|
37
|
-
npm run package
|
|
14
|
+
npm install firebase svelte-firekit
|
|
38
15
|
```
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
```
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
16
|
+
|
|
17
|
+
## Configuration
|
|
18
|
+
|
|
19
|
+
Svelte Firekit automatically manages your Firebase configuration through environment variables. Create a `.env` file in your project root with the following variables:
|
|
20
|
+
|
|
21
|
+
```env
|
|
22
|
+
PUBLIC_FIREBASE_API_KEY=your_api_key
|
|
23
|
+
PUBLIC_FIREBASE_AUTH_DOMAIN=your_auth_domain
|
|
24
|
+
PUBLIC_FIREBASE_PROJECT_ID=your_project_id
|
|
25
|
+
PUBLIC_FIREBASE_STORAGE_BUCKET=your_storage_bucket
|
|
26
|
+
PUBLIC_FIREBASE_MESSAGING_SENDER_ID=your_messaging_sender_id
|
|
27
|
+
PUBLIC_FIREBASE_APP_ID=your_app_id
|
|
28
|
+
PUBLIC_FIREBASE_MEASUREMENT_ID=your_measurement_id
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
The configuration is automatically handled by Firekit - no manual setup required. If any required environment variables are missing, Firekit will throw a helpful error indicating which variables need to be set.
|
|
32
|
+
|
|
33
|
+
## Usage Example
|
|
34
|
+
|
|
35
|
+
Here's a simple example showing how to display the current user's name:
|
|
36
|
+
|
|
37
|
+
```svelte
|
|
38
|
+
<script>
|
|
39
|
+
import { firekitUser } from 'svelte-firekit';
|
|
40
|
+
</script>
|
|
41
|
+
|
|
42
|
+
Hello {firekitUser.displayName}
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
The `firekitUser` store provides access to the current user's information and authentication state.
|
|
46
|
+
|
|
47
|
+
## Core Features
|
|
48
|
+
|
|
49
|
+
### 🔥 Firebase Integration
|
|
50
|
+
- Zero-config Firebase setup through environment variables
|
|
51
|
+
- Automatic initialization and app management
|
|
52
|
+
- Built-in error handling and connection state management
|
|
53
|
+
- Type-safe configuration management
|
|
54
|
+
|
|
55
|
+
### 🔐 Authentication
|
|
56
|
+
- Complete authentication system with built-in components
|
|
57
|
+
- Support for multiple authentication providers:
|
|
58
|
+
- Email/Password
|
|
59
|
+
- Google
|
|
60
|
+
- GitHub
|
|
61
|
+
- Custom providers
|
|
62
|
+
- User state management and persistence through `firekitUser` store
|
|
63
|
+
|
|
64
|
+
### 📚 Firestore Integration
|
|
65
|
+
- Reactive data stores for real-time updates
|
|
66
|
+
- Simplified CRUD operations
|
|
67
|
+
- Batch operations and transactions
|
|
68
|
+
- Type-safe document references
|
|
69
|
+
- Automatic data serialization/deserialization
|
|
70
|
+
|
|
71
|
+
### 📦 Storage Management
|
|
72
|
+
- File upload and download utilities
|
|
73
|
+
- Progress tracking and status updates
|
|
74
|
+
- Storage security rules helpers
|
|
75
|
+
- Image optimization utilities
|
|
76
|
+
|
|
77
|
+
### ⚡ Server-Side Rendering
|
|
78
|
+
- Full SSR compatibility
|
|
79
|
+
- Hydration support
|
|
80
|
+
- Server-side data fetching
|
|
81
|
+
- SEO-friendly rendering
|
|
82
|
+
|
|
83
|
+
### 🎯 Type Safety
|
|
84
|
+
- Built with TypeScript
|
|
85
|
+
- Complete type definitions
|
|
86
|
+
- Intelligent autocomplete
|
|
87
|
+
- Runtime type checking
|
|
88
|
+
- Type-safe Firestore operations
|
|
89
|
+
|
|
90
|
+
## Why Svelte Firekit?
|
|
91
|
+
|
|
92
|
+
- **Zero Configuration**: Automatic Firebase setup through environment variables
|
|
93
|
+
- **Type Safety**: Full TypeScript support with built-in type checking
|
|
94
|
+
- **Rapid Development**: Get your Firebase-powered SvelteKit application up and running in minutes
|
|
95
|
+
- **Best Practices**: Built following Firebase and SvelteKit best practices
|
|
96
|
+
- **Production Ready**: Battle-tested in production environments
|
|
97
|
+
- **Active Community**: Regular updates and active community support
|
|
98
|
+
- **Extensible**: Easy to customize and extend for your specific needs
|
|
99
|
+
|
|
100
|
+
## Next Steps
|
|
101
|
+
|
|
102
|
+
- Check out our [Getting Started](/getting-started) guide
|
|
103
|
+
- Explore the [API Reference](/api)
|
|
104
|
+
- View [Examples](/examples)
|
|
105
|
+
- Join our [Community](/community)
|
|
106
|
+
|
|
107
|
+
## Contributing
|
|
108
|
+
|
|
109
|
+
We welcome contributions! Please see our [Contributing Guide](/contributing) for more details.
|
|
110
|
+
|
|
111
|
+
## License
|
|
112
|
+
|
|
113
|
+
Svelte Firekit is released under the MIT License. See the [LICENSE](/license) file for more details.
|
|
114
|
+
|
|
115
|
+
# Authentication
|
|
116
|
+
|
|
117
|
+
Svelte Firekit provides a comprehensive authentication system through the `firekitAuth` singleton, offering various authentication methods and user management features.
|
|
118
|
+
|
|
119
|
+
## Basic Usage
|
|
120
|
+
|
|
121
|
+
```typescript
|
|
122
|
+
import { firekitAuth } from 'svelte-firekit';
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
## Authentication Methods
|
|
126
|
+
|
|
127
|
+
### Google Authentication
|
|
128
|
+
|
|
129
|
+
```typescript
|
|
130
|
+
await firekitAuth.signInWithGoogle();
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
### Email/Password Authentication
|
|
134
|
+
|
|
135
|
+
```typescript
|
|
136
|
+
// Sign in
|
|
137
|
+
await firekitAuth.signInWithEmail(email, password);
|
|
138
|
+
|
|
139
|
+
// Register
|
|
140
|
+
await firekitAuth.registerWithEmail(email, password, displayName);
|
|
141
|
+
|
|
142
|
+
// Sign out
|
|
143
|
+
await firekitAuth.logOut();
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
## User Management
|
|
147
|
+
|
|
148
|
+
### Password Management
|
|
149
|
+
|
|
150
|
+
```typescript
|
|
151
|
+
// Send password reset email
|
|
152
|
+
await firekitAuth.sendPasswordReset(email);
|
|
153
|
+
|
|
154
|
+
// Update password (requires reauthentication)
|
|
155
|
+
await firekitAuth.updateUserPassword(newPassword, currentPassword);
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
### Profile Management
|
|
159
|
+
|
|
160
|
+
```typescript
|
|
161
|
+
// Update user profile
|
|
162
|
+
await firekitAuth.updateUserProfile({
|
|
163
|
+
displayName: "New Name",
|
|
164
|
+
photoURL: "https://example.com/photo.jpg"
|
|
165
|
+
});
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
### Email Verification
|
|
169
|
+
|
|
170
|
+
```typescript
|
|
171
|
+
// Send verification email
|
|
172
|
+
await firekitAuth.sendEmailVerificationToUser();
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
### Account Deletion
|
|
176
|
+
|
|
177
|
+
```typescript
|
|
178
|
+
// Delete user account
|
|
179
|
+
await firekitAuth.deleteUserAccount();
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
## Automatic Firestore Integration
|
|
183
|
+
|
|
184
|
+
The authentication system automatically maintains a user document in Firestore with the following information:
|
|
185
|
+
- User ID
|
|
186
|
+
- Email
|
|
187
|
+
- Email verification status
|
|
188
|
+
- Display name
|
|
189
|
+
- Photo URL
|
|
190
|
+
- Authentication provider information
|
|
191
|
+
- Anonymous status
|
|
192
|
+
|
|
193
|
+
## Error Handling
|
|
194
|
+
|
|
195
|
+
All methods include proper error handling and return appropriate error messages. For example, password updates will return:
|
|
196
|
+
|
|
197
|
+
```typescript
|
|
198
|
+
{
|
|
199
|
+
success: boolean;
|
|
200
|
+
message: string;
|
|
201
|
+
code?: string; // In case of errors
|
|
202
|
+
}
|
|
58
203
|
```
|
|
204
|
+
|
|
205
|
+
## Features
|
|
206
|
+
|
|
207
|
+
- 🔐 Multiple authentication providers
|
|
208
|
+
- 📝 Automatic user profile management
|
|
209
|
+
- 🔄 Password reset and update functionality
|
|
210
|
+
- ✉️ Email verification
|
|
211
|
+
- 🗑️ Account deletion
|
|
212
|
+
- 🔄 Reauthentication support
|
|
213
|
+
- 📚 Automatic Firestore user document management
|
|
214
|
+
- ⚡ Type-safe operations
|
|
215
|
+
|
|
216
|
+
## Important Notes
|
|
217
|
+
|
|
218
|
+
1. User data is automatically synchronized with Firestore
|
|
219
|
+
2. Password updates require current password verification
|
|
220
|
+
3. Account deletion removes both authentication and Firestore data
|
|
221
|
+
4. All operations are fully typed for TypeScript support
|
package/dist/auth/auth.d.ts
CHANGED
|
@@ -1,20 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module FirekitAuth
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Manages Firebase authentication operations including sign-in, registration, and profile management.
|
|
6
|
+
* @class
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* // Sign in with Google
|
|
10
|
+
* await firekitAuth.signInWithGoogle();
|
|
11
|
+
*
|
|
12
|
+
* // Register new user
|
|
13
|
+
* await firekitAuth.registerWithEmail("user@example.com", "password123", "John Doe");
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
1
16
|
declare class FirekitAuth {
|
|
2
17
|
private static instance;
|
|
3
18
|
private auth;
|
|
4
19
|
private firestore;
|
|
5
20
|
private constructor();
|
|
21
|
+
/**
|
|
22
|
+
* Gets singleton instance of FirekitAuth
|
|
23
|
+
* @returns {FirekitAuth} The FirekitAuth instance
|
|
24
|
+
*/
|
|
6
25
|
static getInstance(): FirekitAuth;
|
|
26
|
+
/**
|
|
27
|
+
* Initiates Google sign-in popup and updates user data in Firestore
|
|
28
|
+
* @throws {Error} If sign-in fails
|
|
29
|
+
*/
|
|
7
30
|
signInWithGoogle(): Promise<void>;
|
|
31
|
+
/**
|
|
32
|
+
* Signs in user with email and password
|
|
33
|
+
* @param {string} email User's email
|
|
34
|
+
* @param {string} password User's password
|
|
35
|
+
* @throws {Error} If sign-in fails
|
|
36
|
+
*/
|
|
8
37
|
signInWithEmail(email: string, password: string): Promise<void>;
|
|
38
|
+
/**
|
|
39
|
+
* Registers new user with email and password
|
|
40
|
+
* @param {string} email User's email
|
|
41
|
+
* @param {string} password User's password
|
|
42
|
+
* @param {string} displayName User's display name
|
|
43
|
+
* @throws {Error} If registration fails
|
|
44
|
+
*/
|
|
9
45
|
registerWithEmail(email: string, password: string, displayName: string): Promise<void>;
|
|
46
|
+
/**
|
|
47
|
+
* Updates user data in Firestore
|
|
48
|
+
* @param {User} user Firebase user object
|
|
49
|
+
* @private
|
|
50
|
+
*/
|
|
10
51
|
private updateUserInFirestore;
|
|
52
|
+
/**
|
|
53
|
+
* Signs out current user
|
|
54
|
+
* @throws {Error} If sign-out fails
|
|
55
|
+
*/
|
|
11
56
|
logOut(): Promise<void>;
|
|
57
|
+
/**
|
|
58
|
+
* Sends password reset email
|
|
59
|
+
* @param {string} email User's email
|
|
60
|
+
* @throws {Error} If sending reset email fails
|
|
61
|
+
*/
|
|
12
62
|
sendPasswordReset(email: string): Promise<void>;
|
|
63
|
+
/**
|
|
64
|
+
* Sends email verification to current user
|
|
65
|
+
* @throws {Error} If sending verification fails
|
|
66
|
+
*/
|
|
13
67
|
sendEmailVerificationToUser(): Promise<void>;
|
|
68
|
+
/**
|
|
69
|
+
* Updates user profile data
|
|
70
|
+
* @param {Object} profile Profile update data
|
|
71
|
+
* @param {string} [profile.displayName] New display name
|
|
72
|
+
* @param {string} [profile.photoURL] New photo URL
|
|
73
|
+
* @throws {Error} If update fails
|
|
74
|
+
*/
|
|
14
75
|
updateUserProfile(profile: {
|
|
15
76
|
displayName?: string;
|
|
16
77
|
photoURL?: string;
|
|
17
78
|
}): Promise<void>;
|
|
79
|
+
/**
|
|
80
|
+
* Updates user password with reauthentication
|
|
81
|
+
* @param {string} newPassword New password
|
|
82
|
+
* @param {string} currentPassword Current password for reauthentication
|
|
83
|
+
* @returns {Promise<{success: boolean, message: string, code?: string}>} Update result
|
|
84
|
+
*/
|
|
18
85
|
updateUserPassword(newPassword: string, currentPassword: string): Promise<{
|
|
19
86
|
success: boolean;
|
|
20
87
|
message: string;
|
|
@@ -24,11 +91,27 @@ declare class FirekitAuth {
|
|
|
24
91
|
code: any;
|
|
25
92
|
message: string;
|
|
26
93
|
}>;
|
|
27
|
-
|
|
94
|
+
/**
|
|
95
|
+
* Reauthenticates current user
|
|
96
|
+
* @param {string} currentPassword Current password
|
|
97
|
+
* @throws {Error} If reauthentication fails
|
|
98
|
+
* @private
|
|
99
|
+
*/
|
|
100
|
+
private reauthenticateUser;
|
|
101
|
+
/**
|
|
102
|
+
* Deletes user account and associated data
|
|
103
|
+
* @returns {Promise<{success: boolean, message: string}>} Deletion result
|
|
104
|
+
* @throws {Error} If deletion fails
|
|
105
|
+
*/
|
|
28
106
|
deleteUserAccount(): Promise<{
|
|
29
107
|
success: boolean;
|
|
30
108
|
message: string;
|
|
31
109
|
}>;
|
|
32
110
|
}
|
|
111
|
+
/**
|
|
112
|
+
* Pre-initialized singleton instance of FirekitAuth
|
|
113
|
+
* @const
|
|
114
|
+
* @type {FirekitAuth}
|
|
115
|
+
*/
|
|
33
116
|
export declare const firekitAuth: FirekitAuth;
|
|
34
117
|
export {};
|
package/dist/auth/auth.js
CHANGED
|
@@ -1,28 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module FirekitAuth
|
|
3
|
+
*/
|
|
1
4
|
import { GoogleAuthProvider, sendPasswordResetEmail, signInWithEmailAndPassword, signInWithPopup, signOut, createUserWithEmailAndPassword, sendEmailVerification, updateProfile, updatePassword, EmailAuthProvider, reauthenticateWithCredential, } from 'firebase/auth';
|
|
2
|
-
import { doc,
|
|
5
|
+
import { doc, setDoc } from 'firebase/firestore';
|
|
3
6
|
import { firebaseService } from '../firebase.js';
|
|
4
7
|
import { firekitDocMutations } from '../firestore/document-mutations.svelte.js';
|
|
5
|
-
|
|
8
|
+
/**
|
|
9
|
+
* Manages Firebase authentication operations including sign-in, registration, and profile management.
|
|
10
|
+
* @class
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* // Sign in with Google
|
|
14
|
+
* await firekitAuth.signInWithGoogle();
|
|
15
|
+
*
|
|
16
|
+
* // Register new user
|
|
17
|
+
* await firekitAuth.registerWithEmail("user@example.com", "password123", "John Doe");
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
6
20
|
class FirekitAuth {
|
|
7
21
|
static instance;
|
|
8
22
|
auth = firebaseService.getAuthInstance();
|
|
9
23
|
firestore = firebaseService.getDbInstance();
|
|
10
24
|
constructor() { }
|
|
25
|
+
/**
|
|
26
|
+
* Gets singleton instance of FirekitAuth
|
|
27
|
+
* @returns {FirekitAuth} The FirekitAuth instance
|
|
28
|
+
*/
|
|
11
29
|
static getInstance() {
|
|
12
30
|
if (!FirekitAuth.instance) {
|
|
13
31
|
FirekitAuth.instance = new FirekitAuth();
|
|
14
32
|
}
|
|
15
33
|
return FirekitAuth.instance;
|
|
16
34
|
}
|
|
35
|
+
/**
|
|
36
|
+
* Initiates Google sign-in popup and updates user data in Firestore
|
|
37
|
+
* @throws {Error} If sign-in fails
|
|
38
|
+
*/
|
|
17
39
|
async signInWithGoogle() {
|
|
18
40
|
const provider = new GoogleAuthProvider();
|
|
19
41
|
const result = await signInWithPopup(this.auth, provider);
|
|
20
42
|
await this.updateUserInFirestore(result.user);
|
|
21
43
|
}
|
|
44
|
+
/**
|
|
45
|
+
* Signs in user with email and password
|
|
46
|
+
* @param {string} email User's email
|
|
47
|
+
* @param {string} password User's password
|
|
48
|
+
* @throws {Error} If sign-in fails
|
|
49
|
+
*/
|
|
22
50
|
async signInWithEmail(email, password) {
|
|
23
51
|
const result = await signInWithEmailAndPassword(this.auth, email, password);
|
|
24
52
|
await this.updateUserInFirestore(result.user);
|
|
25
53
|
}
|
|
54
|
+
/**
|
|
55
|
+
* Registers new user with email and password
|
|
56
|
+
* @param {string} email User's email
|
|
57
|
+
* @param {string} password User's password
|
|
58
|
+
* @param {string} displayName User's display name
|
|
59
|
+
* @throws {Error} If registration fails
|
|
60
|
+
*/
|
|
26
61
|
async registerWithEmail(email, password, displayName) {
|
|
27
62
|
const result = await createUserWithEmailAndPassword(this.auth, email, password);
|
|
28
63
|
const user = result.user;
|
|
@@ -32,6 +67,11 @@ class FirekitAuth {
|
|
|
32
67
|
await sendEmailVerification(user);
|
|
33
68
|
}
|
|
34
69
|
}
|
|
70
|
+
/**
|
|
71
|
+
* Updates user data in Firestore
|
|
72
|
+
* @param {User} user Firebase user object
|
|
73
|
+
* @private
|
|
74
|
+
*/
|
|
35
75
|
async updateUserInFirestore(user) {
|
|
36
76
|
const ref = doc(this.firestore, 'users', user.uid);
|
|
37
77
|
const userData = {
|
|
@@ -46,23 +86,49 @@ class FirekitAuth {
|
|
|
46
86
|
};
|
|
47
87
|
await setDoc(ref, userData, { merge: true });
|
|
48
88
|
}
|
|
89
|
+
/**
|
|
90
|
+
* Signs out current user
|
|
91
|
+
* @throws {Error} If sign-out fails
|
|
92
|
+
*/
|
|
49
93
|
async logOut() {
|
|
50
94
|
await signOut(this.auth);
|
|
51
95
|
}
|
|
96
|
+
/**
|
|
97
|
+
* Sends password reset email
|
|
98
|
+
* @param {string} email User's email
|
|
99
|
+
* @throws {Error} If sending reset email fails
|
|
100
|
+
*/
|
|
52
101
|
async sendPasswordReset(email) {
|
|
53
102
|
await sendPasswordResetEmail(this.auth, email);
|
|
54
103
|
}
|
|
104
|
+
/**
|
|
105
|
+
* Sends email verification to current user
|
|
106
|
+
* @throws {Error} If sending verification fails
|
|
107
|
+
*/
|
|
55
108
|
async sendEmailVerificationToUser() {
|
|
56
109
|
if (this.auth.currentUser) {
|
|
57
110
|
await sendEmailVerification(this.auth.currentUser);
|
|
58
111
|
}
|
|
59
112
|
}
|
|
113
|
+
/**
|
|
114
|
+
* Updates user profile data
|
|
115
|
+
* @param {Object} profile Profile update data
|
|
116
|
+
* @param {string} [profile.displayName] New display name
|
|
117
|
+
* @param {string} [profile.photoURL] New photo URL
|
|
118
|
+
* @throws {Error} If update fails
|
|
119
|
+
*/
|
|
60
120
|
async updateUserProfile(profile) {
|
|
61
121
|
if (this.auth.currentUser) {
|
|
62
122
|
await updateProfile(this.auth.currentUser, profile);
|
|
63
123
|
await this.updateUserInFirestore(this.auth.currentUser);
|
|
64
124
|
}
|
|
65
125
|
}
|
|
126
|
+
/**
|
|
127
|
+
* Updates user password with reauthentication
|
|
128
|
+
* @param {string} newPassword New password
|
|
129
|
+
* @param {string} currentPassword Current password for reauthentication
|
|
130
|
+
* @returns {Promise<{success: boolean, message: string, code?: string}>} Update result
|
|
131
|
+
*/
|
|
66
132
|
async updateUserPassword(newPassword, currentPassword) {
|
|
67
133
|
if (!this.auth.currentUser) {
|
|
68
134
|
throw new Error('No authenticated user found.');
|
|
@@ -76,9 +142,19 @@ class FirekitAuth {
|
|
|
76
142
|
if (error.code === 'auth/wrong-password') {
|
|
77
143
|
return { success: false, code: error.code, message: 'Reauthentication failed: incorrect password.' };
|
|
78
144
|
}
|
|
79
|
-
return {
|
|
145
|
+
return {
|
|
146
|
+
success: false,
|
|
147
|
+
code: error.code || 'unknown_error',
|
|
148
|
+
message: `Failed to update password: ${error.message || 'Unknown error occurred.'}`
|
|
149
|
+
};
|
|
80
150
|
}
|
|
81
151
|
}
|
|
152
|
+
/**
|
|
153
|
+
* Reauthenticates current user
|
|
154
|
+
* @param {string} currentPassword Current password
|
|
155
|
+
* @throws {Error} If reauthentication fails
|
|
156
|
+
* @private
|
|
157
|
+
*/
|
|
82
158
|
async reauthenticateUser(currentPassword) {
|
|
83
159
|
if (!this.auth.currentUser || !this.auth.currentUser.email) {
|
|
84
160
|
throw new Error('No authenticated user or email unavailable.');
|
|
@@ -91,6 +167,11 @@ class FirekitAuth {
|
|
|
91
167
|
throw new Error(`Reauthentication failed: ${error.message || 'Unknown error occurred.'}`);
|
|
92
168
|
}
|
|
93
169
|
}
|
|
170
|
+
/**
|
|
171
|
+
* Deletes user account and associated data
|
|
172
|
+
* @returns {Promise<{success: boolean, message: string}>} Deletion result
|
|
173
|
+
* @throws {Error} If deletion fails
|
|
174
|
+
*/
|
|
94
175
|
async deleteUserAccount() {
|
|
95
176
|
if (!this.auth.currentUser) {
|
|
96
177
|
throw new Error('No authenticated user found.');
|
|
@@ -105,4 +186,9 @@ class FirekitAuth {
|
|
|
105
186
|
}
|
|
106
187
|
}
|
|
107
188
|
}
|
|
189
|
+
/**
|
|
190
|
+
* Pre-initialized singleton instance of FirekitAuth
|
|
191
|
+
* @const
|
|
192
|
+
* @type {FirekitAuth}
|
|
193
|
+
*/
|
|
108
194
|
export const firekitAuth = FirekitAuth.getInstance();
|
|
@@ -1,23 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Geolocation configuration options
|
|
3
|
+
*/
|
|
1
4
|
interface GeolocationConfig {
|
|
5
|
+
/** Whether geolocation tracking is enabled */
|
|
2
6
|
enabled: boolean;
|
|
7
|
+
/** Type of geolocation service to use */
|
|
3
8
|
type: 'browser' | 'ip' | 'custom';
|
|
9
|
+
/** Custom function for retrieving geolocation */
|
|
4
10
|
customGeolocationFn?: () => Promise<{
|
|
5
11
|
latitude: number;
|
|
6
12
|
longitude: number;
|
|
7
13
|
}>;
|
|
14
|
+
/** URL for IP-based geolocation service */
|
|
8
15
|
ipServiceUrl?: string;
|
|
16
|
+
/** Whether user consent is required for location tracking */
|
|
9
17
|
requireConsent?: boolean;
|
|
10
18
|
}
|
|
19
|
+
/**
|
|
20
|
+
* Presence service configuration options
|
|
21
|
+
*/
|
|
11
22
|
interface PresenceConfig {
|
|
23
|
+
/** Geolocation settings */
|
|
12
24
|
geolocation?: GeolocationConfig;
|
|
25
|
+
/** Session timeout in milliseconds */
|
|
13
26
|
sessionTTL?: number;
|
|
27
|
+
/** Presence update interval in milliseconds */
|
|
14
28
|
updateInterval?: number;
|
|
15
29
|
}
|
|
30
|
+
/**
|
|
31
|
+
* Location data structure
|
|
32
|
+
*/
|
|
16
33
|
interface Location {
|
|
17
34
|
latitude: number | null;
|
|
18
35
|
longitude: number | null;
|
|
19
36
|
lastUpdated: string | null;
|
|
20
37
|
}
|
|
38
|
+
/**
|
|
39
|
+
* Session data structure
|
|
40
|
+
*/
|
|
21
41
|
interface SessionData {
|
|
22
42
|
uid: string;
|
|
23
43
|
userId: string;
|
|
@@ -27,6 +47,9 @@ interface SessionData {
|
|
|
27
47
|
lastSeen: string;
|
|
28
48
|
location?: Location;
|
|
29
49
|
}
|
|
50
|
+
/**
|
|
51
|
+
* Presence event structure
|
|
52
|
+
*/
|
|
30
53
|
type PresenceEvent = {
|
|
31
54
|
type: 'status_change' | 'error' | 'init' | 'disconnect' | 'location_update';
|
|
32
55
|
data?: any;
|
|
@@ -34,6 +57,23 @@ type PresenceEvent = {
|
|
|
34
57
|
timestamp: number;
|
|
35
58
|
};
|
|
36
59
|
type PresenceEventCallback = (event: PresenceEvent) => void;
|
|
60
|
+
/**
|
|
61
|
+
* Manages real-time user presence tracking with optional geolocation support
|
|
62
|
+
* @class
|
|
63
|
+
* @example
|
|
64
|
+
* ```typescript
|
|
65
|
+
* // Initialize presence tracking
|
|
66
|
+
* await presenceService.initialize(currentUser, {
|
|
67
|
+
* geolocation: { enabled: true, type: 'browser' },
|
|
68
|
+
* sessionTTL: 30 * 60 * 1000
|
|
69
|
+
* });
|
|
70
|
+
*
|
|
71
|
+
* // Listen for presence events
|
|
72
|
+
* presenceService.addEventListener((event) => {
|
|
73
|
+
* console.log(event.type, event.data);
|
|
74
|
+
* });
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
37
77
|
declare class PresenceService {
|
|
38
78
|
private static instance;
|
|
39
79
|
private connectedListener;
|
|
@@ -50,24 +90,49 @@ declare class PresenceService {
|
|
|
50
90
|
private _error;
|
|
51
91
|
private constructor();
|
|
52
92
|
static getInstance(): PresenceService;
|
|
93
|
+
/** Get current session data */
|
|
53
94
|
get currentSession(): SessionData | null;
|
|
95
|
+
/** Get all active sessions */
|
|
54
96
|
get sessions(): SessionData[];
|
|
97
|
+
/** Get current presence status */
|
|
55
98
|
get status(): "online" | "offline" | "away";
|
|
99
|
+
/** Get loading state */
|
|
56
100
|
get loading(): boolean;
|
|
101
|
+
/** Get error state */
|
|
57
102
|
get error(): Error | null;
|
|
103
|
+
/** Check if service is initialized */
|
|
58
104
|
get isInitialized(): boolean;
|
|
105
|
+
/** Check if location consent is granted */
|
|
59
106
|
get hasLocationConsent(): boolean;
|
|
107
|
+
/**
|
|
108
|
+
* Initialize presence tracking
|
|
109
|
+
* @param {any} user Current user object
|
|
110
|
+
* @param {PresenceConfig} config Optional configuration
|
|
111
|
+
* @throws {Error} If initialization fails
|
|
112
|
+
*/
|
|
60
113
|
private initializePresence;
|
|
61
114
|
private setupVisibilityListener;
|
|
62
115
|
private getDeviceInfo;
|
|
116
|
+
/**
|
|
117
|
+
* Request location tracking consent
|
|
118
|
+
* @returns {Promise<boolean>} Whether consent was granted
|
|
119
|
+
*/
|
|
63
120
|
requestLocationConsent(): Promise<boolean>;
|
|
64
121
|
private getLocation;
|
|
65
122
|
initialize(user: any, config?: PresenceConfig): Promise<void>;
|
|
66
123
|
private setPresence;
|
|
67
124
|
private startLocationWatcher;
|
|
68
125
|
private stopLocationWatcher;
|
|
126
|
+
/**
|
|
127
|
+
* Add presence event listener
|
|
128
|
+
* @param {Function} callback Event callback function
|
|
129
|
+
* @returns {Function} Cleanup function to remove listener
|
|
130
|
+
*/
|
|
69
131
|
addEventListener(callback: PresenceEventCallback): () => boolean;
|
|
70
132
|
private emitEvent;
|
|
133
|
+
/**
|
|
134
|
+
* Cleanup presence tracking
|
|
135
|
+
*/
|
|
71
136
|
dispose(): void;
|
|
72
137
|
}
|
|
73
138
|
export declare const presenceService: PresenceService;
|