forked from firebase/snippets-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
86 lines (71 loc) · 2.34 KB
/
index.js
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
import firebase from "firebase/app";
import "firebase/app-check";
function initialize() {
// [START appcheck_initialize]
firebase.initializeApp({
// Your firebase configuration object
});
const appCheck = firebase.appCheck();
// Pass your reCAPTCHA v3 site key (public key) to activate(). Make sure this
// key is the counterpart to the secret key you set in the Firebase console.
appCheck.activate(
'abcdefghijklmnopqrstuvwxy-1234567890abcd',
// Optional argument. If true, the SDK automatically refreshes App Check
// tokens as needed.
true);
// [END appcheck_initialize]
}
function customProvider() {
// [START appcheck_custom_provider]
const appCheckCustomProvider = {
getToken: () => {
return new Promise((resolve, _reject) => {
// TODO: Logic to exchange proof of authenticity for an App Check token and
// expiration time.
// [START_EXCLUDE]
const tokenFromServer = "abc1234";
const expirationFromServer = 1234;
// [END_EXCLUDE]
const appCheckToken = {
token: tokenFromServer,
expireTimeMillis: expirationFromServer * 1000
};
resolve(appCheckToken);
});
}
};
// [END appcheck_custom_provider]
return appCheckCustomProvider;
}
function initializeCustomProvider() {
const appCheckCustomProvider = customProvider();
// [START appcheck_initialize_custom_provider]
firebase.initializeApp({
// Your firebase configuration object
});
const appCheck = firebase.appCheck();
appCheck.activate(
appCheckCustomProvider,
// Optional argument. If true, the SDK automatically refreshes App Check
// tokens as needed.
true);
// [END appcheck_initialize_custom_provider]
}
// [START appcheck_nonfirebase]
const callApiWithAppCheckExample = async () => {
let appCheckTokenResponse;
try {
appCheckTokenResponse = await firebase.appCheck().getToken(/* forceRefresh= */ false);
} catch (err) {
// Handle any errors if the token was not retrieved.
return;
}
// Include the App Check token with requests to your server.
const apiResponse = await fetch('https://yourbackend.example.com/yourApiEndpoint', {
headers: {
'X-Firebase-AppCheck': appCheckTokenResponse.token,
}
});
// Handle response from your backend.
};
// [END appcheck_nonfirebase]