@react-native-firebase/auth 24.1.1 → 25.0.0

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 (216) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/android/src/main/java/io/invertase/firebase/auth/ReactNativeFirebaseAuthModule.java +16 -4
  3. package/dist/module/ActionCodeURL.js +140 -0
  4. package/dist/module/ActionCodeURL.js.map +1 -0
  5. package/{lib → dist/module}/ConfirmationResult.js +4 -5
  6. package/dist/module/ConfirmationResult.js.map +1 -0
  7. package/{lib → dist/module}/MultiFactorResolver.js +14 -5
  8. package/dist/module/MultiFactorResolver.js.map +1 -0
  9. package/{lib → dist/module}/PhoneAuthListener.js +52 -75
  10. package/dist/module/PhoneAuthListener.js.map +1 -0
  11. package/dist/module/PhoneAuthState.js +25 -0
  12. package/dist/module/PhoneAuthState.js.map +1 -0
  13. package/dist/module/PhoneMultiFactorGenerator.js +39 -0
  14. package/dist/module/PhoneMultiFactorGenerator.js.map +1 -0
  15. package/{lib → dist/module}/Settings.js +3 -7
  16. package/dist/module/Settings.js.map +1 -0
  17. package/{lib → dist/module}/TotpMultiFactorGenerator.js +20 -15
  18. package/dist/module/TotpMultiFactorGenerator.js.map +1 -0
  19. package/{lib → dist/module}/TotpSecret.js +7 -5
  20. package/dist/module/TotpSecret.js.map +1 -0
  21. package/dist/module/User.js +245 -0
  22. package/dist/module/User.js.map +1 -0
  23. package/dist/module/constants.js +54 -0
  24. package/dist/module/constants.js.map +1 -0
  25. package/dist/module/credentials/AuthCredential.js +51 -0
  26. package/dist/module/credentials/AuthCredential.js.map +1 -0
  27. package/dist/module/credentials/EmailAuthCredential.js +51 -0
  28. package/dist/module/credentials/EmailAuthCredential.js.map +1 -0
  29. package/dist/module/credentials/OAuthCredential.js +84 -0
  30. package/dist/module/credentials/OAuthCredential.js.map +1 -0
  31. package/dist/module/credentials/PhoneAuthCredential.js +50 -0
  32. package/dist/module/credentials/PhoneAuthCredential.js.map +1 -0
  33. package/{lib/providers/GoogleAuthProvider.js → dist/module/credentials/index.js} +7 -19
  34. package/dist/module/credentials/index.js.map +1 -0
  35. package/{lib → dist/module}/getMultiFactorResolver.js +5 -8
  36. package/dist/module/getMultiFactorResolver.js.map +1 -0
  37. package/{lib/providers/FacebookAuthProvider.js → dist/module/index.js} +11 -18
  38. package/dist/module/index.js.map +1 -0
  39. package/dist/module/modular.js +779 -0
  40. package/dist/module/modular.js.map +1 -0
  41. package/{lib → dist/module}/multiFactor.js +16 -14
  42. package/dist/module/multiFactor.js.map +1 -0
  43. package/dist/module/namespaced.js +522 -0
  44. package/dist/module/namespaced.js.map +1 -0
  45. package/dist/module/package.json +1 -0
  46. package/dist/module/password-policy/PasswordPolicyImpl.js +103 -0
  47. package/dist/module/password-policy/PasswordPolicyImpl.js.map +1 -0
  48. package/{lib → dist/module}/password-policy/PasswordPolicyMixin.js +14 -16
  49. package/dist/module/password-policy/PasswordPolicyMixin.js.map +1 -0
  50. package/{lib → dist/module}/password-policy/passwordPolicyApi.js +18 -12
  51. package/dist/module/password-policy/passwordPolicyApi.js.map +1 -0
  52. package/{lib → dist/module}/providers/AppleAuthProvider.js +14 -7
  53. package/dist/module/providers/AppleAuthProvider.js.map +1 -0
  54. package/{lib → dist/module}/providers/EmailAuthProvider.js +10 -25
  55. package/dist/module/providers/EmailAuthProvider.js.map +1 -0
  56. package/dist/module/providers/FacebookAuthProvider.js +56 -0
  57. package/dist/module/providers/FacebookAuthProvider.js.map +1 -0
  58. package/dist/module/providers/GithubAuthProvider.js +49 -0
  59. package/dist/module/providers/GithubAuthProvider.js.map +1 -0
  60. package/dist/module/providers/GoogleAuthProvider.js +57 -0
  61. package/dist/module/providers/GoogleAuthProvider.js.map +1 -0
  62. package/dist/module/providers/OAuthProvider.js +116 -0
  63. package/dist/module/providers/OAuthProvider.js.map +1 -0
  64. package/{lib → dist/module}/providers/OIDCAuthProvider.js +15 -7
  65. package/dist/module/providers/OIDCAuthProvider.js.map +1 -0
  66. package/dist/module/providers/PhoneAuthProvider.js +87 -0
  67. package/dist/module/providers/PhoneAuthProvider.js.map +1 -0
  68. package/dist/module/providers/TwitterAuthProvider.js +50 -0
  69. package/dist/module/providers/TwitterAuthProvider.js.map +1 -0
  70. package/dist/module/types/auth.js +43 -0
  71. package/dist/module/types/auth.js.map +1 -0
  72. package/dist/module/types/internal.js +4 -0
  73. package/dist/module/types/internal.js.map +1 -0
  74. package/dist/module/types/namespaced.js +62 -0
  75. package/dist/module/types/namespaced.js.map +1 -0
  76. package/dist/module/version.js +5 -0
  77. package/dist/module/version.js.map +1 -0
  78. package/dist/module/web/RNFBAuthModule.android.js +5 -0
  79. package/dist/module/web/RNFBAuthModule.android.js.map +1 -0
  80. package/dist/module/web/RNFBAuthModule.ios.js +5 -0
  81. package/dist/module/web/RNFBAuthModule.ios.js.map +1 -0
  82. package/{lib → dist/module}/web/RNFBAuthModule.js +104 -321
  83. package/dist/module/web/RNFBAuthModule.js.map +1 -0
  84. package/dist/typescript/lib/ActionCodeURL.d.ts +41 -0
  85. package/dist/typescript/lib/ActionCodeURL.d.ts.map +1 -0
  86. package/dist/typescript/lib/ConfirmationResult.d.ts +10 -0
  87. package/dist/typescript/lib/ConfirmationResult.d.ts.map +1 -0
  88. package/dist/typescript/lib/MultiFactorResolver.d.ts +24 -0
  89. package/dist/typescript/lib/MultiFactorResolver.d.ts.map +1 -0
  90. package/dist/typescript/lib/PhoneAuthListener.d.ts +34 -0
  91. package/dist/typescript/lib/PhoneAuthListener.d.ts.map +1 -0
  92. package/dist/typescript/lib/PhoneAuthState.d.ts +7 -0
  93. package/dist/typescript/lib/PhoneAuthState.d.ts.map +1 -0
  94. package/dist/typescript/lib/PhoneMultiFactorGenerator.d.ts +7 -0
  95. package/dist/typescript/lib/PhoneMultiFactorGenerator.d.ts.map +1 -0
  96. package/dist/typescript/lib/Settings.d.ts +13 -0
  97. package/dist/typescript/lib/Settings.d.ts.map +1 -0
  98. package/dist/typescript/lib/TotpMultiFactorGenerator.d.ts +11 -0
  99. package/dist/typescript/lib/TotpMultiFactorGenerator.d.ts.map +1 -0
  100. package/dist/typescript/lib/TotpSecret.d.ts +33 -0
  101. package/dist/typescript/lib/TotpSecret.d.ts.map +1 -0
  102. package/dist/typescript/lib/User.d.ts +51 -0
  103. package/dist/typescript/lib/User.d.ts.map +1 -0
  104. package/dist/typescript/lib/constants.d.ts +35 -0
  105. package/dist/typescript/lib/constants.d.ts.map +1 -0
  106. package/dist/typescript/lib/credentials/AuthCredential.d.ts +12 -0
  107. package/dist/typescript/lib/credentials/AuthCredential.d.ts.map +1 -0
  108. package/dist/typescript/lib/credentials/EmailAuthCredential.d.ts +8 -0
  109. package/dist/typescript/lib/credentials/EmailAuthCredential.d.ts.map +1 -0
  110. package/dist/typescript/lib/credentials/OAuthCredential.d.ts +22 -0
  111. package/dist/typescript/lib/credentials/OAuthCredential.d.ts.map +1 -0
  112. package/dist/typescript/lib/credentials/PhoneAuthCredential.d.ts +8 -0
  113. package/dist/typescript/lib/credentials/PhoneAuthCredential.d.ts.map +1 -0
  114. package/dist/typescript/lib/credentials/index.d.ts +5 -0
  115. package/dist/typescript/lib/credentials/index.d.ts.map +1 -0
  116. package/dist/typescript/lib/getMultiFactorResolver.d.ts +19 -0
  117. package/dist/typescript/lib/getMultiFactorResolver.d.ts.map +1 -0
  118. package/dist/typescript/lib/index.d.ts +8 -0
  119. package/dist/typescript/lib/index.d.ts.map +1 -0
  120. package/dist/typescript/lib/modular.d.ts +360 -0
  121. package/dist/typescript/lib/modular.d.ts.map +1 -0
  122. package/dist/typescript/lib/multiFactor.d.ts +26 -0
  123. package/dist/typescript/lib/multiFactor.d.ts.map +1 -0
  124. package/dist/typescript/lib/namespaced.d.ts +12 -0
  125. package/dist/typescript/lib/namespaced.d.ts.map +1 -0
  126. package/dist/typescript/lib/password-policy/PasswordPolicyImpl.d.ts +19 -0
  127. package/dist/typescript/lib/password-policy/PasswordPolicyImpl.d.ts.map +1 -0
  128. package/dist/typescript/lib/password-policy/PasswordPolicyMixin.d.ts +11 -0
  129. package/dist/typescript/lib/password-policy/PasswordPolicyMixin.d.ts.map +1 -0
  130. package/dist/typescript/lib/password-policy/passwordPolicyApi.d.ts +10 -0
  131. package/dist/typescript/lib/password-policy/passwordPolicyApi.d.ts.map +1 -0
  132. package/dist/typescript/lib/providers/AppleAuthProvider.d.ts +16 -0
  133. package/dist/typescript/lib/providers/AppleAuthProvider.d.ts.map +1 -0
  134. package/dist/typescript/lib/providers/EmailAuthProvider.d.ts +11 -0
  135. package/dist/typescript/lib/providers/EmailAuthProvider.d.ts.map +1 -0
  136. package/dist/typescript/lib/providers/FacebookAuthProvider.d.ts +19 -0
  137. package/dist/typescript/lib/providers/FacebookAuthProvider.d.ts.map +1 -0
  138. package/dist/typescript/lib/providers/GithubAuthProvider.d.ts +15 -0
  139. package/dist/typescript/lib/providers/GithubAuthProvider.d.ts.map +1 -0
  140. package/dist/typescript/lib/providers/GoogleAuthProvider.d.ts +21 -0
  141. package/dist/typescript/lib/providers/GoogleAuthProvider.d.ts.map +1 -0
  142. package/dist/typescript/lib/providers/OAuthProvider.d.ts +33 -0
  143. package/dist/typescript/lib/providers/OAuthProvider.d.ts.map +1 -0
  144. package/dist/typescript/lib/providers/OIDCAuthProvider.d.ts +16 -0
  145. package/dist/typescript/lib/providers/OIDCAuthProvider.d.ts.map +1 -0
  146. package/dist/typescript/lib/providers/PhoneAuthProvider.d.ts +16 -0
  147. package/dist/typescript/lib/providers/PhoneAuthProvider.d.ts.map +1 -0
  148. package/dist/typescript/lib/providers/TwitterAuthProvider.d.ts +15 -0
  149. package/dist/typescript/lib/providers/TwitterAuthProvider.d.ts.map +1 -0
  150. package/dist/typescript/lib/types/auth.d.ts +305 -0
  151. package/dist/typescript/lib/types/auth.d.ts.map +1 -0
  152. package/dist/typescript/lib/types/internal.d.ts +292 -0
  153. package/dist/typescript/lib/types/internal.d.ts.map +1 -0
  154. package/dist/typescript/lib/types/namespaced.d.ts +2185 -0
  155. package/dist/typescript/lib/types/namespaced.d.ts.map +1 -0
  156. package/dist/typescript/lib/version.d.ts +2 -0
  157. package/dist/typescript/lib/version.d.ts.map +1 -0
  158. package/dist/typescript/lib/web/RNFBAuthModule.android.d.ts +3 -0
  159. package/dist/typescript/lib/web/RNFBAuthModule.android.d.ts.map +1 -0
  160. package/dist/typescript/lib/web/RNFBAuthModule.d.ts +406 -0
  161. package/dist/typescript/lib/web/RNFBAuthModule.d.ts.map +1 -0
  162. package/dist/typescript/lib/web/RNFBAuthModule.ios.d.ts +3 -0
  163. package/dist/typescript/lib/web/RNFBAuthModule.ios.d.ts.map +1 -0
  164. package/dist/typescript/package.json +1 -0
  165. package/ios/RNFBAuth/RNFBAuthModule.m +4 -2
  166. package/lib/ActionCodeURL.ts +174 -0
  167. package/lib/ConfirmationResult.ts +42 -0
  168. package/lib/MultiFactorResolver.ts +51 -0
  169. package/lib/PhoneAuthListener.ts +312 -0
  170. package/lib/PhoneAuthState.ts +22 -0
  171. package/lib/{PhoneMultiFactorGenerator.js → PhoneMultiFactorGenerator.ts} +8 -2
  172. package/lib/Settings.ts +59 -0
  173. package/lib/TotpMultiFactorGenerator.ts +78 -0
  174. package/lib/TotpSecret.ts +71 -0
  175. package/lib/{User.js → User.ts} +127 -83
  176. package/lib/constants.ts +55 -0
  177. package/lib/credentials/AuthCredential.ts +55 -0
  178. package/lib/credentials/EmailAuthCredential.ts +70 -0
  179. package/lib/credentials/OAuthCredential.ts +113 -0
  180. package/lib/credentials/PhoneAuthCredential.ts +66 -0
  181. package/lib/{providers/GithubAuthProvider.js → credentials/index.ts} +4 -19
  182. package/lib/getMultiFactorResolver.ts +40 -0
  183. package/lib/{providers/TwitterAuthProvider.js → index.ts} +14 -19
  184. package/lib/modular.ts +1158 -0
  185. package/lib/multiFactor.ts +83 -0
  186. package/lib/namespaced.ts +846 -0
  187. package/lib/password-policy/{PasswordPolicyImpl.js → PasswordPolicyImpl.ts} +37 -19
  188. package/lib/password-policy/PasswordPolicyMixin.ts +83 -0
  189. package/lib/password-policy/passwordPolicyApi.ts +60 -0
  190. package/lib/providers/AppleAuthProvider.ts +44 -0
  191. package/lib/providers/EmailAuthProvider.ts +42 -0
  192. package/lib/providers/FacebookAuthProvider.ts +58 -0
  193. package/lib/providers/GithubAuthProvider.ts +51 -0
  194. package/lib/providers/GoogleAuthProvider.ts +60 -0
  195. package/lib/providers/OAuthProvider.ts +150 -0
  196. package/lib/providers/OIDCAuthProvider.ts +49 -0
  197. package/lib/providers/PhoneAuthProvider.ts +161 -0
  198. package/lib/providers/TwitterAuthProvider.ts +52 -0
  199. package/lib/types/auth.ts +393 -0
  200. package/lib/types/internal.ts +474 -0
  201. package/lib/{index.d.ts → types/namespaced.ts} +52 -41
  202. package/lib/version.ts +2 -0
  203. package/lib/web/RNFBAuthModule.ts +1359 -0
  204. package/package.json +41 -9
  205. package/plugin/tsconfig.tsbuildinfo +1 -1
  206. package/tsconfig.json +26 -0
  207. package/tsdoc.json +22 -0
  208. package/typedoc.json +9 -2
  209. package/lib/index.js +0 -572
  210. package/lib/modular/index.d.ts +0 -801
  211. package/lib/modular/index.js +0 -651
  212. package/lib/providers/OAuthProvider.js +0 -70
  213. package/lib/providers/PhoneAuthProvider.js +0 -52
  214. package/lib/version.js +0 -2
  215. /package/lib/web/{RNFBAuthModule.android.js → RNFBAuthModule.android.ts} +0 -0
  216. /package/lib/web/{RNFBAuthModule.ios.js → RNFBAuthModule.ios.ts} +0 -0
