svelte-firekit 0.0.16 → 0.0.18

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.
@@ -4,6 +4,7 @@ declare class FirekitAuth {
4
4
  private firestore;
5
5
  private constructor();
6
6
  static getInstance(): FirekitAuth;
7
+ getDeviceIdentifier(): string;
7
8
  signInWithGoogle(): Promise<void>;
8
9
  signInWithEmail(email: string, password: string): Promise<void>;
9
10
  registerWithEmail(email: string, password: string, displayName: string): Promise<void>;
@@ -15,7 +16,21 @@ declare class FirekitAuth {
15
16
  displayName?: string;
16
17
  photoURL?: string;
17
18
  }): Promise<void>;
18
- updateUserPassword(newPassword: string): Promise<void>;
19
+ updateUserPassword(newPassword: string, currentPassword: string): Promise<{
20
+ success: boolean;
21
+ message: string;
22
+ code?: undefined;
23
+ } | {
24
+ success: boolean;
25
+ code: any;
26
+ message: string;
27
+ }>;
28
+ reauthenticateUser(currentPassword: string): Promise<void>;
29
+ deleteUserAccount(): Promise<{
30
+ success: boolean;
31
+ message: string;
32
+ }>;
33
+ updateUserSession(user: any): Promise<void>;
19
34
  }
20
35
  export declare const firekitAuth: FirekitAuth;
21
36
  export {};
package/dist/auth/auth.js CHANGED
@@ -1,6 +1,8 @@
1
- import { GoogleAuthProvider, sendPasswordResetEmail, signInWithEmailAndPassword, signInWithPopup, signOut, createUserWithEmailAndPassword, sendEmailVerification, updateProfile, updatePassword } from 'firebase/auth';
2
- import { doc, setDoc } from 'firebase/firestore';
1
+ import { GoogleAuthProvider, sendPasswordResetEmail, signInWithEmailAndPassword, signInWithPopup, signOut, createUserWithEmailAndPassword, sendEmailVerification, updateProfile, updatePassword, EmailAuthProvider, reauthenticateWithCredential, } from 'firebase/auth';
2
+ import { doc, getDoc, setDoc, Timestamp } from 'firebase/firestore';
3
3
  import { firebaseService } from '../firebase.js';
4
+ import { firekitDocMutations } from '../firestore/document-mutations.svelte.js';
5
+ import { get, ref, set, update } from 'firebase/database';
4
6
  class FirekitAuth {
5
7
  static instance;
6
8
  auth = firebaseService.getAuthInstance();
@@ -12,14 +14,19 @@ class FirekitAuth {
12
14
  }
13
15
  return FirekitAuth.instance;
14
16
  }
17
+ getDeviceIdentifier() {
18
+ return navigator.userAgent; // Puedes personalizar esto según tu preferencia.
19
+ }
15
20
  async signInWithGoogle() {
16
21
  const provider = new GoogleAuthProvider();
17
22
  const result = await signInWithPopup(this.auth, provider);
18
23
  await this.updateUserInFirestore(result.user);
24
+ await this.updateUserSession(result.user);
19
25
  }
20
26
  async signInWithEmail(email, password) {
21
27
  const result = await signInWithEmailAndPassword(this.auth, email, password);
22
28
  await this.updateUserInFirestore(result.user);
29
+ await this.updateUserSession(result.user);
23
30
  }
24
31
  async registerWithEmail(email, password, displayName) {
25
32
  const result = await createUserWithEmailAndPassword(this.auth, email, password);
@@ -27,6 +34,7 @@ class FirekitAuth {
27
34
  if (user) {
28
35
  await updateProfile(user, { displayName });
29
36
  await this.updateUserInFirestore(user);
37
+ await this.updateUserSession(user);
30
38
  await sendEmailVerification(user);
31
39
  }
32
40
  }
@@ -61,10 +69,71 @@ class FirekitAuth {
61
69
  await this.updateUserInFirestore(this.auth.currentUser);
62
70
  }
63
71
  }
