@edgedev/firebase 1.9.14 → 1.9.16

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 +70 -2
  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
  }
@@ -239,6 +246,10 @@ export const EdgeFirebase = class {
239
246
 
240
247
  private functions = null;
241
248
 
249
+ public createRecaptchaVerifier = (containerId, parameters = { size: 'invisible' }) => {
250
+ return new RecaptchaVerifier(containerId, parameters, this.auth);
251
+ }
252
+
242
253
  public runFunction = async (functionName: string, data: Record<string, unknown>) => {
243
254
  data.uid = this.user.uid;
244
255
  const callable = httpsCallable(this.functions, functionName);
@@ -432,6 +443,30 @@ export const EdgeFirebase = class {
432
443
  });
433
444
  };
434
445
 
446
+ public logInWithPhone = async (confirmationResult: ConfirmationResult, phoneCode: string): Promise<void> => {
447
+ try {
448
+ const result = await confirmationResult.confirm(phoneCode);
449
+ if (!Object.prototype.hasOwnProperty.call(result, "user")) {
450
+ this.user.logInError = true;
451
+ this.user.logInErrorMessage = JSON.stringify(result)
452
+ this.logOut();
453
+ return;
454
+ }
455
+ console.log(result.user.uid);
456
+ const userRef = doc(this.db, "users", result.user.uid);
457
+ const userSnap = await getDoc(userRef);
458
+ if (!userSnap.exists()) {
459
+ this.user.logInError = true;
460
+ this.user.logInErrorMessage = "User does not exist";
461
+ this.logOut();
462
+ }
463
+ } catch (error) {
464
+ this.user.logInError = true;
465
+ this.user.logInErrorMessage = error.message;
466
+ this.logOut();
467
+ }
468
+ };
469
+
435
470
  public logInWithMicrosoft = async (providerScopes: string[] = []): Promise<void> => {
436
471
  const result = await this.signInWithMicrosoft(providerScopes);
437
472
  if (!Object.prototype.hasOwnProperty.call(result, "user")) {
@@ -441,7 +476,7 @@ export const EdgeFirebase = class {
441
476
  return;
442
477
  }
443
478
  console.log(result.user.uid);
444
- const userRef = doc(this.db, "staged-users", result.user.uid);
479
+ const userRef = doc(this.db, "users", result.user.uid);
445
480
  const userSnap = await getDoc(userRef);
446
481
  if (!userSnap.exists()) {
447
482
  this.user.logInError = true;
@@ -497,6 +532,28 @@ export const EdgeFirebase = class {
497
532
  }
498
533
  }
499
534
 
535
+ public sendPhoneCode = async (phone: string): Promise<any> => {
536
+ const newDiv = document.createElement("div");
537
+ newDiv.setAttribute("id", "recaptcha-container");
538
+ document.body.appendChild(newDiv);
539
+
540
+ const recaptchaVerifier = new RecaptchaVerifier("recaptcha-container", {
541
+ 'size': 'invisible',
542
+ 'callback': (response) => {
543
+ console.log(response)
544
+ }
545
+ }, this.auth);
546
+
547
+ try {
548
+ const confirmationResult = await signInWithPhoneNumber(this.auth, phone, recaptchaVerifier);
549
+ return confirmationResult
550
+ } catch (error) {
551
+ return error
552
+ }
553
+ }
554
+
555
+
556
+
500
557
  public registerUser = async (
501
558
  userRegister: userRegister,
502
559
  authProvider: authProviders = "email",
@@ -542,6 +599,16 @@ export const EdgeFirebase = class {
542
599
  }
543
600
  } else if (authProvider === "microsoft") {
544
601
  response = await this.registerUserWithMicrosoft(providerScopes);
602
+ } else if (authProvider === "phone") {
603
+ // Try to sign in with the code
604
+ try {
605
+ response = await userRegister.confirmationResult.confirm(userRegister.phoneCode);
606
+ } catch (error) {
607
+ // Handle the case where the code is incorrect or expired
608
+ response = error;
609
+ }
610
+
611
+
545
612
  }
546
613
  if (!Object.prototype.hasOwnProperty.call(response, "user")) {
547
614
  return this.sendResponse({
@@ -1009,6 +1076,7 @@ export const EdgeFirebase = class {
1009
1076
  })
1010
1077
  };
1011
1078
 
1079
+
1012
1080
  public logIn = (credentials: Credentials): void => {
1013
1081
  this.logOut();
1014
1082
  signInWithEmailAndPassword(
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@edgedev/firebase",
3
- "version": "1.9.14",
3
+ "version": "1.9.16",
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": {