Skip to content

Commit da7abdb

Browse files
Add App Check non-Firebase resource snippets (#207)
* Add App Check non-Firebase resource snippets * Pass lint * npm run snippet * more lint fixes * npm run snippets again
1 parent f29550c commit da7abdb

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

appcheck-next/index.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,40 @@ function initializeCustomProvider() {
6363
});
6464
// [END appcheck_initialize_custom_provider]
6565
}
66+
67+
function nonFirebase() {
68+
const { initializeApp } = require("firebase/app");
69+
const app = initializeApp({
70+
// Your firebase configuration object
71+
});
72+
const { ReCaptchaV3Provider } = require('firebase/app-check');
73+
const provider = new ReCaptchaV3Provider('');
74+
75+
// [START appcheck_nonfirebase]
76+
const { initializeAppCheck, getToken } = require('firebase/app-check');
77+
78+
const appCheck = initializeAppCheck(
79+
app,
80+
{ provider: provider } // ReCaptchaV3Provider or CustomProvider
81+
);
82+
83+
const callApiWithAppCheckExample = async () => {
84+
let appCheckTokenResponse;
85+
try {
86+
appCheckTokenResponse = await getToken(appCheck, /* forceRefresh= */ false);
87+
} catch (err) {
88+
// Handle any errors if the token was not retrieved.
89+
return;
90+
}
91+
92+
// Include the App Check token with requests to your server.
93+
const apiResponse = await fetch('https://yourbackend.example.com/yourApiEndpoint', {
94+
headers: {
95+
'X-Firebase-AppCheck': appCheckTokenResponse.token,
96+
}
97+
});
98+
99+
// Handle response from your backend.
100+
};
101+
// [END appcheck_nonfirebase]
102+
}

appcheck/index.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,24 @@ function initializeCustomProvider() {
5353
appCheck.activate(appCheckCustomProvider);
5454
// [END appcheck_initialize_custom_provider]
5555
}
56+
57+
// [START appcheck_nonfirebase]
58+
const callApiWithAppCheckExample = async () => {
59+
let appCheckTokenResponse;
60+
try {
61+
appCheckTokenResponse = await firebase.appCheck().getToken(/* forceRefresh= */ false);
62+
} catch (err) {
63+
// Handle any errors if the token was not retrieved.
64+
return;
65+
}
66+
67+
// Include the App Check token with requests to your server.
68+
const apiResponse = await fetch('https://yourbackend.example.com/yourApiEndpoint', {
69+
headers: {
70+
'X-Firebase-AppCheck': appCheckTokenResponse.token,
71+
}
72+
});
73+
74+
// Handle response from your backend.
75+
};
76+
// [END appcheck_nonfirebase]
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// This snippet file was generated by processing the source file:
2+
// ./appcheck-next/index.js
3+
//
4+
// To make edits to the snippets in this file, please edit the source
5+
6+
// [START appcheck_nonfirebase_modular]
7+
import { initializeAppCheck, getToken } from 'firebase/app-check';
8+
9+
const appCheck = initializeAppCheck(
10+
app,
11+
{ provider: provider } // ReCaptchaV3Provider or CustomProvider
12+
);
13+
14+
const callApiWithAppCheckExample = async () => {
15+
let appCheckTokenResponse;
16+
try {
17+
appCheckTokenResponse = await getToken(appCheck, /* forceRefresh= */ false);
18+
} catch (err) {
19+
// Handle any errors if the token was not retrieved.
20+
return;
21+
}
22+
23+
// Include the App Check token with requests to your server.
24+
const apiResponse = await fetch('https://yourbackend.example.com/yourApiEndpoint', {
25+
headers: {
26+
'X-Firebase-AppCheck': appCheckTokenResponse.token,
27+
}
28+
});
29+
30+
// Handle response from your backend.
31+
};
32+
// [END appcheck_nonfirebase_modular]

0 commit comments

Comments
 (0)