@@ -0,0 +1,42 @@
1
+ /*
2
+ * Copyright (c) 2016-present Invertase Limited & Contributors
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this library except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ *
16
+ */
17
+
18
+ import type { FirebaseAuthTypes } from './types/namespaced';
19
+ import type { AuthInternal } from './types/internal';
20
+
21
+ export default class ConfirmationResult {
22
+ private readonly _auth: AuthInternal;
23
+ private readonly _verificationId: string;
24
+
25
+ constructor(auth: AuthInternal, verificationId: string) {
26
+ this._auth = auth;
27
+ this._verificationId = verificationId;
28
+ }
29
+
30
+ confirm(verificationCode: string): Promise<FirebaseAuthTypes.UserCredential | null> {
31
+ return this._auth.native
32
+ .confirmationResultConfirm(verificationCode)
33
+ .then(
34
+ userCredential =>
35
+ this._auth._setUserCredential(userCredential) as FirebaseAuthTypes.UserCredential,
36
+ );
37
+ }
38
+
39
+ get verificationId(): string {
40
+ return this._verificationId;
41
+ }
42
+ }
@@ -0,0 +1,51 @@
1
+ /**
2
+ * Base class to facilitate multi-factor authentication.
3
+ */
4
+ import type { FirebaseAuthTypes } from './types/namespaced';
5
+ import type { AuthInternal } from './types/internal';
6
+
7
+ type ResolverLike = {
8
+ hints: FirebaseAuthTypes.MultiFactorInfo[];
9
+ session: FirebaseAuthTypes.MultiFactorSession;
10
+ };
11
+
12
+ type PhoneMultiFactorAssertion = {
13
+ token?: string;
14
+ secret?: string;
15
+ uid?: string;
16
+ verificationCode?: string;
17
+ };
18
+
19
+ export default class MultiFactorResolver {
20
+ readonly hints: FirebaseAuthTypes.MultiFactorInfo[];
21
+ readonly session: FirebaseAuthTypes.MultiFactorSession;
22
+ private readonly _auth: AuthInternal;
23
+
24
+ constructor(auth: AuthInternal, resolver: ResolverLike) {
25
+ this._auth = auth;
26
+ this.hints = resolver.hints;
27
+ this.session = resolver.session;
28
+ }
29
+
30
+ resolveSignIn(assertion: PhoneMultiFactorAssertion): Promise<FirebaseAuthTypes.UserCredential> {
31
+ const { token, secret, uid, verificationCode } = assertion;
32
+
33
+ if (token && secret) {
34
+ return this._auth.resolveMultiFactorSignIn(
35
+ this.session,
36
+ token,
37
+ secret,
38
+ ) as Promise<FirebaseAuthTypes.UserCredential>;
39
+ }
40
+
41
+ if (uid && verificationCode) {
42
+ return this._auth.resolveTotpSignIn(
43
+ this.session,
44
+ uid,
45
+ verificationCode,
46
+ ) as Promise<FirebaseAuthTypes.UserCredential>;
47
+ }
48
+
49
+ throw new Error('Invalid multi-factor assertion provided for sign-in resolution.');
50
+ }
51
+ }
@@ -0,0 +1,312 @@
1
+ /*
2
+ * Copyright (c) 2016-present Invertase Limited & Contributors
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this library except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ *
16
+ */
17
+
18
+ import {
19
+ isAndroid,
20
+ isFunction,
21
+ isIOS,
22
+ promiseDefer,
23
+ } from '@react-native-firebase/app/dist/module/common';
24
+ import type { ReactNativeFirebase } from '@react-native-firebase/app';
25
+ import NativeFirebaseError from '@react-native-firebase/app/dist/module/internal/NativeFirebaseError';
26
+ import type { FirebaseAuthTypes } from './types/namespaced';
27
+ import type {
28
+ AuthInternal,
29
+ NativePhoneAuthCredentialInternal,
30
+ NativePhoneAuthErrorInternal,
31
+ } from './types/internal';
32
+
33
+ type PhoneAuthInternalEventType =
34
+ | 'codeSent'
35
+ | 'verificationFailed'
36
+ | 'verificationComplete'
37
+ | 'codeAutoRetrievalTimeout';
38
+
39
+ type InternalEvents = Record<PhoneAuthInternalEventType, string>;
40
+ type PublicEvents = Record<'error' | 'event' | 'success', string>;
41
+ type PhoneAuthSnapshot = FirebaseAuthTypes.PhoneAuthSnapshot;
42
+ type PhoneAuthError = FirebaseAuthTypes.PhoneAuthError;
43
+
44
+ let REQUEST_ID = 0;
45
+
46
+ export default class PhoneAuthListener {
47
+ private readonly _auth: AuthInternal;
48
+ private _reject: ((error: ReactNativeFirebase.NativeFirebaseError) => void) | null;
49
+ private _resolve: ((snapshot: PhoneAuthSnapshot) => void) | null;
50
+ private _promise: Promise<PhoneAuthSnapshot> | null;
51
+ private readonly _jsStack: string;
52
+ private readonly _timeout: number;
53
+ private readonly _phoneAuthRequestId: number;
54
+ private readonly _forceResending: boolean;
55
+ private readonly _internalEvents: InternalEvents;
56
+ private readonly _publicEvents: PublicEvents;
57
+
58
+ constructor(auth: AuthInternal, phoneNumber: string, timeout?: number, forceResend?: boolean) {
59
+ this._auth = auth;
60
+ this._reject = null;
61
+ this._resolve = null;
62
+ this._promise = null;
63
+ this._jsStack = new Error().stack ?? '';
64
+
65
+ this._timeout = timeout || 20;
66
+ this._phoneAuthRequestId = REQUEST_ID++;
67
+ this._forceResending = forceResend || false;
68
+
69
+ // internal events
70
+ this._internalEvents = {
71
+ codeSent: `phone:auth:${this._phoneAuthRequestId}:onCodeSent`,
72
+ verificationFailed: `phone:auth:${this._phoneAuthRequestId}:onVerificationFailed`,
73
+ verificationComplete: `phone:auth:${this._phoneAuthRequestId}:onVerificationComplete`,
74
+ codeAutoRetrievalTimeout: `phone:auth:${this._phoneAuthRequestId}:onCodeAutoRetrievalTimeout`,
75
+ };
76
+
77
+ // user observer events
78
+ this._publicEvents = {
79
+ error: `phone:auth:${this._phoneAuthRequestId}:error`,
80
+ event: `phone:auth:${this._phoneAuthRequestId}:event`,
81
+ success: `phone:auth:${this._phoneAuthRequestId}:success`,
82
+ };
83
+
84
+ this._subscribeToEvents();
85
+
86
+ if (isAndroid) {
87
+ this._auth.native.verifyPhoneNumber(
88
+ phoneNumber,
89
+ this._phoneAuthRequestId + '',
90
+ this._timeout,
91
+ this._forceResending,
92
+ );
93
+ }
94
+
95
+ if (isIOS) {
96
+ this._auth.native.verifyPhoneNumber(phoneNumber, this._phoneAuthRequestId + '');
97
+ }
98
+ }
99
+
100
+ private _subscribeToEvents(): void {
101
+ const events: PhoneAuthInternalEventType[] = [
102
+ 'codeSent',
103
+ 'verificationFailed',
104
+ 'verificationComplete',
105
+ 'codeAutoRetrievalTimeout',
106
+ ];
107
+
108
+ for (const type of events) {
109
+ const subscription = this._auth.emitter.addListener(this._internalEvents[type], event => {
110
+ switch (type) {
111
+ case 'codeSent':
112
+ this._codeSentHandler(event as NativePhoneAuthCredentialInternal);
113
+ break;
114
+ case 'verificationFailed':
115
+ this._verificationFailedHandler(event as NativePhoneAuthErrorInternal);
116
+ break;
117
+ case 'verificationComplete':
118
+ this._verificationCompleteHandler(event as NativePhoneAuthCredentialInternal);
119
+ break;
120
+ case 'codeAutoRetrievalTimeout':
121
+ this._codeAutoRetrievalTimeoutHandler(event as NativePhoneAuthCredentialInternal);
122
+ break;
123
+ }
124
+ subscription.remove();
125
+ });
126
+ }
127
+ }
128
+
129
+ private _addUserObserver(observer: (snapshot: PhoneAuthSnapshot) => void): void {
130
+ this._auth.emitter.addListener(this._publicEvents.event, observer);
131
+ }
132
+
133
+ private _emitToObservers(snapshot: PhoneAuthSnapshot): void {
134
+ this._auth.emitter.emit(this._publicEvents.event, snapshot);
135
+ }
136
+
137
+ private _emitToErrorCb(snapshot: PhoneAuthSnapshot): void {
138
+ const { error } = snapshot;
139
+ if (this._reject && error) {
140
+ this._reject(error);
141
+ }
142
+ this._auth.emitter.emit(this._publicEvents.error, {
143
+ code: error?.code ?? null,
144
+ verificationId: snapshot.verificationId,
145
+ message: error?.message ?? null,
146
+ stack: error?.stack ?? null,
147
+ } as PhoneAuthError);
148
+ }
149
+
150
+ private _emitToSuccessCb(snapshot: PhoneAuthSnapshot): void {
151
+ if (this._resolve) {
152
+ this._resolve(snapshot);
153
+ }
154
+ this._auth.emitter.emit(this._publicEvents.success, snapshot);
155
+ }
156
+
157
+ private _removeAllListeners(): void {
158
+ setTimeout(() => {
159
+ // move to next event loop - not sure if needed
160
+ // internal listeners
161
+ Object.values(this._internalEvents).forEach(event => {
162
+ this._auth.emitter.removeAllListeners(event);
163
+ });
164
+
165
+ // user observer listeners
166
+ Object.values(this._publicEvents).forEach(publicEvent => {
167
+ this._auth.emitter.removeAllListeners(publicEvent);
168
+ });
169
+ }, 0);
170
+ }
171
+
172
+ private _promiseDeferred(): void {
173
+ if (!this._promise) {
174
+ const { promise, resolve, reject } = promiseDefer() as {
175
+ promise: Promise<PhoneAuthSnapshot>;
176
+ resolve: (snapshot: PhoneAuthSnapshot) => void;
177
+ reject: (error: ReactNativeFirebase.NativeFirebaseError) => void;
178
+ };
179
+ this._promise = promise;
180
+ this._resolve = resolve;
181
+ this._reject = reject;
182
+ }
183
+ }
184
+
185
+ /* --------------------------
186
+ --- INTERNAL EVENT HANDLERS
187
+ ---------------------------- */
188
+
189
+ private _codeSentHandler(credential: NativePhoneAuthCredentialInternal): void {
190
+ const snapshot: PhoneAuthSnapshot = {
191
+ verificationId: credential.verificationId,
192
+ code: null,
193
+ error: null,
194
+ state: 'sent',
195
+ };
196
+
197
+ this._emitToObservers(snapshot);
198
+
199
+ if (isIOS) {
200
+ this._emitToSuccessCb(snapshot);
201
+ }
202
+
203
+ if (isAndroid) {
204
+ // android can auto retrieve so we don't emit to successCb immediately,
205
+ // if auto retrieve times out then that will emit to successCb
206
+ }
207
+ }
208
+
209
+ private _codeAutoRetrievalTimeoutHandler(credential: NativePhoneAuthCredentialInternal): void {
210
+ const snapshot: PhoneAuthSnapshot = {
211
+ verificationId: credential.verificationId,
212
+ code: null,
213
+ error: null,
214
+ state: 'timeout',
215
+ };
216
+
217
+ this._emitToObservers(snapshot);
218
+ this._emitToSuccessCb(snapshot);
219
+ }
220
+
221
+ private _verificationCompleteHandler(credential: NativePhoneAuthCredentialInternal): void {
222
+ const snapshot: PhoneAuthSnapshot = {
223
+ verificationId: credential.verificationId,
224
+ code: credential.code || null,
225
+ error: null,
226
+ state: 'verified',
227
+ };
228
+
229
+ this._emitToObservers(snapshot);
230
+ this._emitToSuccessCb(snapshot);
231
+ this._removeAllListeners();
232
+ }
233
+
234
+ private _verificationFailedHandler(state: NativePhoneAuthErrorInternal): void {
235
+ const snapshot: PhoneAuthSnapshot = {
236
+ verificationId: state.verificationId,
237
+ code: null,
238
+ error: null,
239
+ state: 'error',
240
+ };
241
+
242
+ snapshot.error = new NativeFirebaseError(
243
+ { userInfo: state.error },
244
+ this._jsStack,
245
+ 'auth',
246
+ ) as ReactNativeFirebase.NativeFirebaseError;
247
+
248
+ this._emitToObservers(snapshot);
249
+ this._emitToErrorCb(snapshot);
250
+ this._removeAllListeners();
251
+ }
252
+
253
+ /* -------------
254
+ -- PUBLIC API
255
+ --------------*/
256
+
257
+ on(
258
+ event: string,
259
+ observer: (snapshot: PhoneAuthSnapshot) => void,
260
+ errorCb?: (error: PhoneAuthError) => void,
261
+ successCb?: (snapshot: PhoneAuthSnapshot) => void,
262
+ ): PhoneAuthListener {
263
+ if (event !== 'state_changed') {
264
+ throw new Error(
265
+ "firebase.auth.PhoneAuthListener.on(*, _, _, _) 'event' must equal 'state_changed'.",
266
+ );
267
+ }
268
+
269
+ if (!isFunction(observer)) {
270
+ throw new Error(
271
+ "firebase.auth.PhoneAuthListener.on(_, *, _, _) 'observer' must be a function.",
272
+ );
273
+ }
274
+
275
+ this._addUserObserver(observer);
276
+
277
+ if (isFunction(errorCb)) {
278
+ const subscription = this._auth.emitter.addListener(this._publicEvents.error, event => {
279
+ subscription.remove();
280
+ errorCb(event as PhoneAuthError);
281
+ });
282
+ }
283
+
284
+ if (isFunction(successCb)) {
285
+ const subscription = this._auth.emitter.addListener(this._publicEvents.success, event => {
286
+ subscription.remove();
287
+ successCb(event as PhoneAuthSnapshot);
288
+ });
289
+ }
290
+
291
+ return this;
292
+ }
293
+
294
+ then<TResult1 = PhoneAuthSnapshot, TResult2 = never>(
295
+ onFulfilled?: ((value: PhoneAuthSnapshot) => TResult1 | PromiseLike<TResult1>) | null,
296
+ onRejected?:
297
+ | ((reason: ReactNativeFirebase.NativeFirebaseError) => TResult2 | PromiseLike<TResult2>)
298
+ | null,
299
+ ): Promise<TResult1 | TResult2> {
300
+ this._promiseDeferred();
301
+ return this._promise!.then(onFulfilled ?? undefined, onRejected ?? undefined);
302
+ }
303
+
304
+ catch<TResult = never>(
305
+ onRejected?:
306
+ | ((reason: ReactNativeFirebase.NativeFirebaseError) => TResult | PromiseLike<TResult>)
307
+ | null,
308
+ ): Promise<PhoneAuthSnapshot | TResult> {
309
+ this._promiseDeferred();
310
+ return this._promise!.catch(onRejected ?? undefined);
311
+ }
312
+ }
@@ -0,0 +1,22 @@
1
+ /*
2
+ * Copyright (c) 2016-present Invertase Limited & Contributors
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this library except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ export const PhoneAuthState = {
18
+ CODE_SENT: 'sent',
19
+ AUTO_VERIFY_TIMEOUT: 'timeout',
20
+ AUTO_VERIFIED: 'verified',
21
+ ERROR: 'error',
22
+ } as const;
@@ -15,6 +15,8 @@
15
15
  *
16
16
  */
