forked from NativeScript/firebase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ios.ts
105 lines (96 loc) · 2.32 KB
/
index.ios.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { IAppCheck, IAppCheckToken } from './common';
import { firebase, FirebaseApp, FirebaseError } from '@nativescript/firebase-core';
declare const FIRApp;
let defaultAppCheck: AppCheck;
const fb = firebase();
Object.defineProperty(fb, 'appCheck', {
value: (app?: FirebaseApp) => {
if (!app) {
if (!defaultAppCheck) {
defaultAppCheck = new AppCheck();
}
return defaultAppCheck;
}
return new AppCheck(app);
},
writable: false,
});
export class AppCheckToken implements IAppCheckToken {
_native: FIRAppCheckToken;
static fromNative(token: FIRAppCheckToken) {
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(): string {
return this.native?.token;
}
get expireTimeMillis(): number {
return this.native?.expirationDate?.getTime?.();
}
}
let provider;
export class AppCheck implements IAppCheck {
_native: FIRAppCheck;
_nativeApp;
_app: FirebaseApp;
constructor(app?: FirebaseApp) {
if (app?.native) {
this._native = FIRAppCheck.appCheckWithApp(app.native);
this._nativeApp = app.native;
} else {
if (defaultAppCheck) {
return defaultAppCheck;
}
defaultAppCheck = this;
this._native = FIRAppCheck.appCheck();
this._nativeApp = FIRApp.defaultApp();
}
}
static setProviderFactory() {
if (!provider) {
provider = FIRAppCheckDebugProviderFactory.alloc().init();
}
FIRAppCheck.setAppCheckProviderFactory(provider);
}
activate(isTokenAutoRefreshEnabled: boolean) {
this.native.isTokenAutoRefreshEnabled = isTokenAutoRefreshEnabled;
}
getToken(forceRefresh: boolean): Promise<AppCheckToken> {
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: boolean) {
this.native.isTokenAutoRefreshEnabled = enabled;
}
get native() {
return this._native;
}
get ios() {
return this.native;
}
get app(): FirebaseApp {
if (!this._app) {
// @ts-ignore
this._app = FirebaseApp.fromNative(this._nativeApp);
}
return this._app;
}
}