@nativescript/firebase-app-check
Version:
NativeScript Firebase - App Check
193 lines • 6.99 kB
JavaScript
import { firebase, FirebaseApp, FirebaseError } from '@nativescript/firebase-core';
import { Device } from '@nativescript/core';
let defaultAppCheck;
const fb = firebase();
Object.defineProperty(fb, 'appCheck', {
value: (app) => {
if (!app) {
if (!defaultAppCheck) {
defaultAppCheck = new AppCheck();
}
return defaultAppCheck;
}
return new AppCheck(app);
},
writable: false,
});
export class AppCheckToken {
static fromNative(token) {
if (token instanceof FIRAppCheckToken) {
const t = new AppCheckToken();
t._native = token;
return t;
}
return null;
}
get native() {
return this._native;
}
get ios() {
return this.native;
}
get token() {
return this.native?.token;
}
get expireTimeMillis() {
return this.native?.expirationDate?.getTime?.();
}
}
var FIRAppCheckProviderFactoryImpl = /** @class */ (function (_super) {
__extends(FIRAppCheckProviderFactoryImpl, _super);
function FIRAppCheckProviderFactoryImpl() {
return _super !== null && _super.apply(this, arguments) || this;
}
FIRAppCheckProviderFactoryImpl.prototype.createProviderWithApp = function (app) {
var provider;
if (parseFloat(Device.sdkVersion) > 14) {
provider = FIRAppAttestProvider.alloc().initWithApp(app);
}
else {
provider = FIRDeviceCheckProvider.alloc().initWithApp(app);
}
return provider;
};
FIRAppCheckProviderFactoryImpl = __decorate([
ObjCClass(FIRAppCheckProviderFactory)
], FIRAppCheckProviderFactoryImpl);
return FIRAppCheckProviderFactoryImpl;
}(NSObject));
var FIRAppCheckProviderImpl = /** @class */ (function (_super) {
__extends(FIRAppCheckProviderImpl, _super);
function FIRAppCheckProviderImpl() {
return _super !== null && _super.apply(this, arguments) || this;
}
FIRAppCheckProviderImpl_1 = FIRAppCheckProviderImpl;
FIRAppCheckProviderImpl.initWithOwner = function (owner) {
var provider = FIRAppCheckProviderImpl_1.new();
provider._owner = owner;
provider._queue = dispatch_queue_create('FIRAppCheckProviderImpl', null);
return provider;
};
FIRAppCheckProviderImpl.prototype.getTokenWithCompletion = function (handler) {
var _this = this;
dispatch_async(this._queue, function () {
var _a, _b, _c, _d;
var callback = function (token, error) {
if (token) {
handler(FIRAppCheckToken.alloc().initWithTokenExpirationDate(token.token, token.expirationDate), null);
}
else {
handler(null, error.intoNative());
}
};
(_d = (_b = (_a = _this._owner).get) === null || _b === void 0 ? void 0 : (_c = _b.call(_a)).getToken) === null || _d === void 0 ? void 0 : _d.call(_c, callback);
});
};
var FIRAppCheckProviderImpl_1;
FIRAppCheckProviderImpl = FIRAppCheckProviderImpl_1 = __decorate([
ObjCClass(FIRAppCheckProvider)
], FIRAppCheckProviderImpl);
return FIRAppCheckProviderImpl;
}(NSObject));
var FIRAppCheckProviderFactoryCustomImpl = /** @class */ (function (_super) {
__extends(FIRAppCheckProviderFactoryCustomImpl, _super);
function FIRAppCheckProviderFactoryCustomImpl() {
return _super !== null && _super.apply(this, arguments) || this;
}
FIRAppCheckProviderFactoryCustomImpl_1 = FIRAppCheckProviderFactoryCustomImpl;
FIRAppCheckProviderFactoryCustomImpl.initWithOwner = function (owner) {
var provider = FIRAppCheckProviderFactoryCustomImpl_1.new();
provider._owner = owner;
return provider;
};
FIRAppCheckProviderFactoryCustomImpl.prototype.createProviderWithApp = function (app) {
var _a, _b, _c, _d, _e;
return ((_e = (_d = (_c = (_b = (_a = this._owner).get) === null || _b === void 0 ? void 0 : _b.call(_a)) === null || _c === void 0 ? void 0 : _c.createProvider) === null || _d === void 0 ? void 0 : _d.call(_c, FirebaseApp.fromNative(app))) === null || _e === void 0 ? void 0 : _e.native) || null;
};
var FIRAppCheckProviderFactoryCustomImpl_1;
FIRAppCheckProviderFactoryCustomImpl = FIRAppCheckProviderFactoryCustomImpl_1 = __decorate([
ObjCClass(FIRAppCheckProviderFactory)
], FIRAppCheckProviderFactoryCustomImpl);
return FIRAppCheckProviderFactoryCustomImpl;
}(NSObject));
export class AppCheckProviderFactory {
constructor() {
this._native = FIRAppCheckProviderFactoryCustomImpl.initWithOwner(new WeakRef(this));
}
get native() {
return this._native;
}
}
export class AppCheckProvider {
constructor() {
this._native = FIRAppCheckProviderImpl.initWithOwner(new WeakRef(this));
}
get native() {
return this._native;
}
}
let provider;
let customProvider;
export class AppCheck {
constructor(app) {
if (app?.native) {
this._native = FIRAppCheck.appCheckWithApp(app.native);
this._nativeApp = app.native;
}
else {
if (defaultAppCheck) {
return defaultAppCheck;
}
defaultAppCheck = this;
const app = FIRApp.defaultApp();
this._native = FIRAppCheck.appCheckWithApp(app);
this._nativeApp = app;
}
}
static setProviderFactory(custom) {
if (custom && custom instanceof AppCheckProviderFactory) {
customProvider = custom;
FIRAppCheck.setAppCheckProviderFactory(custom.native);
}
else {
if (!provider) {
provider = FIRAppCheckProviderFactoryImpl.alloc().init();
}
customProvider = undefined;
FIRAppCheck.setAppCheckProviderFactory(provider);
}
}
activate(isTokenAutoRefreshEnabled) {
this.native.isTokenAutoRefreshEnabled = isTokenAutoRefreshEnabled;
}
getToken(forceRefresh) {
return new Promise((resolve, reject) => {
this.native.tokenForcingRefreshCompletion(forceRefresh, (token, error) => {
if (error) {
const err = FirebaseError.fromNative(error);
reject(err);
}
else {
resolve(AppCheckToken.fromNative(token));
}
});
});
}
setTokenAutoRefreshEnabled(enabled) {
this.native.isTokenAutoRefreshEnabled = enabled;
}
get native() {
return this._native;
}
get ios() {
return this.native;
}
get app() {
if (!this._app) {
// @ts-ignore
this._app = FirebaseApp.fromNative(this._nativeApp);
}
return this._app;
}
}
//# sourceMappingURL=index.ios.js.map