@edgedev/firebase 1.9.15 → 1.9.17

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.
Files changed (2) hide show
  1. package/edgeFirebase.ts +79 -1
  2. package/package.json +1 -1
package/edgeFirebase.ts CHANGED
@@ -46,7 +46,11 @@ import {
46
46
  OAuthProvider,
47
47
  browserPopupRedirectResolver,
48
48
  signInWithPopup,
49
+ signInWithPhoneNumber,
49
50
  updateEmail,
51
+ RecaptchaVerifier,
52
+ ConfirmationResult,
53
+ PhoneAuthProvider,
50
54
  } from "firebase/auth";
51
55
 
52
56
  import { getFunctions, httpsCallable, connectFunctionsEmulator } from "firebase/functions";
@@ -85,7 +89,7 @@ interface permissions {
85
89
 
86
90
  type action = "assign" | "read" | "write" | "delete";
87
91
 
88
- type authProviders = "email" | "microsoft" | "google" | "facebook" | "github" | "twitter" | "apple";
92
+ type authProviders = "email" | "microsoft" | "google" | "facebook" | "github" | "twitter" | "apple" | "phone";
89
93
 
90
94
  interface role {
91
95
  collectionPath: "-" | string; // - is root
@@ -129,6 +133,8 @@ interface newUser {
129
133
  interface userRegister {
130
134
  uid?: string;
131
135
  email?: string;
136
+ phoneCode?: string;
137
+ confirmationResult?: ConfirmationResult;
132
138
  password?: string;
133
139
  meta: object;
134
140
  registrationCode: string;
@@ -209,6 +215,7 @@ export const EdgeFirebase = class {
209
215
  } else {
210
216
  this.auth = initializeAuth(this.app, { persistence });
211
217
  }
218
+
212
219
  if (this.firebaseConfig.emulatorAuth) {
213
220
  connectAuthEmulator(this.auth, `http://localhost:${this.firebaseConfig.emulatorAuth}`)
214
221
  }
@@ -411,6 +418,8 @@ export const EdgeFirebase = class {
411
418
 
412
419
  private setOnAuthStateChanged = (): void => {
413
420
  onAuthStateChanged(this.auth, (userAuth) => {
421
+ const oldDiv = document.getElementById("recaptcha-container");
422
+ if (oldDiv) oldDiv.remove();
414
423
  if (userAuth) {
415
424
  this.user.loggingIn = true;
416
425
  this.user.email = userAuth.email;
@@ -432,6 +441,35 @@ export const EdgeFirebase = class {
432
441
  });
433
442
  };
434
443
 
444
+ public logInWithPhone = async (confirmationResult: ConfirmationResult, phoneCode: string): Promise<void> => {
445
+ const oldDiv = document.getElementById("recaptcha-container");
446
+ if (oldDiv) oldDiv.remove();
447
+ try {
448
+ console.log(confirmationResult)
449
+ const result = await confirmationResult.confirm(phoneCode);
450
+ if (!Object.prototype.hasOwnProperty.call(result, "user")) {
451
+ this.user.logInError = true;
452
+ this.user.logInErrorMessage = JSON.stringify(result)
453
+ this.logOut();
454
+ return;
455
+ }
456
+ console.log(result.user.uid);
457
+ const userRef = doc(this.db, "users", result.user.uid);
458
+ const userSnap = await getDoc(userRef);
459
+ if (!userSnap.exists()) {
460
+ this.user.logInError = true;
461
+ this.user.logInErrorMessage = "User does not exist";
462
+ this.logOut();
463
+ return;
464
+ }
465
+ } catch (error) {
466
+ this.user.logInError = true;
467
+ this.user.logInErrorMessage = error.message;
468
+ this.logOut();
469
+ return;
470
+ }
471
+ };
472
+
435
473
  public logInWithMicrosoft = async (providerScopes: string[] = []): Promise<void> => {
436
474
  const result = await this.signInWithMicrosoft(providerScopes);
437
475
  if (!Object.prototype.hasOwnProperty.call(result, "user")) {
@@ -497,6 +535,34 @@ export const EdgeFirebase = class {
497
535
  }
498
536
  }
499
537
 
538
+ public sendPhoneCode = async (phone: string): Promise<any> => {
539
+ const oldDiv = document.getElementById("recaptcha-container");
540
+ if (oldDiv) oldDiv.remove();
541
+
542
+ const newDiv = document.createElement("div");
543
+ newDiv.setAttribute("id", "recaptcha-container");
544
+ document.body.appendChild(newDiv);
545
+
546
+ const recaptchaVerifier = new RecaptchaVerifier("recaptcha-container", {
547
+ 'size': 'invisible',
548
+ 'callback': (response) => {
549
+ console.log(response)
550
+ }
551
+ }, this.auth);
552
+
553
+ try {
554
+ const confirmationResult = await signInWithPhoneNumber(this.auth, phone, recaptchaVerifier);
555
+ return confirmationResult
556
+ } catch (error) {
557
+ this.user.logInError = true;
558
+ this.user.logInErrorMessage = error.message;
559
+ this.logOut();
560
+ return false;
561
+ }
562
+ }
563
+
564
+
565
+
500
566
  public registerUser = async (
501
567
  userRegister: userRegister,
502
568
  authProvider: authProviders = "email",
@@ -542,6 +608,17 @@ export const EdgeFirebase = class {
542
608
  }
543
609
  } else if (authProvider === "microsoft") {
544
610
  response = await this.registerUserWithMicrosoft(providerScopes);
611
+ } else if (authProvider === "phone") {
612
+ const oldDiv = document.getElementById("recaptcha-container");
613
+ if (oldDiv) oldDiv.remove();
614
+ try {
615
+ response = await userRegister.confirmationResult.confirm(userRegister.phoneCode);
616
+ } catch (error) {
617
+ // Handle the case where the code is incorrect or expired
618
+ response = error;
619
+ }
620
+
621
+
545
622
  }
546
623
  if (!Object.prototype.hasOwnProperty.call(response, "user")) {
547
624
  return this.sendResponse({
@@ -1009,6 +1086,7 @@ export const EdgeFirebase = class {
1009
1086
  })
1010
1087
  };
1011
1088
 
1089
+
1012
1090
  public logIn = (credentials: Credentials): void => {
1013
1091
  this.logOut();
1014
1092
  signInWithEmailAndPassword(
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@edgedev/firebase",
3
- "version": "1.9.15",
3
+ "version": "1.9.17",
4
4
  "description": "Vue 3 / Nuxt 3 Plugin or Nuxt 3 plugin for firebase authentication and firestore.",
5
5
  "main": "index.ts",
6
6
  "scripts": {