forked from NativeScript/firebase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.android.ts
171 lines (157 loc) · 4.42 KB
/
index.android.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import { IAppCheck, IAppCheckToken } from './common';
import { firebase, FirebaseApp, FirebaseError } from '@nativescript/firebase-core';
import lazy from '@nativescript/core/utils/lazy';
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,
});
const NSAppCheck = lazy(() => org.nativescript.firebase.app_check.FirebaseAppCheck);
export class AppCheckToken implements IAppCheckToken {
_native: com.google.firebase.appcheck.AppCheckToken;
static fromNative(token: com.google.firebase.appcheck.AppCheckToken) {
if (token instanceof com.google.firebase.appcheck.AppCheckToken) {
const t = new AppCheckToken();
t._native = token;
return t;
}
return null;
}
get native() {
return this._native;
}
get android() {
return this.native;
}
get token(): string {
return this.native?.getToken?.();
}
get expireTimeMillis(): number {
return this.native?.getExpireTimeMillis?.();
}
}
export abstract class AppCheckProviderFactory {
_native: com.google.firebase.appcheck.AppCheckProviderFactory;
constructor() {
const ref = new WeakRef(this);
this._native = new com.google.firebase.appcheck.AppCheckProviderFactory({
create(app) {
return ref.get?.()?.createProvider?.((<any>FirebaseApp).fromNative(app))?.native || null;
},
});
}
abstract createProvider(app: FirebaseApp): AppCheckProvider;
get native() {
return this._native;
}
}
export abstract class AppCheckProvider {
_native;
_callback;
constructor() {
const ref = new WeakRef(this);
this._callback = new (<any>org).nativescript.firebase.app_check.FirebaseAppCheck.CustomAppCheckProvider.Callback({
getToken() {
let result;
const callback = (token: { token: string; expirationDate: Date }, error: FirebaseError) => {
if (token) {
token = new (<any>org).nativescript.firebase.app_check.FirebaseAppCheck.AppCheckToken(token.token, token.expirationDate.getTime());
} else {
result = (error as any).intoNative();
}
};
ref.get?.().getToken(callback);
return result;
},
});
this._native = (<any>org).nativescript.firebase.app_check.FirebaseAppCheck.CustomAppCheckProvider(this._callback);
}
abstract getToken(
done: (
token: {
token: string;
expirationDate: Date;
},
error: FirebaseError
) => void
);
get native() {
return this._native;
}
}
let customProvider: AppCheckProviderFactory;
export class AppCheck implements IAppCheck {
_native: com.google.firebase.appcheck.FirebaseAppCheck;
_nativeApp;
constructor(app?: FirebaseApp) {
if (app?.native) {
this._nativeApp = app.native;
this._native = com.google.firebase.appcheck.FirebaseAppCheck.getInstance(app.native);
} else {
if (defaultAppCheck) {
return defaultAppCheck;
}
defaultAppCheck = this;
this._nativeApp = (<any>com).google.firebase.FirebaseApp.getInstance();
this._native = com.google.firebase.appcheck.FirebaseAppCheck.getInstance();
}
}
static setProviderFactory(custom?: AppCheckProviderFactory) {
if (custom && custom instanceof AppCheckProviderFactory) {
customProvider = custom;
} else {
customProvider = undefined;
}
}
activate(isTokenAutoRefreshEnabled: boolean) {
this.native.setTokenAutoRefreshEnabled(isTokenAutoRefreshEnabled);
if (customProvider) {
this.native.installAppCheckProviderFactory(customProvider.native);
} else {
this.native.installAppCheckProviderFactory(com.google.firebase.appcheck.safetynet.SafetyNetAppCheckProviderFactory.getInstance());
}
}
getToken(forceRefresh: boolean): Promise<AppCheckToken> {
return new Promise((resolve, reject) => {
NSAppCheck().getToken(
this.native,
forceRefresh,
new org.nativescript.firebase.app_check.FirebaseAppCheck.Callback({
onSuccess(param0) {
resolve(AppCheckToken.fromNative(param0));
},
onError(param0) {
const err = FirebaseError.fromNative(param0);
reject(err);
},
})
);
});
}
setTokenAutoRefreshEnabled(enabled: boolean) {
this.native.setTokenAutoRefreshEnabled(enabled);
}
get native() {
return this._native;
}
get ios() {
return this.native;
}
_app: FirebaseApp;
get app(): FirebaseApp {
if (!this._app) {
// @ts-ignore
this._app = FirebaseApp.fromNative(this._nativeApp);
}
return this._app;
}
}