Skip to main content

Sample Web Page to fetch the user data by API

<!doctype html>
<html>
<head>
<title>Firebase Auth</title>
<meta charset="UTF-8" />
<!-- json content type -->
<meta http-equiv="Content-Type" content="application/json; charset=utf-8" />
<script type="module">
// credential configuration
// MODIFY THESE TO YOUR OWN CREDENTIALS
const gofaFirebaseAuthEmail = "__FIREBASE_AUTH_EMAIL__";
const gofaFirebaseAuthPassword = "__FIREBASE_AUTH_PASSWORD__";
const clientId = "abc"; // this sample client id is for testing only
const clientSecret = "abcSecretKey123"; // this sample client secret is for testing only

// general configuration
// DO NOT MODIFY THESE
const gofaFunctionHost =
"https://us-central1-gofa-sdk.cloudfunctions.net";
const gofaFunctionGetUserData = `${gofaFunctionHost}/expressApp/user?`;

// target user id
// MODIFY THIS TO YOUR TARGET USER ID
const clientUserId = "gofa_tester";

// Import the functions you need from the SDKs you need
import { initializeApp } from "https://www.gstatic.com/firebasejs/10.4.0/firebase-app.js";
import {
getAuth,
signInWithEmailAndPassword,
} from "https://www.gstatic.com/firebasejs/10.4.0/firebase-auth.js";

// Initialize GOFA's Firebase
// DO NOT MODIFY THIS
var firebaseConfig = {
apiKey: "AIzaSyAfCRiduJRCEXfp_ap5i91O0XU-HN1yeAQ",
authDomain: "gofa-sdk.firebaseapp.com",
databaseURL:
"https://gofa-sdk-default-rtdb.asia-southeast1.firebasedatabase.app",
projectId: "gofa-sdk",
storageBucket: "gofa-sdk.appspot.com",
messagingSenderId: "1082100926736",
appId: "1:1082100926736:web:1e43f1a664395e5d10fd95",
measurementId: "G-PZ90ZKN3KF",
};

// Initialize Firebase
const app = initializeApp(firebaseConfig);
const auth = getAuth(app);

// Authenticate user
signInWithEmailAndPassword(
auth,
gofaFirebaseAuthEmail,
gofaFirebaseAuthPassword,
)
.catch((error) => {
var errorCode = error.code;
var errorMessage = error.message;
console.error("Error signing in:", errorCode, errorMessage);
})
.then((userCredential) => {
// Signed in
var user = userCredential.user;
console.log("User signed in:", user);

// get access token
return user.getIdToken(true /*forceRefresh*/);
})
.catch((error) => {
console.error("Error getting user access token:", error);
})
.then((idToken) => {
console.log("User access token:", idToken);

// // Access Cloud Functions

return fetch(
gofaFunctionGetUserData +
new URLSearchParams({
clientId,
clientSecret,
clientUserId,
}),
{
method: "GET",
headers: {
Authorization: `Bearer ${idToken}`,
},
},
);
})
.catch((error) => {
console.error("Error calling Cloud Function:", error);
})
.then((response) => response.json())
.then((data) => {
GET / user - data;
console.log("Cloud Function result:", data);

// Display the JSON in the browser
document.getElementById("json").textContent = JSON.stringify(
data,
null,
2,
);
});
</script>
</head>
<body>
<pre id="json"></pre>
</body>
</html>