@nativescript/firebase-ui
Version:
NativeScript Firebase - UI
512 lines • 16.2 kB
JavaScript
import { Utils } from '@nativescript/core';
import { FirebaseApp, FirebaseError, firebase } from '@nativescript/firebase-core';
import { ProviderBase } from './common';
let defaultUI;
const fb = firebase();
Object.defineProperty(fb, 'ui', {
value: (auth) => {
if (!auth) {
if (!defaultUI) {
defaultUI = new UI();
}
return defaultUI;
}
return new UI(auth);
},
writable: false,
});
// https://github.com/firebase/FirebaseUI-iOS/blob/master/FirebaseOAuthUI/Sources/FUIOAuth.m
export class GoogleProvider extends ProviderBase {
constructor() {
super();
this.scopes = [];
}
getNative(ui) {
if (this.scopes.length > 0) {
return FUIGoogleAuth.alloc().initWithAuthUIScopes(ui.native, this.scopes);
}
else {
return FUIGoogleAuth.alloc().initWithAuthUI(ui.native);
}
}
}
export class FacebookProvider extends ProviderBase {
constructor() {
super();
this.permissions = [];
}
getNative(ui) {
if (this.permissions.length > 0) {
return FUIFacebookAuth.alloc().initWithAuthUIPermissions(ui.native, this.permissions);
}
else {
return FUIFacebookAuth.alloc().initWithAuthUI(ui.native);
}
}
}
export class TwitterProvider extends ProviderBase {
constructor() {
super();
this.scopes = [];
}
setCustomParameters(params) {
if (typeof params === 'object') {
this._setCustomParameters = params;
}
return this;
}
getNative(ui) {
return FUIOAuth.twitterAuthProviderWithAuthUI(ui.native);
}
}
export class GithubProvider extends ProviderBase {
constructor() {
super();
this.scopes = [];
}
setCustomParameters(params) {
if (typeof params === 'object') {
this._setCustomParameters = params;
}
return this;
}
getNative(ui) {
return FUIOAuth.githubAuthProviderWithAuthUI(ui.native);
}
}
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 EmailProvider extends ProviderBase {
constructor() {
super();
this.actionCodeSettings = null;
this.requireName = true;
this._forceSameDevice = false;
this._enableEmailLinkSignIn = false;
this.defaultEmail = '';
this.allowNewAccounts = true;
}
forceSameDevice() {
this._forceSameDevice = true;
return this;
}
enableEmailLinkSignIn() {
this._enableEmailLinkSignIn = true;
return this;
}
getNative(ui) {
if (this.requireName) {
return FUIEmailAuth.alloc().initAuthAuthUISignInMethodForceSameDeviceAllowNewEmailAccountsRequireDisplayNameActionCodeSetting(ui.native, this.actionCodeSettings ? FIREmailLinkAuthSignInMethod : FIREmailPasswordAuthSignInMethod, this._forceSameDevice, this.allowNewAccounts, this.requireName, this.actionCodeSettings?.native ?? FIRActionCodeSettings.new());
}
return FUIEmailAuth.alloc().initAuthAuthUISignInMethodForceSameDeviceAllowNewEmailAccountsActionCodeSetting(ui.native, FIREmailLinkAuthSignInMethod, this._forceSameDevice, this.allowNewAccounts, this.actionCodeSettings?.native ?? FIRActionCodeSettings.new());
}
}
export class PhoneProvider extends ProviderBase {
constructor() {
super();
this.defaultNumber = '';
this.defaultCountryIso = '';
this.blacklistedCountries = [];
this.whitelistedCountries = [];
}
getNative(ui) {
if (this.blacklistedCountries.length > 0) {
const countries = NSMutableSet.new();
this.blacklistedCountries.forEach((country) => {
countries.addObject(country);
});
return FUIPhoneAuth.alloc().initWithAuthUIBlacklistedCountries(ui.native, countries);
}
else if (this.whitelistedCountries.length > 0) {
const countries = NSMutableSet.new();
this.whitelistedCountries.forEach((country) => {
countries.addObject(country);
});
return FUIPhoneAuth.alloc().initWithAuthUIWhitelistedCountries(ui.native, countries);
}
else {
return FUIPhoneAuth.alloc().initWithAuthUI(ui.native);
}
}
}
export class AnonymousProvider extends ProviderBase {
constructor() {
super();
}
getNative(ui) {
return FUIAnonymousAuth.alloc().initWithAuthUI(ui.native);
}
}
export class MicrosoftProvider extends ProviderBase {
constructor() {
super();
this.scopes = [];
}
setCustomParameters(params) {
if (typeof params === 'object') {
this._setCustomParameters = params;
}
return this;
}
getNative(ui) {
return FUIOAuth.microsoftAuthProviderWithAuthUI(ui.native);
}
}
export class YahooProvider extends ProviderBase {
constructor() {
super();
this.scopes = [];
}
setCustomParameters(params) {
if (typeof params === 'object') {
this._setCustomParameters = params;
}
return this;
}
getNative(ui) {
return FUIOAuth.yahooAuthProviderWithAuthUI(ui.native);
}
}
export class AppleProvider extends ProviderBase {
constructor() {
super();
this.scopes = [];
}
setCustomParameters(params) {
if (typeof params === 'object') {
this._setCustomParameters = params;
}
return this;
}
getNative(ui) {
return FUIOAuth.appleAuthProviderWithAuthUI(ui.native);
}
}
export class User {
static fromNative(user) {
if (user instanceof FIRUser) {
const ret = new User();
ret._native = user;
return ret;
}
return null;
}
get native() {
return this._native;
}
get android() {
return this.native;
}
get name() {
return this.native.displayName;
}
get email() {
return this.native.email;
}
get phoneNumber() {
return this.native.phoneNumber;
}
get photoUri() {
return this.native.photoURL?.absoluteString;
}
get providerId() {
return this.native.providerID;
}
toJSON() {
return {
name: this.name,
email: this.email,
phoneNumber: this.phoneNumber,
photoUri: this.photoUri,
};
}
}
export class IdpResponse {
constructor() {
this._isOauthCred = false;
}
static fromNative(response) {
if (response instanceof FIRAuthDataResult) {
const ret = new IdpResponse();
ret._native = response;
ret._isOauthCred = response.credential instanceof FIROAuthCredential;
return ret;
}
return null;
}
get native() {
return this._native;
}
get android() {
return this.native;
}
get isNewUser() {
return this.native.additionalUserInfo?.newUser() ?? false;
}
get hasCredentialForLinking() {
return !!this.native.credential;
}
get email() {
return this.native.user.email;
}
get idpSecret() {
if (this._isOauthCred) {
const cred = this.native.credential;
return cred.secret;
}
return null;
}
get idpToken() {
if (this._isOauthCred) {
const cred = this.native.credential;
return cred.accessToken;
}
return null;
}
get phoneNumber() {
return this.native.user.phoneNumber;
}
get providerType() {
return this.native?.credential?.provider ?? null;
}
get user() {
if (!this._user) {
this._user = User.fromNative(this.native.user);
}
return this._user;
}
}
var FUIAuthDelegateImpl = /** @class */ (function (_super) {
__extends(FUIAuthDelegateImpl, _super);
function FUIAuthDelegateImpl() {
return _super !== null && _super.apply(this, arguments) || this;
}
FUIAuthDelegateImpl_1 = FUIAuthDelegateImpl;
FUIAuthDelegateImpl.initWithOwner = function (owner) {
var delegate = FUIAuthDelegateImpl_1.new();
delegate._owner = owner;
return delegate;
};
FUIAuthDelegateImpl.prototype.authUIDidSignInWithAuthDataResultError = function (authUI, authDataResult, error) {
this._authDataResult = authDataResult;
this._handleAuth(error);
};
FUIAuthDelegateImpl.prototype.authUIDidSignInWithAuthDataResultURLError = function (authUI, authDataResult, url, error) {
this._authDataResult = authDataResult;
this._handleAuth(error);
};
FUIAuthDelegateImpl.prototype._handleAuth = function (error) {
var _a, _b;
var owner = (_b = (_a = this._owner) === null || _a === void 0 ? void 0 : _a.get) === null || _b === void 0 ? void 0 : _b.call(_a);
if (!owner._resolve && !owner._reject) {
return;
}
if (error) {
if (error.code === FUIAuthErrorCode.UserCancelledSignIn) {
owner._reject(FirebaseError.fromNative(null, 'User Cancelled'));
}
else {
owner._reject(FirebaseError.fromNative(error));
}
// close controller after an error
owner._controller.dismissViewControllerAnimatedCompletion(true, function () { });
}
else {
owner._resolve(IdpResponse.fromNative(this._authDataResult));
}
owner._reject = null;
owner._resolve = null;
owner._controller = null;
this._authDataResult = null;
};
var FUIAuthDelegateImpl_1;
FUIAuthDelegateImpl = FUIAuthDelegateImpl_1 = __decorate([
ObjCClass(FUIAuthDelegate)
], FUIAuthDelegateImpl);
return FUIAuthDelegateImpl;
}(NSObject));
export class UI {
constructor(auth) {
const native = auth?.native;
if (native) {
this._native = FUIAuth.authUIWithAuth(native);
this._auth = auth;
}
else {
if (defaultUI) {
return defaultUI;
}
defaultUI = this;
this._native = FUIAuth.defaultAuthUI();
}
this._delegate = FUIAuthDelegateImpl.initWithOwner(new WeakRef(this));
this._native.delegate = this._delegate;
}
static fromNative(ui) {
if (ui instanceof FUIAuth) {
const ret = new UI();
ret._native = ui;
return ret;
}
return null;
}
get native() {
return this._native;
}
get android() {
return this.native;
}
get app() {
if (this._auth) {
return this._auth.app;
}
if (!this._app) {
// @ts-ignore
this._app = FirebaseApp.fromNative(this.native.getApp());
}
return this._app;
}
useEmulator(host, port) {
this.native.useEmulatorWithHostPort(host, port);
}
show(config) {
return new Promise((resolve, reject) => {
try {
if (config.providers.length > 0) {
const providers = config.providers
.map((provider) => {
return provider.getNative(this);
})
.filter((value) => !!value);
this.native.providers = providers;
}
if (config.anonymousUsersAutoUpgrade) {
this.native.autoUpgradeAnonymousUsers = true;
}
else {
this.native.autoUpgradeAnonymousUsers = false;
}
if (config.tosAndPrivacyPolicy?.tos && config.tosAndPrivacyPolicy?.privacyPolicy) {
this.native.TOSURL = NSURL.URLWithString(config.tosAndPrivacyPolicy?.tos);
this.native.privacyPolicyURL = NSURL.URLWithString(config.tosAndPrivacyPolicy?.privacyPolicy);
}
this._resolve = resolve;
this._reject = reject;
const keyWindow = UIApplication.sharedApplication.keyWindow;
const vc = keyWindow != null ? keyWindow.rootViewController : undefined;
const controller = Utils.ios.getVisibleViewController(vc);
this._controller = this.native.authViewController();
controller.presentViewControllerAnimatedCompletion(this._controller, true, () => { });
}
catch (error) {
reject(new FirebaseError(error?.message || 'Failed to show UI'));
this._resolve = null;
this._reject = null;
}
});
}
delete() {
return new Promise((resolve, reject) => {
if (this._auth) {
const currentUser = this._auth.currentUser;
if (!currentUser) {
reject(new FirebaseError('No user is currently logged in.'));
return;
}
return this._auth.currentUser.delete();
}
const auth = FIRAuth.authWithApp(this.app.native);
const currentUser = auth.currentUser;
if (!currentUser) {
reject(new FirebaseError('No user is currently logged in.'));
return;
}
currentUser.deleteWithCompletion((error) => {
if (!error) {
const err = FirebaseError.fromNative(error);
reject(err);
}
else {
resolve();
}
});
});
}
signOut() {
return new Promise((resolve, reject) => {
try {
this.native.signOutWithError();
resolve();
}
catch (error) {
reject(FirebaseError.fromNative(error.message));
}
});
}
}
//# sourceMappingURL=index.ios.js.map