17
17
 
18
+ import type { PhoneAuthCredential, PhoneMultiFactorAssertion } from './types/auth';
19
+
18
20
  export default class PhoneMultiFactorGenerator {
19
21
  static FACTOR_ID = 'phone';
20
22
 
@@ -24,10 +26,14 @@ export default class PhoneMultiFactorGenerator {
24
26
  );
25
27
  }
26
28
 
27
- static assertion(credential) {
29
+ static assertion(credential: PhoneAuthCredential): PhoneMultiFactorAssertion {
28
30
  // There is no logic here, we mainly do this for API compatibility
29
31
  // (following the Web API).
30
32
  const { token, secret } = credential;
31
- return { token, secret };
33
+ return {
34
+ token,
35
+ secret,
36
+ factorId: 'phone',
37
+ } as PhoneMultiFactorAssertion;
32
38
  }
33
39
  }
@@ -0,0 +1,59 @@
1
+ /*
2
+ * Copyright (c) 2016-present Invertase Limited & Contributors
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this library except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ *
16
+ */
17
+
18
+ import { isAndroid } from '@react-native-firebase/app/dist/module/common';
19
+ import type { AuthInternal } from './types/internal';
20
+
21
+ export default class Settings {
22
+ private readonly _auth: AuthInternal;
23
+ private _forceRecaptchaFlowForTesting: boolean;
24
+ private _appVerificationDisabledForTesting: boolean;
25
+
26
+ constructor(auth: AuthInternal) {
27
+ this._auth = auth;
28
+ this._forceRecaptchaFlowForTesting = false;
29
+ this._appVerificationDisabledForTesting = false;
30
+ }
31
+
32
+ get forceRecaptchaFlowForTesting(): boolean {
33
+ return this._forceRecaptchaFlowForTesting;
34
+ }
35
+
36
+ set forceRecaptchaFlowForTesting(forceRecaptchaFlow: boolean) {
37
+ if (isAndroid) {
38
+ this._forceRecaptchaFlowForTesting = forceRecaptchaFlow;
39
+ this._auth.native.forceRecaptchaFlowForTesting(forceRecaptchaFlow);
40
+ }
41
+ }
42
+
43
+ get appVerificationDisabledForTesting(): boolean {
44
+ return this._appVerificationDisabledForTesting;
45
+ }
46
+
47
+ set appVerificationDisabledForTesting(disabled: boolean) {
48
+ this._appVerificationDisabledForTesting = disabled;
49
+ this._auth.native.setAppVerificationDisabledForTesting(disabled);
50
+ }
51
+
52
+ setAutoRetrievedSmsCodeForPhoneNumber(phoneNumber: string, smsCode: string): Promise<null> {
53
+ if (isAndroid) {
54
+ return this._auth.native.setAutoRetrievedSmsCodeForPhoneNumber(phoneNumber, smsCode);
55
+ }
56
+
57
+ return Promise.resolve(null);
58
+ }
59
+ }
@@ -0,0 +1,78 @@
1
+ /*
2
+ * Copyright (c) 2016-present Invertase Limited & Contributors
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this library except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ *
16
+ */
17
+
18
+ import { isOther } from '@react-native-firebase/app/dist/module/common';
19
+ import { TotpSecret } from './TotpSecret';
20
+ import { getAuth } from './modular';
21
+ import type { Auth, MultiFactorSession, TotpMultiFactorAssertion } from './types/auth';
22
+ import type { AuthInternal } from './types/internal';
23
+
24
+ export default class TotpMultiFactorGenerator {
25
+ static readonly FACTOR_ID = 'totp' as const;
26
+
27
+ constructor() {
28
+ throw new Error(
29
+ '`new TotpMultiFactorGenerator()` is not supported on the native Firebase SDKs.',
30
+ );
31
+ }
32
+
33
+ static assertionForSignIn(uid: string, verificationCode: string): TotpMultiFactorAssertion {
34
+ if (isOther) {
35
+ // we require the web native assertion when using firebase-js-sdk
36
+ // as it has functions used by the SDK, a shim won't do
37
+ return (getAuth() as unknown as AuthInternal).native.assertionForSignIn(
38
+ uid,
39
+ verificationCode,
40
+ ) as unknown as TotpMultiFactorAssertion;
41
+ }
42
+ return {
43
+ uid,
44
+ verificationCode,
45
+ factorId: 'totp',
46
+ } as unknown as TotpMultiFactorAssertion;
47
+ }
48
+
49
+ static assertionForEnrollment(
50
+ totpSecret: TotpSecret,
51
+ verificationCode: string,
52
+ ): TotpMultiFactorAssertion {
53
+ return {
54
+ totpSecret: totpSecret.secretKey,
55
+ verificationCode,
56
+ factorId: 'totp',
57
+ } as unknown as TotpMultiFactorAssertion;
58
+ }
59
+
60
+ static generateSecret(session: MultiFactorSession): Promise<TotpSecret>;
61
+ static generateSecret(session: MultiFactorSession, auth: Auth): Promise<TotpSecret>;
62
+ static async generateSecret(
63
+ session: MultiFactorSession,
64
+ // Defaulting to getAuth() preserves the firebase-js-sdk one-argument API for the default app.
65
+ auth: Auth = getAuth(),
66
+ ): Promise<TotpSecret> {
67
+ if (!session) {
68
+ throw new Error('Session is required to generate a TOTP secret.');
69
+ }
70
+ const {
71
+ secretKey,
72
+ // Other properties are not publicly exposed in native APIs
73
+ // hashingAlgorithm, codeLength, codeIntervalSeconds, enrollmentCompletionDeadline
74
+ } = await (auth as unknown as AuthInternal).native.generateTotpSecret(session);
75
+
76
+ return new TotpSecret(secretKey, auth as unknown as AuthInternal);
77
+ }
78
+ }
@@ -0,0 +1,71 @@
1
+ /*
2
+ * Copyright (c) 2016-present Invertase Limited & Contributors
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this library except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ import { isString } from '@react-native-firebase/app/dist/module/common';
18
+ import type { AuthInternal } from './types/internal';
19
+
20
+ export class TotpSecret {
21
+ readonly secretKey: string;
22
+ private readonly auth: AuthInternal;
23
+
24
+ constructor(secretKey: string, auth: AuthInternal) {
25
+ // The native TotpSecret has many more properties, but they are
26
+ // internal to the native SDKs, we only maintain the secret in JS layer
27
+ this.secretKey = secretKey;
28
+
29
+ // we do need a handle to the correct auth instance to generate QR codes etc
30
+ this.auth = auth;
31
+ }
32
+
33
+ /**
34
+ * Shared secret key/seed used for enrolling in TOTP MFA and generating OTPs.
35
+ */
36
+ /**
37
+ * Returns a QR code URL as described in
38
+ * https://github.com/google/google-authenticator/wiki/Key-Uri-Format
39
+ * This can be displayed to the user as a QR code to be scanned into a TOTP app like Google Authenticator.
40
+ * If the optional parameters are unspecified, an accountName of <userEmail> and issuer of <firebaseAppName> are used.
41
+ *
42
+ * @param accountName the name of the account/app along with a user identifier.
43
+ * @param issuer issuer of the TOTP (likely the app name).
44
+ * @returns A Promise that resolves to a QR code URL string. Unlike the firebase-js-sdk synchronous
45
+ * return type, this method is async because QR code generation is performed through the React Native
46
+ * native auth bridge.
47
+ */
48
+ async generateQrCodeUrl(accountName?: string, issuer?: string): Promise<string> {
49
+ // accountName and issure are nullable in the API specification but are
50
+ // required by tha native SDK. The JS SDK returns '' if they are missing/empty.
51
+ if (!isString(accountName) || !isString(issuer) || accountName === '' || issuer === '') {
52
+ return '';
53
+ }
54
+ return this.auth.native.generateQrCodeUrl(this.secretKey, accountName, issuer);
55
+ }
56
+
57
+ /**
58
+ * Opens the specified QR Code URL in an OTP authenticator app on the device.
59
+ * The shared secret key and account name will be populated in the OTP authenticator app.
60
+ * The URL uses the otpauth:// scheme and will be opened on an app that handles this scheme,
61
+ * if it exists on the device, possibly opening the ecocystem-specific app store with a generic
62
+ * query for compatible apps if no app exists on the device.
63
+ *
64
+ * @param qrCodeUrl the URL to open in the app, from generateQrCodeUrl
65
+ */
66
+ openInOtpApp(qrCodeUrl: string): string | void {
67
+ if (isString(qrCodeUrl) && qrCodeUrl !== '') {
68
+ return this.auth.native.openInOtpApp(this.secretKey, qrCodeUrl);
69
+ }
70
+ }
71
+ }