64
- async updateUserPassword(newPassword) {
65
- if (this.auth.currentUser) {
72
+ async updateUserPassword(newPassword, currentPassword) {
73
+ if (!this.auth.currentUser) {
74
+ throw new Error('No authenticated user found.');
75
+ }
76
+ try {
77
+ await this.reauthenticateUser(currentPassword);
66
78
  await updatePassword(this.auth.currentUser, newPassword);
79
+ return { success: true, message: 'Password successfully updated.' };
80
+ }
81
+ catch (error) {
82
+ if (error.code === 'auth/wrong-password') {
83
+ return { success: false, code: error.code, message: 'Reauthentication failed: incorrect password.' };
84
+ }
85
+ return { success: false, code: error.code || 'unknown_error', message: `Failed to update password: ${error.message || 'Unknown error occurred.'}` };
86
+ }
87
+ }
88
+ async reauthenticateUser(currentPassword) {
89
+ if (!this.auth.currentUser || !this.auth.currentUser.email) {
90
+ throw new Error('No authenticated user or email unavailable.');
91
+ }
92
+ const credential = EmailAuthProvider.credential(this.auth.currentUser.email, currentPassword);
93
+ try {
94
+ await reauthenticateWithCredential(this.auth.currentUser, credential);
95
+ }
96
+ catch (error) {
97
+ throw new Error(`Reauthentication failed: ${error.message || 'Unknown error occurred.'}`);
67
98
  }
68
99
  }
100
+ async deleteUserAccount() {
101
+ if (!this.auth.currentUser) {
102
+ throw new Error('No authenticated user found.');
103
+ }
104
+ try {
105
+ firekitDocMutations.delete(`users/${this.auth.currentUser.uid}`);
106
+ await this.auth.currentUser.delete();
107
+ return { success: true, message: 'Account successfully deleted.' };
108
+ }
109
+ catch (error) {
110
+ throw new Error(error.message);
111
+ }
112
+ }
113
+ async updateUserSession(user) {
114
+ let nav = this.getDeviceIdentifier().replace(/[ /]/g, '');
115
+ const sessionId = `${user.uid}_${nav}`;
116
+ const db = firebaseService.getDatabaseInstance();
117
+ const userSessionsRef = ref(db, `sessions/${user.uid}`);
118
+ const userSessionsSnap = await get(userSessionsRef);
119
+ let sessionDatas = [];
120
+ if (userSessionsSnap.exists()) {
121
+ sessionDatas = userSessionsSnap.val().sessionDatas || [];
122
+ if (sessionDatas.some(session => session.uid === sessionId)) {
123
+ console.log('Session already registered from this device.');
124
+ return;
125
+ }
126
+ }
127
+ const newSessionData = {
128
+ uid: sessionId,
129
+ userId: user.uid,
130
+ device: this.getDeviceIdentifier(),
131
+ createdAt: new Date().toISOString(),
132
+ last_changed: new Date().toISOString(),
133
+ status: "online"
134
+ };
135
+ sessionDatas.push(newSessionData);
136
+ await set(userSessionsRef, { sessionDatas });
137
+ }
69
138
  }
70
139
  export const firekitAuth = FirekitAuth.getInstance();
package/dist/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  export { firebaseConfig } from './config.js';
2
2
  export { firebaseService } from './firebase.js';
3
3
  export { firekitUser } from './auth/user.svelte.js';
4
+ export { firekitAuth } from './auth/auth.js';
4
5
  export { firekitAwaitableDoc } from './firestore/awaitable-doc.svelte.js';
5
6
  export { firekitDocMutations } from './firestore/document-mutations.svelte';
6
7
  export { firekitCollection } from './firestore/collection.svelte.js';
package/dist/index.js CHANGED
@@ -3,6 +3,7 @@ export { firebaseConfig } from './config.js';
3
3
  export { firebaseService } from './firebase.js';
4
4
  // auth services
5
5
  export { firekitUser } from './auth/user.svelte.js';
6
+ export { firekitAuth } from './auth/auth.js';
6
7
  // firestore services
7
8
  export { firekitAwaitableDoc } from './firestore/awaitable-doc.svelte.js';
8
9
  export { firekitDocMutations } from './firestore/document-mutations.svelte';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "svelte-firekit",
3
- "version": "0.0.16",
3
+ "version": "0.0.18",
4
4
  "license": "MIT",
5
5
  "scripts": {
6
6
  "dev": "vite dev",