UNPKG

@nativescript/firebase-auth

Version:
1,198 lines 39.1 kB
import { FirebaseApp, firebase, deserialize, FirebaseError } from '@nativescript/firebase-core'; import { ActionCodeInfoOperation } from './common'; export { ActionCodeInfoOperation }; let defaultAuth; const fb = firebase(); if (!fb.hasOwnProperty('auth')) { Object.defineProperty(fb, 'auth', { value: (app) => { if (!app) { if (!defaultAuth) { defaultAuth = new Auth(); } return defaultAuth; } return new Auth(app); }, writable: false, }); } export class UserMetadata { static fromNative(metadata) { if (metadata instanceof FIRUserMetadata) { const meta = new UserMetadata(); meta._native = metadata; return meta; } return null; } get native() { return this._native; } get ios() { return this.native; } get creationDate() { return this.native?.creationDate; } get lastSignInDate() { return this.native.lastSignInDate; } toJSON() { return { creationDate: this.creationDate, lastSignInDate: this.lastSignInDate, }; } } export class UserInfo { static fromNative(info) { if (info) { const nativeInfo = new UserInfo(); nativeInfo._native = info; return nativeInfo; } return null; } get native() { return this._native; } get ios() { return this.native; } get uid() { return this.native?.valueForKey('uid'); } get displayName() { return this.native?.valueForKey('displayName'); } get email() { return this.native?.valueForKey('email'); } get phoneNumber() { return this.native?.valueForKey('phoneNumber'); } get photoURL() { return this.native?.valueForKey('photoURL')?.absoluteString; } get providerId() { return this.native?.valueForKey('providerID'); } toJSON() { return { displayName: this.displayName, email: this.email, uid: this.uid, phoneNumber: this.phoneNumber, providerId: this.providerId, photoURL: this.photoURL, }; } } export class User { get native() { return this._native; } get ios() { return this.native; } static fromNative(user) { if (user instanceof FIRUser) { const usr = new User(); usr._native = user; return usr; } return null; } get displayName() { return this.native?.displayName; } get anonymous() { return this.native?.anonymous(); } get emailVerified() { return this.native?.emailVerified(); } get email() { return this.native?.email; } get uid() { return this.native?.uid; } get phoneNumber() { return this.native?.phoneNumber; } get providerId() { return this.native?.providerID; } get photoURL() { return this.native?.photoURL?.absoluteString; } get metadata() { return UserMetadata.fromNative(this.native?.metadata); } get providerData() { const providerData = this.native.providerData; const count = providerData.count; const data = []; for (let i = 0; i < count; i++) { data.push(UserInfo.fromNative(providerData.objectAtIndex(i))); } return data; } toJSON() { return { displayName: this.displayName, anonymous: this.anonymous, emailVerified: this.emailVerified, email: this.email, uid: this.uid, phoneNumber: this.phoneNumber, providerId: this.providerId, photoURL: this.photoURL, metadata: this.metadata, providerData: this.providerData, }; } delete() { return new Promise((resolve, reject) => { if (!this.native) { reject(); } else { this.native?.deleteWithCompletion((error) => { if (error) { reject(FirebaseError.fromNative(error)); } else { resolve(); } }); } }); } getIdToken(forceRefresh) { return new Promise((resolve, reject) => { if (!this.native) { reject(); } else { if (typeof forceRefresh === 'boolean') { this.native.getIDTokenForcingRefreshCompletion(forceRefresh, (idToken, error) => { if (error) { reject(FirebaseError.fromNative(error)); } else { resolve(idToken); } }); } else { this.native.getIDTokenWithCompletion((idToken, error) => { if (error) { reject(FirebaseError.fromNative(error)); } else { resolve(idToken); } }); } } }); } getIdTokenResult(forceRefresh) { return new Promise((resolve, reject) => { if (!this.native) { reject(); } else { if (typeof forceRefresh === 'boolean') { this.native.getIDTokenResultForcingRefreshCompletion(forceRefresh, (idToken, error) => { if (error) { reject(FirebaseError.fromNative(error)); } else { resolve(AuthTokenResult.fromNative(idToken)); } }); } else { this.native.getIDTokenResultWithCompletion((idToken, error) => { if (error) { reject(FirebaseError.fromNative(error)); } else { resolve(AuthTokenResult.fromNative(idToken)); } }); } } }); } linkWithCredential(credential) { return new Promise((resolve, reject) => { if (!this.native) { reject(); } else { this.native.linkWithCredentialCompletion(credential.native, (result, error) => { if (error) { reject(FirebaseError.fromNative(error)); } else { resolve(toUserCredential(result)); } }); } }); } reauthenticateWithProvider(provider) { return new Promise((resolve, reject) => { if (!this.native) { reject(); } else { provider._builder.getCredentialWithUIDelegateCompletion(null, (credential, error) => { if (error) { reject(FirebaseError.fromNative(error)); } else { this.native.reauthenticateWithCredentialCompletion(credential, (result, error) => { if (error) { reject(FirebaseError.fromNative(error)); } else { resolve(toUserCredential(result)); } }); } }); } }); } reauthenticateWithCredential(credential) { return new Promise((resolve, reject) => { if (!this.native) { reject(); } else { this.native.reauthenticateWithCredentialCompletion(credential.native, (result, error) => { if (error) { reject(FirebaseError.fromNative(error)); } else { resolve(toUserCredential(result)); } }); } }); } reload() { return new Promise((resolve, reject) => { if (!this.native) { reject(); } else { this.native.reloadWithCompletion((error) => { if (error) { reject(FirebaseError.fromNative(error)); } else { resolve(); } }); } }); } sendEmailVerification(actionCodeSettings) { return new Promise((resolve, reject) => { if (!this.native) { reject(); } else { if (actionCodeSettings) { this.native.sendEmailVerificationWithActionCodeSettingsCompletion(actionCodeSettings.native, (error) => { if (error) { reject(FirebaseError.fromNative(error)); } else { resolve(); } }); } else { this.native.sendEmailVerificationWithCompletion((error) => { if (error) { reject(FirebaseError.fromNative(error)); } else { resolve(); } }); } } }); } unlink(providerId) { return new Promise((resolve, reject) => { if (!this.native) { reject(); } else { this.native.unlinkFromProviderCompletion(providerId, (user, error) => { if (error) { reject(FirebaseError.fromNative(error)); } else { resolve(User.fromNative(user)); } }); } }); } updateEmail(email) { return new Promise((resolve, reject) => { if (!this.native) { reject(); } else { this.native.updateEmailCompletion(email, (error) => { if (error) { reject(FirebaseError.fromNative(error)); } else { resolve(); } }); } }); } updatePassword(password) { return new Promise((resolve, reject) => { if (!this.native) { reject(); } else { this.native.updatePasswordCompletion(password, (error) => { if (error) { reject(FirebaseError.fromNative(error)); } else { resolve(); } }); } }); } updatePhoneNumber(credential) { return new Promise((resolve, reject) => { if (!this.native) { reject(); } else { this.native.updatePhoneNumberCredentialCompletion(credential.native, (error) => { if (error) { reject(FirebaseError.fromNative(error)); } else { resolve(); } }); } }); } updateProfile(profile) { return new Promise((resolve, reject) => { if (!this.native) { reject(); } else { const request = this.native.profileChangeRequest(); if (profile.displayName) { request.displayName = profile.displayName; } if (profile.photoUri) { try { request.photoURL = NSURL.URLWithString(profile.photoUri); } catch (e) { } } request.commitChangesWithCompletion((error) => { if (error) { reject(FirebaseError.fromNative(error)); } else { resolve(); } }); } }); } verifyBeforeUpdateEmail(email, actionCodeSettings) { return new Promise((resolve, reject) => { if (!this.native) { reject(); } else { if (actionCodeSettings) { this.native.sendEmailVerificationBeforeUpdatingEmailActionCodeSettingsCompletion(email, actionCodeSettings.native, (error) => { if (error) { reject(FirebaseError.fromNative(error)); } else { resolve(); } }); } else { this.native.sendEmailVerificationBeforeUpdatingEmailCompletion(email, (error) => { if (error) { reject(FirebaseError.fromNative(error)); } else { resolve(); } }); } } }); } } export class AuthSettings { static fromNative(settings) { if (settings instanceof FIRAuthSettings) { const nativeSettings = new AuthSettings(); nativeSettings._native = settings; return nativeSettings; } return null; } get native() { return this._native; } get ios() { return this.native; } set appVerificationDisabledForTesting(value) { if (this.native) { this.native.appVerificationDisabledForTesting = value; } } get appVerificationDisabledForTesting() { return this.native?.appVerificationDisabledForTesting; } } function toActionCodeOperation(operation) { switch (operation) { case 0 /* FIRActionCodeOperation.Unknown */: return ActionCodeInfoOperation.Unknown; case 1 /* FIRActionCodeOperation.PasswordReset */: return ActionCodeInfoOperation.PasswordReset; case 2 /* FIRActionCodeOperation.VerifyEmail */: return ActionCodeInfoOperation.VerifyEmail; case 3 /* FIRActionCodeOperation.RecoverEmail */: return ActionCodeInfoOperation.RecoverEmail; case 4 /* FIRActionCodeOperation.EmailLink */: return ActionCodeInfoOperation.EmailLink; case 5 /* FIRActionCodeOperation.VerifyAndChangeEmail */: return ActionCodeInfoOperation.VerifyAndChangeEmail; case 6 /* FIRActionCodeOperation.RevertSecondFactorAddition */: return ActionCodeInfoOperation.RevertSecondFactorAddition; } } function toUserCredential(authData) { const result = { additionalUserInfo: null, user: User.fromNative(authData.user), credential: authData.credential instanceof FIROAuthCredential ? OAuthCredential.fromNative(authData.credential) : AuthCredential.fromNative(authData.credential), }; if (authData?.additionalUserInfo) { result.additionalUserInfo = { newUser: authData?.additionalUserInfo?.newUser, providerId: authData?.additionalUserInfo?.providerID, username: authData?.additionalUserInfo?.username, profile: deserialize(authData?.additionalUserInfo?.profile), }; } return result; } export class ActionCodeSettings { static fromNative(settings) { if (settings instanceof FIRActionCodeSettings) { const nativeSettings = new ActionCodeSettings(); nativeSettings._native = settings; return nativeSettings; } return null; } get androidInstallIfNotAvailable() { return this.native?.androidInstallIfNotAvailable; } set androidInstallIfNotAvailable(value) { if (this.native) { this.native.setAndroidPackageNameInstallIfNotAvailableMinimumVersion(this.androidPackageName, value, this.androidMinimumVersion); } } get androidMinimumVersion() { return this.native?.androidMinimumVersion; } set androidMinimumVersion(value) { if (this.native) { this.native.setAndroidPackageNameInstallIfNotAvailableMinimumVersion(this.androidPackageName, this.androidInstallIfNotAvailable, value); } } get androidPackageName() { return this.native?.androidPackageName; } set androidPackageName(value) { if (this.native) { this.native.setAndroidPackageNameInstallIfNotAvailableMinimumVersion(value, this.androidInstallIfNotAvailable, this.androidMinimumVersion); } } get dynamicLinkDomain() { return this.native.dynamicLinkDomain; } set dynamicLinkDomain(value) { if (this.native) { this.native.dynamicLinkDomain = value; } } get handleCodeInApp() { return this.native?.handleCodeInApp; } set handleCodeInApp(handle) { if (this.native) { this.native.handleCodeInApp = handle; } } get native() { return this._native; } get ios() { return this.native; } get url() { return this.native.URL?.absoluteString; } get iOSBundleId() { return this.native?.iOSBundleID; } set iOSBundleId(id) { if (this.native) { this.native.iOSBundleID = id; } } } export class AuthCredential { static fromNative(credential) { if (credential instanceof FIRAuthCredential) { const nativeCredential = new AuthCredential(); nativeCredential._native = credential; return nativeCredential; } return null; } get native() { return this._native; } get ios() { return this.native; } get provider() { return this.native?.provider; } } export class EmailAuthProvider { static credential(email, password) { return AuthCredential.fromNative(FIREmailAuthProvider.credentialWithEmailPassword(email, password)); } static credentialWithLink(email, link) { return AuthCredential.fromNative(FIREmailAuthProvider.credentialWithEmailLink(email, link)); } } export class PhoneAuthProvider { get native() { return this._native; } get ios() { return this.native; } static provider(auth) { const provider = new PhoneAuthProvider(); if (auth) { provider._native = FIRPhoneAuthProvider.providerWithAuth(auth.native); } else { provider._native = FIRPhoneAuthProvider.provider(); } return provider; } credential(verificationId, verificationCode) { return AuthCredential.fromNative(this._native.credentialWithVerificationIDVerificationCode(verificationId, verificationCode)); } verifyPhoneNumber(phoneNumber) { return new Promise((resolve, reject) => { if (!this._native) { reject(); } else { this.native.verifyPhoneNumberUIDelegateCompletion(phoneNumber, null, (verificationId, error) => { if (error) { reject(FirebaseError.fromNative(error)); } else { resolve(verificationId); } }); } }); } } export class AppleAuthProvider { static credential(idToken, nonce) { return AuthCredential.fromNative(FIROAuthProvider.credentialWithProviderIDIDTokenRawNonce('apple.com', idToken, nonce)); } } export class FacebookAuthProvider { static credential(accessToken) { return AuthCredential.fromNative(FIRFacebookAuthProvider.credentialWithAccessToken(accessToken)); } } export class GithubAuthProvider { static credential(token) { return AuthCredential.fromNative(FIRGitHubAuthProvider.credentialWithToken(token)); } } export class GoogleAuthProvider { static credential(idToken, accessToken) { return AuthCredential.fromNative(FIRGoogleAuthProvider.credentialWithIDTokenAccessToken(idToken, accessToken)); } } export class OAuthCredential extends AuthCredential { static fromNative(credential) { if (credential instanceof FIRAuthCredential) { const nativeCredential = new OAuthCredential(); nativeCredential._native = credential; return nativeCredential; } return null; } get native() { return this._native; } get ios() { return this.native; } get idToken() { return this.native?.IDToken; } get accessToken() { return this.native?.accessToken; } get secret() { return this.native?.secret; } } export class OAuthProvider { constructor(providerId) { this._providerId = providerId; this._customParameters = {}; this._scopes = []; } get _builder() { const builder = FIROAuthProvider.providerWithProviderID(this._providerId); builder.customParameters = this._customParameters; builder.scopes = this._scopes; return builder; } addCustomParameter(key, value) { this._customParameters[key] = value; } setScopes(scopes) { if (Array.isArray(scopes)) { this._scopes = scopes; } } credential(optionsOrIdToken, accessToken) { let provider; if (!optionsOrIdToken && accessToken) { provider = FIROAuthProvider.credentialWithProviderIDAccessToken(this._providerId, accessToken); } else if (optionsOrIdToken) { if (typeof optionsOrIdToken === 'string') { provider = FIROAuthProvider.credentialWithProviderIDAccessToken(this._providerId, optionsOrIdToken); } else if (typeof optionsOrIdToken === 'object') { if (optionsOrIdToken.idToken && !optionsOrIdToken.rawNonce) { provider = FIROAuthProvider.credentialWithProviderIDIDTokenAccessToken(this._providerId, optionsOrIdToken.idToken, optionsOrIdToken.accessToken); } else if (optionsOrIdToken.idToken && optionsOrIdToken.rawNonce) { provider = FIROAuthProvider.credentialWithProviderIDIDTokenRawNonce(this._providerId, optionsOrIdToken.idToken, optionsOrIdToken.rawNonce); } else { provider = FIROAuthProvider.credentialWithProviderIDIDTokenRawNonceAccessToken(this._providerId, optionsOrIdToken.idToken, optionsOrIdToken.rawNonce, optionsOrIdToken.accessToken); } } } return OAuthCredential.fromNative(provider); } } export class TwitterAuthProvider { static credential(token, secret) { return AuthCredential.fromNative(FIRTwitterAuthProvider.credentialWithTokenSecret(token, secret)); } } export class PhoneAuthCredential extends AuthCredential { static fomNative(credential) { if (credential instanceof FIRPhoneAuthCredential) { const nativeCredential = new PhoneAuthCredential(); nativeCredential._native = credential; return nativeCredential; } return null; } get native() { return this._native; } get ios() { return this.native; } } export class AuthTokenResult { static fromNative(tokenResult) { if (tokenResult instanceof FIRAuthTokenResult) { const result = new AuthTokenResult(); result._native = tokenResult; return result; } return null; } get native() { return this._native; } get ios() { return this.native; } get authDate() { return this.native.authDate; } get claims() { return deserialize(this.native?.claims); } get expirationDate() { return this.native?.expirationDate; } get issuedAtDate() { return this.native?.issuedAtDate; } get signInProvider() { return this.native.signInProvider; } get token() { return this.native?.token; } } export class Auth { constructor(app) { this._authStateChangeListeners = new Map(); this._idTokenChangeListeners = new Map(); this._app = app; } useEmulator(host, port) { this.native.useEmulatorWithHostPort(host, port); } fetchSignInMethodsForEmail(email) { return new Promise((resolve, reject) => { if (!this.native) { reject(); } this.native.fetchSignInMethodsForEmailCompletion(email, (emails, error) => { if (error) { reject(FirebaseError.fromNative(error)); } else { const arr = []; if (emails) { const size = emails.count; for (let i = 0; i < size; i++) { arr.push(emails.objectAtIndex(i)); } } resolve(arr); } }); }); } isSignInWithEmailLink(emailLink) { return this.native.isSignInWithEmailLink(emailLink); } addAuthStateChangeListener(listener) { if (this.native && typeof listener === 'function') { const id = this.native.addAuthStateDidChangeListener((auth, user) => { listener(User.fromNative(user)); }); this._authStateChangeListeners.set(listener, id); } } removeAuthStateChangeListener(listener) { if (this.native && typeof listener === 'function') { const id = this._authStateChangeListeners.get(listener); if (id) { this.native.removeAuthStateDidChangeListener(id); this._authStateChangeListeners.delete(id); } } } addIdTokenChangeListener(listener) { if (this.native && typeof listener === 'function') { const id = this.native.addIDTokenDidChangeListener((auth, user) => { listener(User.fromNative(user)); }); this._idTokenChangeListeners.set(listener, id); } } removeIdTokenChangListener(listener) { if (this.native && typeof listener === 'function') { const id = this._authStateChangeListeners.get(listener); if (id) { this.native.removeIDTokenDidChangeListener(id); this._authStateChangeListeners.delete(id); } } } sendPasswordResetEmail(email, actionCodeSettings) { return new Promise((resolve, reject) => { if (!this.native) { reject(); } if (actionCodeSettings) { this.native.sendPasswordResetWithEmailActionCodeSettingsCompletion(email, actionCodeSettings.native, (error) => { if (error) { reject(FirebaseError.fromNative(error)); } else { resolve(); } }); } else { this.native.sendPasswordResetWithEmailCompletion(email, (error) => { if (error) { reject(FirebaseError.fromNative(error)); } else { resolve(); } }); } }); } sendSignInLinkToEmail(email, actionCodeSettings) { return new Promise((resolve, reject) => { if (!this.native) { reject(); } this.native.sendSignInLinkToEmailActionCodeSettingsCompletion(email, actionCodeSettings.native || FIRActionCodeSettings.new(), (error) => { if (error) { reject(FirebaseError.fromNative(error)); } else { resolve(); } }); }); } signInAnonymously() { return new Promise((resolve, reject) => { if (!this.native) { reject(); } else { this.native.signInAnonymouslyWithCompletion((result, error) => { if (error) { reject(FirebaseError.fromNative(error)); } else { resolve(toUserCredential(result)); } }); } }); } signInWithProvider(provider) { return new Promise((resolve, reject) => { if (!this.native) { reject(); } else { provider._builder.getCredentialWithUIDelegateCompletion(null, (credential, error) => { if (error) { reject(FirebaseError.fromNative(error)); } else { this.native.signInWithCredentialCompletion(credential, (result, error) => { if (error) { reject(FirebaseError.fromNative(error)); } else { resolve(toUserCredential(result)); } }); } }); } }); } getProviderCredential(provider) { return new Promise((resolve, reject) => { if (!this.native) { reject(); } else { provider._builder.getCredentialWithUIDelegateCompletion(null, (credential, error) => { if (error) { reject(FirebaseError.fromNative(error)); } else { resolve(OAuthCredential.fromNative(credential)); } }); } }); } signInWithCredential(credential) { return new Promise((resolve, reject) => { if (!this.native) { reject(); } else { this.native.signInWithCredentialCompletion(credential.native, (result, error) => { if (error) { reject(FirebaseError.fromNative(error)); } else { resolve(toUserCredential(result)); } }); } }); } signInWithCustomToken(customToken) { return new Promise((resolve, reject) => { if (!this.native) { reject(); } else { this.native.signInWithCustomTokenCompletion(customToken, (result, error) => { if (error) { reject(FirebaseError.fromNative(error)); } else { resolve(toUserCredential(result)); } }); } }); } signInWithEmailLink(email, emailLink) { return new Promise((resolve, reject) => { if (!this.native) { reject(); } else { this.native.signInWithEmailLinkCompletion(email, emailLink, (result, error) => { if (error) { reject(FirebaseError.fromNative(error)); } else { resolve(toUserCredential(result)); } }); } }); } useUserAccessGroup(userAccessGroup) { return new Promise((resolve, reject) => { if (!this.native) { reject(); } else { try { this.native?.useUserAccessGroupError?.(userAccessGroup); resolve(); } catch (error) { reject({ message: error.message, native: error, }); } } }); } verifyPasswordResetCode(code) { return new Promise((resolve, reject) => { if (!this.native) { reject(); } else { this.native.verifyPasswordResetCodeCompletion(code, (email, error) => { if (error) { reject(FirebaseError.fromNative(error)); } else { resolve(email); } }); } }); } createUserWithEmailAndPassword(email, password) { return new Promise((resolve, reject) => { if (!this.native) { reject(); } this.native.createUserWithEmailPasswordCompletion(email, password, (result, error) => { if (error) { reject(FirebaseError.fromNative(error)); } else { resolve(toUserCredential(result)); } }); }); } confirmPasswordReset(code, newPassword) { return new Promise((resolve, reject) => { if (!this.native) { reject(); } this.native.confirmPasswordResetWithCodeNewPasswordCompletion(code, newPassword, (error) => { if (error) { reject(FirebaseError.fromNative(error)); } else { resolve(); } }); }); } checkActionCode(code) { return new Promise((resolve, reject) => { if (!this.native) { reject(); } this.native.checkActionCodeCompletion(code, (info, error) => { if (error) { reject(FirebaseError.fromNative(error)); } else { const operation = toActionCodeOperation(info.operation); const actionCodeInfo = { data: { email: undefined, previousEmail: undefined, }, operation, }; if (operation === ActionCodeInfoOperation.VerifyEmail || operation === ActionCodeInfoOperation.PasswordReset) { actionCodeInfo.data.email = info.email; } else if (operation === ActionCodeInfoOperation.RevertSecondFactorAddition) { actionCodeInfo.data.email = null; actionCodeInfo.data.previousEmail = null; } else if (operation === ActionCodeInfoOperation.RecoverEmail || operation === ActionCodeInfoOperation.VerifyAndChangeEmail) { actionCodeInfo.data.email = info.email; actionCodeInfo.data.previousEmail = info.previousEmail; } resolve(actionCodeInfo); } }); }); } applyActionCode(code) { return new Promise((resolve, reject) => { if (!this.native) { reject(); } else { this.native.applyActionCodeCompletion(code, (error) => { if (!error) { resolve(); } else { reject(FirebaseError.fromNative(error)); } }); } }); } signInWithEmailAndPassword(email, password) { return new Promise((resolve, reject) => { if (!this.native) { reject(); } else { this.native.signInWithEmailPasswordCompletion(email, password, (result, error) => { if (error) { reject(FirebaseError.fromNative(error)); } else { resolve(toUserCredential(result)); } }); } }); } signOut() { return Promise.resolve(this.native?.signOut?.()); } get native() { if (!this._native) { if (this._app?.native) { this._native = FIRAuth.authWithApp(this._app.native); } else { this._native = FIRAuth.auth(); } } return this._native; } get ios() { return this.native; } get app() { if (!this._app) { // @ts-ignore this._app = FirebaseApp.fromNative(this.native.app); } return this._app; } get currentUser() { return this.native ? User.fromNative(this.native.currentUser) : null; } get languageCode() { return this.native?.languageCode; } set languageCode(code) { if (this.native) { this.native.languageCode = code; } } get settings() { if (!this._settings) { this._settings = AuthSettings.fromNative(this.native.settings); } return this._settings; } get tenantId() { return this.native.tenantID; } set tenantId(id) { if (this.native) { this.native.tenantID = id; } } } //# sourceMappingURL=index.ios.js.map