canvasframework 0.4.4 → 0.4.6

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.
@@ -0,0 +1,653 @@
1
+ /**
2
+ * FirebaseAuth - Utilitaire pour Firebase Authentication
3
+ *
4
+ * @example
5
+ * const firebaseAuth = new FirebaseAuth(firebaseCore);
6
+ * await firebaseAuth.signUpWithEmail('user@example.com', 'password123');
7
+ * await firebaseAuth.signInWithEmail('user@example.com', 'password123');
8
+ * firebaseAuth.onAuthStateChanged((user) => console.log('User:', user));
9
+ */
10
+ class FirebaseAuth {
11
+ constructor(firebaseCore) {
12
+ this.core = firebaseCore;
13
+ this.auth = null;
14
+ this.currentUser = null;
15
+ this.authStateListeners = [];
16
+ }
17
+
18
+ /**
19
+ * Initialiser Authentication
20
+ */
21
+ initialize() {
22
+ if (!this.auth) {
23
+ this.auth = this.core.getAuth();
24
+ }
25
+ return this.auth;
26
+ }
27
+
28
+ // ==================== EMAIL / PASSWORD ====================
29
+
30
+ /**
31
+ * Créer un compte avec email/mot de passe
32
+ */
33
+ async signUpWithEmail(email, password, displayName = null) {
34
+ try {
35
+ if (!this.auth) this.initialize();
36
+
37
+ const userCredential = await this.auth.createUserWithEmailAndPassword(email, password);
38
+
39
+ // Mettre à jour le profil si displayName fourni
40
+ if (displayName && userCredential.user) {
41
+ await userCredential.user.updateProfile({ displayName });
42
+ }
43
+
44
+ // Envoyer email de vérification
45
+ await userCredential.user.sendEmailVerification();
46
+
47
+ this.currentUser = userCredential.user;
48
+
49
+ return {
50
+ success: true,
51
+ user: this.formatUser(userCredential.user)
52
+ };
53
+ } catch (error) {
54
+ console.error('❌ Erreur signUpWithEmail:', error);
55
+ throw this.formatError(error);
56
+ }
57
+ }
58
+
59
+ /**
60
+ * Connexion avec email/mot de passe
61
+ */
62
+ async signInWithEmail(email, password) {
63
+ try {
64
+ if (!this.auth) this.initialize();
65
+
66
+ const userCredential = await this.auth.signInWithEmailAndPassword(email, password);
67
+ this.currentUser = userCredential.user;
68
+
69
+ return {
70
+ success: true,
71
+ user: this.formatUser(userCredential.user)
72
+ };
73
+ } catch (error) {
74
+ console.error('❌ Erreur signInWithEmail:', error);
75
+ throw this.formatError(error);
76
+ }
77
+ }
78
+
79
+ /**
80
+ * Connexion anonyme
81
+ */
82
+ async signInAnonymously() {
83
+ try {
84
+ if (!this.auth) this.initialize();
85
+
86
+ const userCredential = await this.auth.signInAnonymously();
87
+ this.currentUser = userCredential.user;
88
+
89
+ return {
90
+ success: true,
91
+ user: this.formatUser(userCredential.user)
92
+ };
93
+ } catch (error) {
94
+ console.error('❌ Erreur signInAnonymously:', error);
95
+ throw this.formatError(error);
96
+ }
97
+ }
98
+
99
+ // ==================== SOCIAL AUTH ====================
100
+
101
+ /**
102
+ * Connexion avec Google
103
+ */
104
+ async signInWithGoogle() {
105
+ try {
106
+ if (!this.auth) this.initialize();
107
+
108
+ const provider = new firebase.auth.GoogleAuthProvider();
109
+ provider.addScope('profile');
110
+ provider.addScope('email');
111
+
112
+ const result = await this.auth.signInWithPopup(provider);
113
+ this.currentUser = result.user;
114
+
115
+ return {
116
+ success: true,
117
+ user: this.formatUser(result.user),
118
+ credential: result.credential
119
+ };
120
+ } catch (error) {
121
+ console.error('❌ Erreur signInWithGoogle:', error);
122
+ throw this.formatError(error);
123
+ }
124
+ }
125
+
126
+ /**
127
+ * Connexion avec Facebook
128
+ */
129
+ async signInWithFacebook() {
130
+ try {
131
+ if (!this.auth) this.initialize();
132
+
133
+ const provider = new firebase.auth.FacebookAuthProvider();
134
+ provider.addScope('email');
135
+ provider.addScope('public_profile');
136
+
137
+ const result = await this.auth.signInWithPopup(provider);
138
+ this.currentUser = result.user;
139
+
140
+ return {
141
+ success: true,
142
+ user: this.formatUser(result.user),
143
+ credential: result.credential
144
+ };
145
+ } catch (error) {
146
+ console.error('❌ Erreur signInWithFacebook:', error);
147
+ throw this.formatError(error);
148
+ }
149
+ }
150
+
151
+ /**
152
+ * Connexion avec Twitter
153
+ */
154
+ async signInWithTwitter() {
155
+ try {
156
+ if (!this.auth) this.initialize();
157
+
158
+ const provider = new firebase.auth.TwitterAuthProvider();
159
+
160
+ const result = await this.auth.signInWithPopup(provider);
161
+ this.currentUser = result.user;
162
+
163
+ return {
164
+ success: true,
165
+ user: this.formatUser(result.user),
166
+ credential: result.credential
167
+ };
168
+ } catch (error) {
169
+ console.error('❌ Erreur signInWithTwitter:', error);
170
+ throw this.formatError(error);
171
+ }
172
+ }
173
+
174
+ /**
175
+ * Connexion avec GitHub
176
+ */
177
+ async signInWithGithub() {
178
+ try {
179
+ if (!this.auth) this.initialize();
180
+
181
+ const provider = new firebase.auth.GithubAuthProvider();
182
+ provider.addScope('user');
183
+
184
+ const result = await this.auth.signInWithPopup(provider);
185
+ this.currentUser = result.user;
186
+
187
+ return {
188
+ success: true,
189
+ user: this.formatUser(result.user),
190
+ credential: result.credential
191
+ };
192
+ } catch (error) {
193
+ console.error('❌ Erreur signInWithGithub:', error);
194
+ throw this.formatError(error);
195
+ }
196
+ }
197
+
198
+ /**
199
+ * Connexion avec Microsoft
200
+ */
201
+ async signInWithMicrosoft() {
202
+ try {
203
+ if (!this.auth) this.initialize();
204
+
205
+ const provider = new firebase.auth.OAuthProvider('microsoft.com');
206
+
207
+ const result = await this.auth.signInWithPopup(provider);
208
+ this.currentUser = result.user;
209
+
210
+ return {
211
+ success: true,
212
+ user: this.formatUser(result.user),
213
+ credential: result.credential
214
+ };
215
+ } catch (error) {
216
+ console.error('❌ Erreur signInWithMicrosoft:', error);
217
+ throw this.formatError(error);
218
+ }
219
+ }
220
+
221
+ /**
222
+ * Connexion avec Apple
223
+ */
224
+ async signInWithApple() {
225
+ try {
226
+ if (!this.auth) this.initialize();
227
+
228
+ const provider = new firebase.auth.OAuthProvider('apple.com');
229
+ provider.addScope('email');
230
+ provider.addScope('name');
231
+
232
+ const result = await this.auth.signInWithPopup(provider);
233
+ this.currentUser = result.user;
234
+
235
+ return {
236
+ success: true,
237
+ user: this.formatUser(result.user),
238
+ credential: result.credential
239
+ };
240
+ } catch (error) {
241
+ console.error('❌ Erreur signInWithApple:', error);
242
+ throw this.formatError(error);
243
+ }
244
+ }
245
+
246
+ // ==================== PHONE AUTH ====================
247
+
248
+ /**
249
+ * Envoyer un code de vérification SMS
250
+ */
251
+ async sendPhoneVerification(phoneNumber, recaptchaContainerId) {
252
+ try {
253
+ if (!this.auth) this.initialize();
254
+
255
+ // Créer le reCAPTCHA verifier
256
+ window.recaptchaVerifier = new firebase.auth.RecaptchaVerifier(recaptchaContainerId, {
257
+ size: 'invisible',
258
+ callback: () => {
259
+ console.log('✅ reCAPTCHA résolu');
260
+ }
261
+ });
262
+
263
+ const confirmationResult = await this.auth.signInWithPhoneNumber(
264
+ phoneNumber,
265
+ window.recaptchaVerifier
266
+ );
267
+
268
+ return {
269
+ success: true,
270
+ confirmationResult
271
+ };
272
+ } catch (error) {
273
+ console.error('❌ Erreur sendPhoneVerification:', error);
274
+ throw this.formatError(error);
275
+ }
276
+ }
277
+
278
+ /**
279
+ * Vérifier le code SMS
280
+ */
281
+ async verifyPhoneCode(confirmationResult, code) {
282
+ try {
283
+ const result = await confirmationResult.confirm(code);
284
+ this.currentUser = result.user;
285
+
286
+ return {
287
+ success: true,
288
+ user: this.formatUser(result.user)
289
+ };
290
+ } catch (error) {
291
+ console.error('❌ Erreur verifyPhoneCode:', error);
292
+ throw this.formatError(error);
293
+ }
294
+ }
295
+
296
+ // ==================== CUSTOM TOKEN ====================
297
+
298
+ /**
299
+ * Connexion avec custom token
300
+ */
301
+ async signInWithCustomToken(token) {
302
+ try {
303
+ if (!this.auth) this.initialize();
304
+
305
+ const userCredential = await this.auth.signInWithCustomToken(token);
306
+ this.currentUser = userCredential.user;
307
+
308
+ return {
309
+ success: true,
310
+ user: this.formatUser(userCredential.user)
311
+ };
312
+ } catch (error) {
313
+ console.error('❌ Erreur signInWithCustomToken:', error);
314
+ throw this.formatError(error);
315
+ }
316
+ }
317
+
318
+ // ==================== PASSWORD MANAGEMENT ====================
319
+
320
+ /**
321
+ * Envoyer email de réinitialisation de mot de passe
322
+ */
323
+ async sendPasswordResetEmail(email) {
324
+ try {
325
+ if (!this.auth) this.initialize();
326
+
327
+ await this.auth.sendPasswordResetEmail(email);
328
+
329
+ return { success: true };
330
+ } catch (error) {
331
+ console.error('❌ Erreur sendPasswordResetEmail:', error);
332
+ throw this.formatError(error);
333
+ }
334
+ }
335
+
336
+ /**
337
+ * Confirmer la réinitialisation du mot de passe
338
+ */
339
+ async confirmPasswordReset(code, newPassword) {
340
+ try {
341
+ if (!this.auth) this.initialize();
342
+
343
+ await this.auth.confirmPasswordReset(code, newPassword);
344
+
345
+ return { success: true };
346
+ } catch (error) {
347
+ console.error('❌ Erreur confirmPasswordReset:', error);
348
+ throw this.formatError(error);
349
+ }
350
+ }
351
+
352
+ /**
353
+ * Changer le mot de passe
354
+ */
355
+ async updatePassword(newPassword) {
356
+ try {
357
+ const user = this.getCurrentUser();
358
+ if (!user) throw new Error('Utilisateur non connecté');
359
+
360
+ await user.updatePassword(newPassword);
361
+
362
+ return { success: true };
363
+ } catch (error) {
364
+ console.error('❌ Erreur updatePassword:', error);
365
+ throw this.formatError(error);
366
+ }
367
+ }
368
+
369
+ // ==================== EMAIL MANAGEMENT ====================
370
+
371
+ /**
372
+ * Envoyer email de vérification
373
+ */
374
+ async sendEmailVerification() {
375
+ try {
376
+ const user = this.getCurrentUser();
377
+ if (!user) throw new Error('Utilisateur non connecté');
378
+
379
+ await user.sendEmailVerification();
380
+
381
+ return { success: true };
382
+ } catch (error) {
383
+ console.error('❌ Erreur sendEmailVerification:', error);
384
+ throw this.formatError(error);
385
+ }
386
+ }
387
+
388
+ /**
389
+ * Vérifier le code de vérification d'email
390
+ */
391
+ async applyActionCode(code) {
392
+ try {
393
+ if (!this.auth) this.initialize();
394
+
395
+ await this.auth.applyActionCode(code);
396
+ return { success: true };
397
+ } catch (error) {
398
+ console.error('❌ Erreur applyActionCode:', error);
399
+ throw this.formatError(error);
400
+ }
401
+ }
402
+
403
+ /**
404
+ * Changer l'email
405
+ */
406
+ async updateEmail(newEmail) {
407
+ try {
408
+ const user = this.getCurrentUser();
409
+ if (!user) throw new Error('Utilisateur non connecté');
410
+
411
+ await user.updateEmail(newEmail);
412
+
413
+ return { success: true };
414
+ } catch (error) {
415
+ console.error('❌ Erreur updateEmail:', error);
416
+ throw this.formatError(error);
417
+ }
418
+ }
419
+
420
+ // ==================== PROFILE MANAGEMENT ====================
421
+
422
+ /**
423
+ * Mettre à jour le profil
424
+ */
425
+ async updateProfile(updates) {
426
+ try {
427
+ const user = this.getCurrentUser();
428
+ if (!user) throw new Error('Utilisateur non connecté');
429
+
430
+ await user.updateProfile(updates);
431
+ return { success: true };
432
+ } catch (error) {
433
+ console.error('❌ Erreur updateProfile:', error);
434
+ throw this.formatError(error);
435
+ }
436
+ }
437
+
438
+ /**
439
+ * Supprimer le compte
440
+ */
441
+ async deleteAccount() {
442
+ try {
443
+ const user = this.getCurrentUser();
444
+ if (!user) throw new Error('Utilisateur non connecté');
445
+
446
+ await user.delete();
447
+ this.currentUser = null;
448
+ return { success: true };
449
+ } catch (error) {
450
+ console.error('❌ Erreur deleteAccount:', error);
451
+ throw this.formatError(error);
452
+ }
453
+ }
454
+
455
+ /**
456
+ * Ré-authentifier l'utilisateur
457
+ */
458
+ async reauthenticateWithEmail(email, password) {
459
+ try {
460
+ const user = this.getCurrentUser();
461
+ if (!user) throw new Error('Utilisateur non connecté');
462
+
463
+ const credential = firebase.auth.EmailAuthProvider.credential(email, password);
464
+ await user.reauthenticateWithCredential(credential);
465
+
466
+ return { success: true };
467
+ } catch (error) {
468
+ console.error('❌ Erreur reauthenticateWithEmail:', error);
469
+ throw this.formatError(error);
470
+ }
471
+ }
472
+
473
+ // ==================== TOKEN MANAGEMENT ====================
474
+
475
+ /**
476
+ * Obtenir le token ID
477
+ */
478
+ async getIdToken(forceRefresh = false) {
479
+ try {
480
+ const user = this.getCurrentUser();
481
+ if (!user) throw new Error('Utilisateur non connecté');
482
+
483
+ const token = await user.getIdToken(forceRefresh);
484
+ return token;
485
+ } catch (error) {
486
+ console.error('❌ Erreur getIdToken:', error);
487
+ throw this.formatError(error);
488
+ }
489
+ }
490
+
491
+ /**
492
+ * Rafraîchir le token
493
+ */
494
+ async refreshToken() {
495
+ return this.getIdToken(true);
496
+ }
497
+
498
+ // ==================== STATE MANAGEMENT ====================
499
+
500
+ /**
501
+ * Écouter les changements d'état d'authentification
502
+ */
503
+ onAuthStateChanged(callback) {
504
+ if (!this.auth) this.initialize();
505
+
506
+ const unsubscribe = this.auth.onAuthStateChanged((user) => {
507
+ this.currentUser = user;
508
+ callback(user ? this.formatUser(user) : null);
509
+ });
510
+
511
+ this.authStateListeners.push(unsubscribe);
512
+
513
+ return unsubscribe;
514
+ }
515
+
516
+ /**
517
+ * Déconnexion
518
+ */
519
+ async signOut() {
520
+ try {
521
+ if (!this.auth) this.initialize();
522
+
523
+ await this.auth.signOut();
524
+ this.currentUser = null;
525
+
526
+ return { success: true };
527
+ } catch (error) {
528
+ console.error('❌ Erreur signOut:', error);
529
+ throw this.formatError(error);
530
+ }
531
+ }
532
+
533
+ /**
534
+ * Obtenir l'utilisateur actuel
535
+ */
536
+ getCurrentUser() {
537
+ if (!this.auth) this.initialize();
538
+ return this.auth.currentUser;
539
+ }
540
+
541
+ /**
542
+ * Vérifier si l'utilisateur est connecté
543
+ */
544
+ isSignedIn() {
545
+ return this.getCurrentUser() !== null;
546
+ }
547
+
548
+ // ==================== LINKING ACCOUNTS ====================
549
+
550
+ /**
551
+ * Lier un compte email/password
552
+ */
553
+ async linkWithEmail(email, password) {
554
+ try {
555
+ const user = this.getCurrentUser();
556
+ if (!user) throw new Error('Utilisateur non connecté');
557
+
558
+ const credential = firebase.auth.EmailAuthProvider.credential(email, password);
559
+ const result = await user.linkWithCredential(credential);
560
+
561
+
562
+ return {
563
+ success: true,
564
+ user: this.formatUser(result.user)
565
+ };
566
+ } catch (error) {
567
+ console.error('❌ Erreur linkWithEmail:', error);
568
+ throw this.formatError(error);
569
+ }
570
+ }
571
+
572
+ /**
573
+ * Délier un fournisseur
574
+ */
575
+ async unlinkProvider(providerId) {
576
+ try {
577
+ const user = this.getCurrentUser();
578
+ if (!user) throw new Error('Utilisateur non connecté');
579
+
580
+ await user.unlink(providerId);
581
+
582
+ return { success: true };
583
+ } catch (error) {
584
+ console.error('❌ Erreur unlinkProvider:', error);
585
+ throw this.formatError(error);
586
+ }
587
+ }
588
+
589
+ // ==================== HELPERS ====================
590
+
591
+ /**
592
+ * Formater l'objet utilisateur
593
+ */
594
+ formatUser(user) {
595
+ if (!user) return null;
596
+
597
+ return {
598
+ uid: user.uid,
599
+ email: user.email,
600
+ displayName: user.displayName,
601
+ photoURL: user.photoURL,
602
+ phoneNumber: user.phoneNumber,
603
+ emailVerified: user.emailVerified,
604
+ isAnonymous: user.isAnonymous,
605
+ metadata: {
606
+ creationTime: user.metadata.creationTime,
607
+ lastSignInTime: user.metadata.lastSignInTime
608
+ },
609
+ providerId: user.providerId,
610
+ providerData: user.providerData
611
+ };
612
+ }
613
+
614
+ /**
615
+ * Formater les erreurs
616
+ */
617
+ formatError(error) {
618
+ const errorMessages = {
619
+ 'auth/email-already-in-use': 'Cet email est déjà utilisé',
620
+ 'auth/invalid-email': 'Email invalide',
621
+ 'auth/operation-not-allowed': 'Opération non autorisée',
622
+ 'auth/weak-password': 'Mot de passe trop faible',
623
+ 'auth/user-disabled': 'Compte désactivé',
624
+ 'auth/user-not-found': 'Utilisateur introuvable',
625
+ 'auth/wrong-password': 'Mot de passe incorrect',
626
+ 'auth/too-many-requests': 'Trop de tentatives, réessayez plus tard',
627
+ 'auth/network-request-failed': 'Erreur réseau',
628
+ 'auth/popup-closed-by-user': 'Popup fermée par l\'utilisateur',
629
+ 'auth/account-exists-with-different-credential': 'Un compte existe déjà avec cet email',
630
+ 'auth/requires-recent-login': 'Ré-authentification requise'
631
+ };
632
+
633
+ return {
634
+ code: error.code,
635
+ message: errorMessages[error.code] || error.message,
636
+ originalError: error
637
+ };
638
+ }
639
+
640
+ // ==================== CLEANUP ====================
641
+
642
+ /**
643
+ * Nettoyer les ressources
644
+ */
645
+ destroy() {
646
+ this.authStateListeners.forEach(unsubscribe => unsubscribe());
647
+ this.authStateListeners = [];
648
+ this.auth = null;
649
+ this.currentUser = null;
650
+ }
651
+ }
652
+
653
+ export default FirebaseAuth;