@nativescript/firebase-auth
Version:
NativeScript Firebase - Auth
1,191 lines • 43.1 kB
JavaScript
import { deserialize, firebase, FirebaseApp, FirebaseError } from '@nativescript/firebase-core';
import { ActionCodeInfoOperation } from './common';
import lazy from '@nativescript/core/utils/lazy';
import { Application } from '@nativescript/core';
export { ActionCodeInfoOperation };
let defaultAuth;
const fb = firebase();
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 com.google.firebase.auth.FirebaseUserMetadata) {
const meta = new UserMetadata();
meta._native = metadata;
return meta;
}
return null;
}
get native() {
return this._native;
}
get android() {
return this.native;
}
get creationDate() {
if (this.native) {
return new Date(this.native?.getCreationTimestamp());
}
return null;
}
get lastSignInDate() {
if (this.native) {
return new Date(this.native?.getLastSignInTimestamp());
}
return null;
}
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 android() {
return this.native;
}
get uid() {
return this.native?.getUid?.();
}
get displayName() {
return this.native?.getDisplayName?.();
}
get email() {
return this.native?.getEmail?.();
}
get phoneNumber() {
return this.native?.getPhoneNumber?.();
}
get photoURL() {
let url;
try {
url = this.native?.getPhotoUrl()?.toString?.();
}
catch (e) { }
return url;
}
get providerId() {
return this.native?.getProviderId?.();
}
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 android() {
return this.native;
}
static fromNative(user) {
if (user instanceof com.google.firebase.auth.FirebaseUser) {
const usr = new User();
usr._native = user;
return usr;
}
return null;
}
get displayName() {
return this.native?.getDisplayName?.();
}
get anonymous() {
return this.native?.isAnonymous?.();
}
get emailVerified() {
return this.native?.isEmailVerified?.();
}
get email() {
return this.native?.getEmail?.();
}
get uid() {
return this.native?.getUid?.();
}
get phoneNumber() {
return this.native?.getPhoneNumber?.();
}
get providerId() {
return this.native?.getProviderId?.();
}
get photoURL() {
let url;
try {
url = this.native?.getPhotoUrl()?.toString?.();
}
catch (e) { }
return url;
}
get metadata() {
return UserMetadata.fromNative(this.native?.getMetadata?.());
}
get providerData() {
const providerData = this.native.getProviderData();
const count = providerData.size();
const data = [];
for (let i = 0; i < count; i++) {
data.push(UserInfo.fromNative(providerData.get(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 {
NSFirebaseAuth().User.delete(this.native, new org.nativescript.firebase.auth.FirebaseAuth.Callback({
onSuccess(success) {
resolve();
},
onError(error) {
reject(FirebaseError.fromNative(error));
},
}));
}
});
}
getIdToken(forceRefresh = false) {
return new Promise((resolve, reject) => {
if (!this.native) {
reject();
}
else {
NSFirebaseAuth().User.getIdToken(this.native, forceRefresh, new org.nativescript.firebase.auth.FirebaseAuth.Callback({
onSuccess(success) {
resolve(success);
},
onError(error) {
reject(FirebaseError.fromNative(error));
},
}));
}
});
}
getIdTokenResult(forceRefresh = false) {
return new Promise((resolve, reject) => {
if (!this.native) {
reject();
}
else {
NSFirebaseAuth().User.getIdTokenResult(this.native, forceRefresh, new org.nativescript.firebase.auth.FirebaseAuth.Callback({
onSuccess(success) {
resolve(AuthTokenResult.fromNative(success));
},
onError(error) {
reject(FirebaseError.fromNative(error));
},
}));
}
});
}
linkWithCredential(credential) {
return new Promise((resolve, reject) => {
if (!this.native) {
reject();
}
else {
NSFirebaseAuth().User.linkWithCredential(this.native, credential.native, new org.nativescript.firebase.auth.FirebaseAuth.Callback({
onSuccess(success) {
resolve(toUserCredential(success));
},
onError(error) {
reject(FirebaseError.fromNative(error));
},
}));
}
});
}
reauthenticateWithProvider(provider) {
return new Promise((resolve, reject) => {
if (provider._isCustomProvider && provider._builder) {
org.nativescript.firebase.auth.FirebaseAuth.User.reauthenticateWithProvider(Application.android.foregroundActivity || Application.android.startActivity, this.native, provider._builder, new org.nativescript.firebase.auth.FirebaseAuth.Callback({
onSuccess(success) {
resolve(toUserCredential(success));
},
onError(error) {
reject({
message: error.getMessage(),
native: error,
});
},
}));
}
else {
reject(FirebaseError.fromNative(null, 'OAuthProvider not configured'));
}
});
}
reauthenticateWithCredential(credential) {
return new Promise((resolve, reject) => {
if (!this.native) {
reject();
}
else {
NSFirebaseAuth().User.reauthenticateWithCredential(this.native, credential.native, new org.nativescript.firebase.auth.FirebaseAuth.Callback({
onSuccess(success) {
resolve(toUserCredential(success));
},
onError(error) {
reject(FirebaseError.fromNative(error));
},
}));
}
});
}
reload() {
return new Promise((resolve, reject) => {
if (!this.native) {
reject();
}
else {
NSFirebaseAuth().User.reload(this.native, new org.nativescript.firebase.auth.FirebaseAuth.Callback({
onSuccess(success) {
resolve();
},
onError(error) {
reject(FirebaseError.fromNative(error));
},
}));
}
});
}
sendEmailVerification(actionCodeSettings) {
return new Promise((resolve, reject) => {
if (!this.native) {
reject();
}
else {
NSFirebaseAuth().User.sendEmailVerification(this.native, actionCodeSettings?.native ?? null, new org.nativescript.firebase.auth.FirebaseAuth.Callback({
onSuccess(success) {
resolve();
},
onError(error) {
reject(FirebaseError.fromNative(error));
},
}));
}
});
}
unlink(providerId) {
return new Promise((resolve, reject) => {
if (!this.native) {
reject();
}
else {
NSFirebaseAuth().User.unlink(this.native, providerId, new org.nativescript.firebase.auth.FirebaseAuth.Callback({
onSuccess(success) {
resolve(User.fromNative(success.getUser()));
},
onError(error) {
reject(FirebaseError.fromNative(error));
},
}));
}
});
}
updateEmail(email) {
return new Promise((resolve, reject) => {
if (!this.native) {
reject();
}
else {
NSFirebaseAuth().User.updateEmail(this.native, email, new org.nativescript.firebase.auth.FirebaseAuth.Callback({
onSuccess(success) {
resolve();
},
onError(error) {
reject(FirebaseError.fromNative(error));
},
}));
}
});
}
updatePassword(password) {
return new Promise((resolve, reject) => {
if (!this.native) {
reject();
}
else {
NSFirebaseAuth().User.updatePassword(this.native, password, new org.nativescript.firebase.auth.FirebaseAuth.Callback({
onSuccess(success) {
resolve();
},
onError(error) {
reject(FirebaseError.fromNative(error));
},
}));
}
});
}
updatePhoneNumber(credential) {
return new Promise((resolve, reject) => {
if (!this.native) {
reject();
}
else {
NSFirebaseAuth().User.updatePhoneNumber(this.native, credential.native, new org.nativescript.firebase.auth.FirebaseAuth.Callback({
onSuccess(success) {
resolve();
},
onError(error) {
reject(FirebaseError.fromNative(error));
},
}));
}
});
}
updateProfile(profile) {
return new Promise((resolve, reject) => {
if (!this.native) {
reject();
}
else {
const builder = new com.google.firebase.auth.UserProfileChangeRequest.Builder();
if (profile.displayName) {
builder.setDisplayName(profile.displayName);
}
if (profile.photoUri) {
try {
builder.setPhotoUri(android.net.Uri.parse(profile.photoUri));
}
catch (e) { }
}
NSFirebaseAuth().User.updateProfile(this.native, builder.build(), new org.nativescript.firebase.auth.FirebaseAuth.Callback({
onSuccess(success) {
resolve();
},
onError(error) {
reject(FirebaseError.fromNative(error));
},
}));
}
});
}
verifyBeforeUpdateEmail(email, actionCodeSettings) {
return new Promise((resolve, reject) => {
if (!this.native) {
reject();
}
else {
NSFirebaseAuth().User.verifyBeforeUpdateEmail(this.native, email, actionCodeSettings?.native ?? null, new org.nativescript.firebase.auth.FirebaseAuth.Callback({
onSuccess(success) {
resolve();
},
onError(error) {
reject(FirebaseError.fromNative(error));
},
}));
}
});
}
}
export class AuthSettings {
constructor() {
this._appVerificationDisabledForTesting = false;
}
static fromNative(settings) {
if (settings instanceof com.google.firebase.auth.FirebaseAuthSettings) {
const nativeSettings = new AuthSettings();
nativeSettings._native = settings;
return nativeSettings;
}
return null;
}
get native() {
return this._native;
}
get android() {
return this.native;
}
set appVerificationDisabledForTesting(value) {
if (this.native) {
this.native.setAppVerificationDisabledForTesting(value);
this._appVerificationDisabledForTesting = value;
}
}
get appVerificationDisabledForTesting() {
return this._appVerificationDisabledForTesting;
}
}
var ActionCodeResult;
(function (ActionCodeResult) {
// https://firebase.google.com/docs/reference/android/com/google/firebase/auth/ActionCodeResult_public-static-final-int-password_reset
ActionCodeResult[ActionCodeResult["PasswordReset"] = 0] = "PasswordReset";
// https://firebase.google.com/docs/reference/android/com/google/firebase/auth/ActionCodeResult_public-static-final-int-verify_email
ActionCodeResult[ActionCodeResult["VerifyEmail"] = 1] = "VerifyEmail";
// https://firebase.google.com/docs/reference/android/com/google/firebase/auth/ActionCodeResult_public-static-final-int-recover_email
ActionCodeResult[ActionCodeResult["RecoverEmail"] = 2] = "RecoverEmail";
// https://firebase.google.com/docs/reference/android/com/google/firebase/auth/ActionCodeResult_public-static-final-int-error
ActionCodeResult[ActionCodeResult["Error"] = 3] = "Error";
// https://firebase.google.com/docs/reference/android/com/google/firebase/auth/ActionCodeResult_public-static-final-int-sign_in_with_email_link
ActionCodeResult[ActionCodeResult["EmailLink"] = 4] = "EmailLink";
// https://firebase.google.com/docs/reference/android/com/google/firebase/auth/ActionCodeResult_public-static-final-int-verify_before_change_email
ActionCodeResult[ActionCodeResult["VerifyAndChangeEmail"] = 5] = "VerifyAndChangeEmail";
// https://firebase.google.com/docs/reference/android/com/google/firebase/auth/ActionCodeResult_public-static-final-int-revert_second_factor_addition
ActionCodeResult[ActionCodeResult["RevertSecondFactorAddition"] = 6] = "RevertSecondFactorAddition";
})(ActionCodeResult || (ActionCodeResult = {}));
function toActionCodeOperation(operation) {
switch (operation) {
case ActionCodeResult.Error:
return ActionCodeInfoOperation.Unknown;
case ActionCodeResult.PasswordReset:
return ActionCodeInfoOperation.PasswordReset;
case ActionCodeResult.VerifyEmail:
return ActionCodeInfoOperation.VerifyEmail;
case ActionCodeResult.RecoverEmail:
return ActionCodeInfoOperation.RecoverEmail;
case ActionCodeResult.EmailLink:
return ActionCodeInfoOperation.EmailLink;
case ActionCodeResult.VerifyAndChangeEmail:
return ActionCodeInfoOperation.VerifyAndChangeEmail;
case ActionCodeResult.RevertSecondFactorAddition:
return ActionCodeInfoOperation.RevertSecondFactorAddition;
}
}
function toUserCredential(authData) {
const additionalUserInfo = authData.getAdditionalUserInfo();
const credential = authData.getCredential();
const result = {
additionalUserInfo: null,
user: User.fromNative(authData.getUser()),
credential: credential instanceof com.google.firebase.auth.OAuthCredential ? OAuthCredential.fromNative(credential) : AuthCredential.fromNative(credential),
};
if (additionalUserInfo) {
result.additionalUserInfo = {
newUser: additionalUserInfo?.isNewUser?.(),
providerId: additionalUserInfo?.getProviderId?.(),
username: additionalUserInfo?.getUsername?.(),
profile: deserialize(additionalUserInfo?.getProfile?.()),
};
}
return result;
}
export class ActionCodeSettings {
constructor() {
this._native = new com.google.firebase.auth.ActionCodeSettings.Builder();
}
get native() {
return this._native
.setUrl(this.url || '')
.setIOSBundleId(this.iOSBundleId || '')
.setHandleCodeInApp(this.handleCodeInApp || false)
.setDynamicLinkDomain(this.dynamicLinkDomain || '')
.setAndroidPackageName(this.androidPackageName || '', this.androidInstallIfNotAvailable || false, this.androidMinimumVersion || '')
.build();
}
get android() {
return this.native;
}
}
export class AuthCredential {
static fromNative(credential) {
if (credential instanceof com.google.firebase.auth.AuthCredential) {
const nativeCredential = new AuthCredential();
nativeCredential._native = credential;
return nativeCredential;
}
return null;
}
get native() {
return this._native;
}
get android() {
return this.native;
}
get provider() {
return this.native?.getProvider?.();
}
get signInMethod() {
return this.native?.getSignInMethod?.();
}
}
export class EmailAuthProvider {
static credential(email, password) {
return AuthCredential.fromNative(com.google.firebase.auth.EmailAuthProvider.getCredential(email, password));
}
static credentialWithLink(email, link) {
return AuthCredential.fromNative(com.google.firebase.auth.EmailAuthProvider.getCredentialWithLink(email, link));
}
}
let OnVerificationStateChangedCallbacks;
function ensureClass() {
if (OnVerificationStateChangedCallbacks) {
return;
}
var OnVerificationStateChangedCallbacksImpl = /** @class */ (function (_super) {
__extends(OnVerificationStateChangedCallbacksImpl, _super);
function OnVerificationStateChangedCallbacksImpl(resolve, reject) {
var _this = _super.call(this) || this;
_this._resolve = resolve;
_this._reject = reject;
return global.__native(_this);
}
OnVerificationStateChangedCallbacksImpl.prototype.onVerificationFailed = function (error) {
this._reject(FirebaseError.fromNative(error));
};
OnVerificationStateChangedCallbacksImpl.prototype.onVerificationCompleted = function (credential) { };
OnVerificationStateChangedCallbacksImpl.prototype.onCodeSent = function (verificationId, resendingToken) {
this._resolve(verificationId);
};
OnVerificationStateChangedCallbacksImpl.prototype.onCodeAutoRetrievalTimeOut = function (verificationId) {
this._resolve(verificationId);
};
return OnVerificationStateChangedCallbacksImpl;
}(com.google.firebase.auth.PhoneAuthProvider.OnVerificationStateChangedCallbacks));
OnVerificationStateChangedCallbacks = OnVerificationStateChangedCallbacksImpl;
}
export class PhoneAuthProvider {
get native() {
return this._native;
}
get android() {
return this.native;
}
static provider(auth) {
const provider = new PhoneAuthProvider();
const timeout = java.lang.Long.valueOf(60);
if (auth) {
provider._native = com.google.firebase.auth.PhoneAuthOptions.newBuilder(auth.native).setTimeout(timeout, java.util.concurrent.TimeUnit.SECONDS);
}
else {
provider._native = com.google.firebase.auth.PhoneAuthOptions.newBuilder().setTimeout(timeout, java.util.concurrent.TimeUnit.SECONDS);
}
provider._native.setActivity(Application.android.foregroundActivity || Application.android.startActivity);
return provider;
}
credential(verificationId, verificationCode) {
return AuthCredential.fromNative(com.google.firebase.auth.PhoneAuthProvider.getCredential(verificationId, verificationCode));
}
verifyPhoneNumber(phoneNumber) {
return new Promise((resolve, reject) => {
if (!this._native) {
reject();
}
else {
ensureClass();
const cb = new OnVerificationStateChangedCallbacks(resolve, reject);
com.google.firebase.auth.PhoneAuthProvider.verifyPhoneNumber(this.native.setPhoneNumber(phoneNumber).setCallbacks(cb).build());
}
});
}
}
export class AppleAuthProvider {
static credential(idToken, nonce) {
return null;
}
}
export class FacebookAuthProvider {
static credential(accessToken) {
return AuthCredential.fromNative(com.google.firebase.auth.FacebookAuthProvider.getCredential(accessToken));
}
}
export class GithubAuthProvider {
static credential(token) {
return AuthCredential.fromNative(com.google.firebase.auth.GithubAuthProvider.getCredential(token));
}
}
export class GoogleAuthProvider {
static credential(idToken, accessToken) {
return AuthCredential.fromNative(com.google.firebase.auth.GoogleAuthProvider.getCredential(idToken, accessToken));
}
}
export class OAuthCredential extends AuthCredential {
static fromNative(credential) {
if (credential instanceof com.google.firebase.auth.OAuthCredential) {
const nativeCredential = new OAuthCredential();
nativeCredential._native = credential;
return nativeCredential;
}
return null;
}
get native() {
return this._native;
}
get android() {
return this.native;
}
get idToken() {
return this.native?.getIdToken?.();
}
get accessToken() {
return this.native?.getAccessToken?.();
}
get secret() {
return this.native?.getSecret?.();
}
}
export class OAuthProvider {
constructor(providerId) {
this._providerId = providerId;
this._customProvider = false;
}
get _isCustomProvider() {
return this._customProvider;
}
addCustomParameter(key, value) {
if (!this._builder) {
this._builder = com.google.firebase.auth.OAuthProvider.newBuilder(this._providerId);
this._customProvider = true;
}
this._builder.addCustomParameter(key, value);
}
setScopes(scopes) {
if (!this._builder) {
this._builder = com.google.firebase.auth.OAuthProvider.newBuilder(this._providerId);
this._customProvider = true;
}
if (Array.isArray(scopes)) {
const array = new java.util.ArrayList();
scopes.forEach((item) => {
array.add(item);
});
this._builder.setScopes(array);
}
}
credential(optionsOrIdToken, accessToken) {
const builder = com.google.firebase.auth.OAuthProvider.newCredentialBuilder(this._providerId);
if (!optionsOrIdToken && accessToken) {
builder.setAccessToken(accessToken);
}
else if (optionsOrIdToken) {
if (typeof optionsOrIdToken === 'string') {
builder.setAccessToken(accessToken);
}
else if (typeof optionsOrIdToken === 'object') {
if (optionsOrIdToken.idToken && !optionsOrIdToken.rawNonce) {
builder.setIdToken(optionsOrIdToken.idToken).setAccessToken(optionsOrIdToken.accessToken);
}
else if (optionsOrIdToken.idToken && optionsOrIdToken.rawNonce) {
builder.setIdTokenWithRawNonce(optionsOrIdToken.idToken, optionsOrIdToken.rawNonce);
}
else {
builder.setAccessToken(optionsOrIdToken.accessToken).setIdTokenWithRawNonce(optionsOrIdToken.idToken, optionsOrIdToken.rawNonce);
}
}
}
return OAuthCredential.fromNative(builder.build());
}
}
export class TwitterAuthProvider {
static credential(token, secret) {
return AuthCredential.fromNative(com.google.firebase.auth.TwitterAuthProvider.getCredential(token, secret));
}
}
export class PhoneAuthCredential extends AuthCredential {
static fromNative(credential) {
if (credential instanceof com.google.firebase.auth.PhoneAuthCredential) {
const nativeCredential = new PhoneAuthCredential();
nativeCredential._native = credential;
return nativeCredential;
}
return null;
}
get native() {
return this._native;
}
get android() {
return this.native;
}
}
export class AuthTokenResult {
static fromNative(tokenResult) {
if (tokenResult instanceof com.google.firebase.auth.GetTokenResult) {
const result = new AuthTokenResult();
result._native = tokenResult;
return result;
}
return null;
}
get native() {
return this._native;
}
get android() {
return this.native;
}
get authDate() {
const ts = this.native?.getAuthTimestamp?.();
if (ts) {
return new Date(ts);
}
return null;
}
get claims() {
return deserialize(this.native?.getClaims?.());
}
get expirationDate() {
const ts = this.native?.getExpirationTimestamp?.();
if (ts) {
return new Date(ts);
}
return null;
}
get issuedAtDate() {
const ts = this.native?.getIssuedAtTimestamp?.();
if (ts) {
return new Date(ts);
}
return null;
}
get signInProvider() {
return this.native.getSignInProvider?.();
}
get token() {
return this.native?.getToken?.();
}
}
const NSFirebaseAuth = lazy(() => org.nativescript.firebase.auth.FirebaseAuth);
export class Auth {
constructor(app) {
this._authStateChangeListeners = new Map();
this._idTokenChangeListeners = new Map();
if (app?.native) {
this._native = com.google.firebase.auth.FirebaseAuth.getInstance(app.native);
}
else {
if (defaultAuth) {
return defaultAuth;
}
defaultAuth = this;
}
}
useEmulator(host, port) {
this.native.useEmulator(host === 'localhost' || host === '127.0.0.1' ? '10.0.2.2' : host, port);
}
fetchSignInMethodsForEmail(email) {
return new Promise((resolve, reject) => {
if (!this.native) {
reject();
}
NSFirebaseAuth().fetchSignInMethodsForEmail(this.native, email, new org.nativescript.firebase.auth.FirebaseAuth.Callback({
onSuccess(success) {
const nativeArray = success.getSignInMethods().toArray();
const arr = [];
for (let i = 0; i < nativeArray.length; i++) {
arr.push(nativeArray[i]);
}
resolve(arr);
},
onError(error) {
reject({
message: error.getMessage(),
native: error,
});
},
}));
});
}
isSignInWithEmailLink(emailLink) {
return this.native.isSignInWithEmailLink(emailLink);
}
addAuthStateChangeListener(listener) {
if (this.native && typeof listener === 'function') {
const nativeListener = new com.google.firebase.auth.FirebaseAuth.AuthStateListener({
onAuthStateChanged(auth) {
listener(User.fromNative(auth.getCurrentUser()));
},
});
this.native.addAuthStateListener(nativeListener);
this._authStateChangeListeners.set(listener, nativeListener);
}
}
removeAuthStateChangeListener(listener) {
if (this.native && typeof listener === 'function') {
const nativeListener = this._authStateChangeListeners.get(listener);
if (nativeListener) {
this.native.removeAuthStateListener(nativeListener);
this._authStateChangeListeners.delete(nativeListener);
}
}
}
addIdTokenChangeListener(listener) {
if (this.native && typeof listener === 'function') {
const nativeListener = new com.google.firebase.auth.FirebaseAuth.IdTokenListener({
onIdTokenChanged(auth) {
listener(User.fromNative(auth.getCurrentUser()));
},
});
this.native.addIdTokenListener(nativeListener);
this._idTokenChangeListeners.set(listener, nativeListener);
}
}
removeIdTokenChangListener(listener) {
if (this.native && typeof listener === 'function') {
const nativeListener = this._authStateChangeListeners.get(listener);
if (nativeListener) {
this.native.removeIdTokenListener(nativeListener);
this._authStateChangeListeners.delete(nativeListener);
}
}
}
sendPasswordResetEmail(email, actionCodeSettings) {
return new Promise((resolve, reject) => {
if (!this.native) {
reject();
}
else {
NSFirebaseAuth().sendPasswordResetEmail(this.native, email, actionCodeSettings?.native || null, new org.nativescript.firebase.auth.FirebaseAuth.Callback({
onSuccess(success) {
resolve();
},
onError(error) {
reject(FirebaseError.fromNative(error));
},
}));
}
});
}
sendSignInLinkToEmail(email, actionCodeSettings) {
return new Promise((resolve, reject) => {
if (!this.native) {
reject();
}
else {
NSFirebaseAuth().sendSignInLinkToEmail(this.native, email, actionCodeSettings.native, new org.nativescript.firebase.auth.FirebaseAuth.Callback({
onSuccess(success) {
resolve();
},
onError(error) {
reject(FirebaseError.fromNative(error));
},
}));
}
});
}
signInAnonymously() {
return new Promise((resolve, reject) => {
if (!this.native) {
reject();
}
else {
NSFirebaseAuth().signInAnonymously(this.native, new org.nativescript.firebase.auth.FirebaseAuth.Callback({
onSuccess(success) {
resolve(toUserCredential(success));
},
onError(error) {
reject(FirebaseError.fromNative(error));
},
}));
}
});
}
signInWithProvider(provider) {
return new Promise((resolve, reject) => {
if (provider._isCustomProvider && provider._builder) {
org.nativescript.firebase.auth.FirebaseAuth.signInWithProvider(Application.android.foregroundActivity || Application.android.startActivity, this.native, provider._builder, new org.nativescript.firebase.auth.FirebaseAuth.Callback({
onSuccess(success) {
resolve(toUserCredential(success));
},
onError(error) {
reject({
message: error.getMessage(),
native: error,
});
},
}));
}
else {
reject(FirebaseError.fromNative(null, 'OAuthProvider not configured'));
}
});
}
getProviderCredential(provider) {
return new Promise((resolve, reject) => {
if (provider._isCustomProvider && provider._builder) {
org.nativescript.firebase.auth.FirebaseAuth.signInWithProvider(Application.android.foregroundActivity || Application.android.startActivity, this.native, provider._builder, new org.nativescript.firebase.auth.FirebaseAuth.Callback({
onSuccess(success) {
resolve(OAuthCredential.fromNative(success?.getCredential?.()));
},
onError(error) {
reject({
message: error.getMessage(),
native: error,
});
},
}));
}
else {
reject(FirebaseError.fromNative(null, 'OAuthProvider not configured'));
}
});
}
signInWithCredential(credential) {
return new Promise((resolve, reject) => {
if (!this.native) {
reject();
}
else {
NSFirebaseAuth().signInWithCredential(this.native, credential.native, new org.nativescript.firebase.auth.FirebaseAuth.Callback({
onSuccess(success) {
resolve(toUserCredential(success));
},
onError(error) {
reject(FirebaseError.fromNative(error));
},
}));
}
});
}
signInWithCustomToken(customToken) {
return new Promise((resolve, reject) => {
if (!this.native) {
reject();
}
else {
NSFirebaseAuth().signInWithCustomToken(this.native, customToken, new org.nativescript.firebase.auth.FirebaseAuth.Callback({
onSuccess(success) {
resolve(toUserCredential(success));
},
onError(error) {
reject(FirebaseError.fromNative(error));
},
}));
}
});
}
signInWithEmailLink(email, emailLink) {
return new Promise((resolve, reject) => {
if (!this.native) {
reject();
}
else {
NSFirebaseAuth().signInWithEmailLink(this.native, email, emailLink, new org.nativescript.firebase.auth.FirebaseAuth.Callback({
onSuccess(success) {
resolve(toUserCredential(success));
},
onError(error) {
reject(FirebaseError.fromNative(error));
},
}));
}
});
}
useUserAccessGroup(userAccessGroup) {
return Promise.reject();
}
verifyPasswordResetCode(code) {
return new Promise((resolve, reject) => {
if (!this.native) {
reject();
}
else {
NSFirebaseAuth().verifyPasswordResetCode(this.native, code, new org.nativescript.firebase.auth.FirebaseAuth.Callback({
onSuccess(success) {
resolve(success);
},
onError(error) {
reject(FirebaseError.fromNative(error));
},
}));
}
});
}
createUserWithEmailAndPassword(email, password) {
return new Promise((resolve, reject) => {
if (!this.native) {
reject();
}
else {
NSFirebaseAuth().createUserWithEmailAndPassword(this.native, email, password, new org.nativescript.firebase.auth.FirebaseAuth.Callback({
onSuccess(success) {
resolve(toUserCredential(success));
},
onError(error) {
reject(FirebaseError.fromNative(error));
},
}));
}
});
}
confirmPasswordReset(code, newPassword) {
return new Promise((resolve, reject) => {
if (!this.native) {
reject();
}
NSFirebaseAuth().confirmPasswordReset(this.native, code, newPassword, new org.nativescript.firebase.auth.FirebaseAuth.Callback({
onSuccess(success) {
resolve();
},
onError(error) {
reject({
message: error.getMessage(),
native: error,
});
},
}));
});
}
checkActionCode(code) {
return new Promise((resolve, reject) => {
if (!this.native) {
reject();
}
NSFirebaseAuth().checkActionCode(this.native, code, new org.nativescript.firebase.auth.FirebaseAuth.Callback({
onSuccess(success) {
const operation = toActionCodeOperation(success.getOperation());
const actionCodeInfo = {
data: {
email: undefined,
previousEmail: undefined,
},
operation,
};
const info = success.getInfo();
if (operation === ActionCodeInfoOperation.VerifyEmail || operation === ActionCodeInfoOperation.PasswordReset) {
actionCodeInfo.data.email = info.getEmail();
}
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.getEmail();
actionCodeInfo.data.previousEmail = info.getPreviousEmail();
}
resolve(actionCodeInfo);
},
onError(error) {
reject({
message: error.getMessage(),
native: error,
});
},
}));
});
}
applyActionCode(code) {
return new Promise((resolve, reject) => {
if (!this.native) {
reject();
}
else {
NSFirebaseAuth().applyActionCode(this.native, code, new org.nativescript.firebase.auth.FirebaseAuth.Callback({
onSuccess(success) {
resolve();
},
onError(error) {
reject(FirebaseError.fromNative(error));
},
}));
}
});
}
signInWithEmailAndPassword(email, password) {
return new Promise((resolve, reject) => {
if (!this.native) {
reject();
}
else {
NSFirebaseAuth().signInWithEmailAndPassword(this.native, email || '', password || '', new org.nativescript.firebase.auth.FirebaseAuth.Callback({
onSuccess(success) {
resolve(toUserCredential(success));
},
onError(error) {
reject(FirebaseError.fromNative(error));
},
}));
}
});
}
async signOut() {
return new Promise((resolve, reject) => {
const timeout = setTimeout(() => {
reject(false);
}, 5000);
const listener = (user) => {
this.removeAuthStateChangeListener(listener);
clearTimeout(timeout);
if (user) {
reject(false);
}
resolve(true);
};
this.addAuthStateChangeListener(listener);
this.native?.signOut();
});
}
get native() {
if (!this._native) {
this._native = com.google.firebase.auth.FirebaseAuth.getInstance();
}
return this._native;
}
get android() {
return this.native;
}
get app() {
if (!this._app) {
// @ts-ignore
this._app = FirebaseApp.fromNative(this.native.getApp());
}
return this._app;
}
get currentUser() {
return this.native ? User.fromNative(this.native.getCurrentUser()) : null;
}
get languageCode() {
return this.native?.getLanguageCode?.();
}
set languageCode(code) {
if (this.native) {
this.native.setLanguageCode(code);
}
}
get settings() {
if (!this._settings) {
this._settings = AuthSettings.fromNative(this.native.getFirebaseAuthSettings());
}
return this._settings;
}
get tenantId() {
return this.native.getTenantId();
}
set tenantId(id) {
if (this.native) {
this.native.setTenantId(id);
}
}
}
//# sourceMappingURL=index.android.js.map