|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2020 Google LLC |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +import { FirebaseApp } from '@firebase/app-types'; |
| 19 | +import * as impl from '@firebase/auth-exp'; |
| 20 | +import { |
| 21 | + AuthImplCompat, |
| 22 | + DEFAULT_API_HOST, |
| 23 | + DEFAULT_API_SCHEME, |
| 24 | + DEFAULT_TOKEN_API_HOST |
| 25 | +} from '@firebase/auth-exp/src/core/auth/auth_impl'; |
| 26 | +import { AuthErrorCode } from '@firebase/auth-exp/src/core/errors'; |
| 27 | +import { Persistence as persistenceImpl } from '@firebase/auth-exp/src/core/persistence'; |
| 28 | +import { assert, fail } from '@firebase/auth-exp/src/core/util/assert'; |
| 29 | +import { _getInstance } from '@firebase/auth-exp/src/core/util/instantiator'; |
| 30 | +import { |
| 31 | + ClientPlatform, |
| 32 | + _getClientVersion |
| 33 | +} from '@firebase/auth-exp/src/core/util/version'; |
| 34 | +import * as compat from '@firebase/auth-types'; |
| 35 | +import * as externs from '@firebase/auth-types-exp'; |
| 36 | +import '@firebase/installations'; |
| 37 | +import { Observer, Unsubscribe, ErrorFn } from '@firebase/util'; |
| 38 | +import { User } from './user'; |
| 39 | +import { |
| 40 | + convertComfirmationResult, |
| 41 | + convertCredential |
| 42 | +} from './user_credential'; |
| 43 | + |
| 44 | +export class Auth extends AuthImplCompat implements compat.FirebaseAuth { |
| 45 | + readonly app: FirebaseApp; |
| 46 | + currentUser: User | null; |
| 47 | + |
| 48 | + constructor(app: FirebaseApp) { |
| 49 | + const { apiKey, authDomain } = app.options; |
| 50 | + |
| 51 | + const persistence = [impl.indexedDBLocalPersistence]; |
| 52 | + const hierarchy = (Array.isArray(persistence) |
| 53 | + ? persistence |
| 54 | + : [persistence] |
| 55 | + ).map<persistenceImpl>(_getInstance); |
| 56 | + |
| 57 | + // TODO: platform needs to be determined using heuristics |
| 58 | + assert(apiKey, app.name, AuthErrorCode.INVALID_API_KEY); |
| 59 | + const config: externs.Config = { |
| 60 | + apiKey, |
| 61 | + authDomain, |
| 62 | + apiHost: DEFAULT_API_HOST, |
| 63 | + tokenApiHost: DEFAULT_TOKEN_API_HOST, |
| 64 | + apiScheme: DEFAULT_API_SCHEME, |
| 65 | + sdkClientVersion: _getClientVersion(ClientPlatform.BROWSER) |
| 66 | + }; |
| 67 | + |
| 68 | + super(app.name, config); |
| 69 | + this.app = app; |
| 70 | + this.currentUser = null; |
| 71 | + |
| 72 | + // This promise is intended to float; auth initialization happens in the |
| 73 | + // background, meanwhile the auth object may be used by the app. |
| 74 | + // eslint-disable-next-line @typescript-eslint/no-floating-promises |
| 75 | + this._initializeWithPersistence( |
| 76 | + hierarchy, |
| 77 | + impl.browserPopupRedirectResolver |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + applyActionCode(code: string): Promise<void> { |
| 82 | + return impl.applyActionCode(this, code); |
| 83 | + } |
| 84 | + |
| 85 | + checkActionCode(code: string): Promise<compat.ActionCodeInfo> { |
| 86 | + return impl.checkActionCode(this, code); |
| 87 | + } |
| 88 | + |
| 89 | + confirmPasswordReset(code: string, newPassword: string): Promise<void> { |
| 90 | + return impl.confirmPasswordReset(this, code, newPassword); |
| 91 | + } |
| 92 | + |
| 93 | + async createUserWithEmailAndPassword( |
| 94 | + email: string, |
| 95 | + password: string |
| 96 | + ): Promise<compat.UserCredential> { |
| 97 | + return convertCredential( |
| 98 | + impl.createUserWithEmailAndPassword(this, email, password) |
| 99 | + ); |
| 100 | + } |
| 101 | + // currentUser: User | null; |
| 102 | + fetchSignInMethodsForEmail(email: string): Promise<string[]> { |
| 103 | + return impl.fetchSignInMethodsForEmail(this, email); |
| 104 | + } |
| 105 | + isSignInWithEmailLink(emailLink: string): boolean { |
| 106 | + return impl.isSignInWithEmailLink(this, emailLink); |
| 107 | + } |
| 108 | + async getRedirectResult(): Promise<compat.UserCredential> { |
| 109 | + const credential = await impl.getRedirectResult( |
| 110 | + this, |
| 111 | + impl.browserPopupRedirectResolver |
| 112 | + ); |
| 113 | + if (!credential) { |
| 114 | + return { |
| 115 | + credential: null, |
| 116 | + user: null |
| 117 | + }; |
| 118 | + } |
| 119 | + return convertCredential(Promise.resolve(credential!)); |
| 120 | + } |
| 121 | + // languageCode: string | null; |
| 122 | + // settings: AuthSettings; |
| 123 | + onAuthStateChanged( |
| 124 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 125 | + nextOrObserver: Observer<any> | ((a: compat.User | null) => any), |
| 126 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 127 | + error?: (error: compat.Error) => any, |
| 128 | + completed?: Unsubscribe |
| 129 | + ): Unsubscribe { |
| 130 | + return super._onAuthStateChanged( |
| 131 | + nextOrObserver as externs.NextOrObserver<externs.User | null>, |
| 132 | + error as ErrorFn, |
| 133 | + completed |
| 134 | + ); |
| 135 | + } |
| 136 | + onIdTokenChanged( |
| 137 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 138 | + nextOrObserver: Observer<any> | ((a: compat.User | null) => any), |
| 139 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 140 | + error?: (error: compat.Error) => any, |
| 141 | + completed?: Unsubscribe |
| 142 | + ): Unsubscribe { |
| 143 | + return super._onIdTokenChanged( |
| 144 | + nextOrObserver as externs.NextOrObserver<externs.User | null>, |
| 145 | + error as ErrorFn, |
| 146 | + completed |
| 147 | + ); |
| 148 | + } |
| 149 | + sendSignInLinkToEmail( |
| 150 | + email: string, |
| 151 | + actionCodeSettings: compat.ActionCodeSettings |
| 152 | + ): Promise<void> { |
| 153 | + return impl.sendSignInLinkToEmail(this, email, actionCodeSettings); |
| 154 | + } |
| 155 | + sendPasswordResetEmail( |
| 156 | + email: string, |
| 157 | + actionCodeSettings?: compat.ActionCodeSettings | null |
| 158 | + ): Promise<void> { |
| 159 | + return impl.sendPasswordResetEmail( |
| 160 | + this, |
| 161 | + email, |
| 162 | + actionCodeSettings || undefined |
| 163 | + ); |
| 164 | + } |
| 165 | + async setPersistence(persistence: string): Promise<void> { |
| 166 | + function convertPersistence( |
| 167 | + auth: externs.Auth, |
| 168 | + persistenceCompat: string |
| 169 | + ): externs.Persistence { |
| 170 | + switch (persistenceCompat) { |
| 171 | + case compat.FirebaseAuth.Persistence.LOCAL: |
| 172 | + return impl.browserLocalPersistence; |
| 173 | + case compat.FirebaseAuth.Persistence.SESSION: |
| 174 | + return impl.browserSessionPersistence; |
| 175 | + case compat.FirebaseAuth.Persistence.NONE: |
| 176 | + return impl.inMemoryPersistence; |
| 177 | + default: |
| 178 | + fail(auth.name, AuthErrorCode.ARGUMENT_ERROR); |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + return super._setPersistence( |
| 183 | + _getInstance(convertPersistence(this, persistence)) |
| 184 | + ); |
| 185 | + } |
| 186 | + |
| 187 | + signInAndRetrieveDataWithCredential( |
| 188 | + credential: compat.AuthCredential |
| 189 | + ): Promise<compat.UserCredential> { |
| 190 | + return this.signInWithCredential(credential); |
| 191 | + } |
| 192 | + signInAnonymously(): Promise<compat.UserCredential> { |
| 193 | + return convertCredential(impl.signInAnonymously(this)); |
| 194 | + } |
| 195 | + signInWithCredential( |
| 196 | + credential: compat.AuthCredential |
| 197 | + ): Promise<compat.UserCredential> { |
| 198 | + return convertCredential( |
| 199 | + impl.signInWithCredential(this, credential as externs.AuthCredential) |
| 200 | + ); |
| 201 | + } |
| 202 | + signInWithCustomToken(token: string): Promise<compat.UserCredential> { |
| 203 | + return convertCredential(impl.signInWithCustomToken(this, token)); |
| 204 | + } |
| 205 | + signInWithEmailAndPassword( |
| 206 | + email: string, |
| 207 | + password: string |
| 208 | + ): Promise<compat.UserCredential> { |
| 209 | + return convertCredential( |
| 210 | + impl.signInWithEmailAndPassword(this, email, password) |
| 211 | + ); |
| 212 | + } |
| 213 | + signInWithEmailLink( |
| 214 | + email: string, |
| 215 | + emailLink?: string |
| 216 | + ): Promise<compat.UserCredential> { |
| 217 | + return convertCredential(impl.signInWithEmailLink(this, email, emailLink)); |
| 218 | + } |
| 219 | + signInWithPhoneNumber( |
| 220 | + phoneNumber: string, |
| 221 | + applicationVerifier: compat.ApplicationVerifier |
| 222 | + ): Promise<compat.ConfirmationResult> { |
| 223 | + return convertComfirmationResult( |
| 224 | + impl.signInWithPhoneNumber(this, phoneNumber, applicationVerifier) |
| 225 | + ); |
| 226 | + } |
| 227 | + signInWithPopup( |
| 228 | + provider: compat.AuthProvider |
| 229 | + ): Promise<compat.UserCredential> { |
| 230 | + return convertCredential( |
| 231 | + impl.signInWithPopup( |
| 232 | + this, |
| 233 | + provider as externs.AuthProvider, |
| 234 | + impl.browserLocalPersistence |
| 235 | + ) |
| 236 | + ); |
| 237 | + } |
| 238 | + signInWithRedirect(provider: compat.AuthProvider): Promise<void> { |
| 239 | + return impl.signInWithRedirect( |
| 240 | + this, |
| 241 | + provider as externs.AuthProvider, |
| 242 | + impl.browserPopupRedirectResolver |
| 243 | + ); |
| 244 | + } |
| 245 | + updateCurrentUser(user: User | null): Promise<void> { |
| 246 | + return super.updateCurrentUser(user); |
| 247 | + } |
| 248 | + verifyPasswordResetCode(code: string): Promise<string> { |
| 249 | + return impl.verifyPasswordResetCode(this, code); |
| 250 | + } |
| 251 | +} |
0 commit comments