Skip to content

Commit dcc21b3

Browse files
committed
API endpoint: Add database credentials
1 parent 6cea25e commit dcc21b3

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

pages/api/test.js

+17-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,26 @@ import admin from "firebase-admin";
22

33
const test = async (req, res) => {
44
if (!admin.apps.length) {
5-
admin.initializeApp();
5+
admin.initializeApp({
6+
credential: admin.credential.cert({
7+
projectId: "next-on-netlify-firebase",
8+
clientEmail: process.env.FIREBASE_CLIENT_EMAIL,
9+
// Need to un-escape linebreaks from private key.
10+
// See: https://stackoverflow.com/a/50376092/6451879
11+
privateKey: process.env.FIREBASE_PRIVATE_KEY.replace(/\\n/g, "\n"),
12+
}),
13+
databaseURL: "https://next-on-netlify-firebase.firebaseio.com",
14+
});
615
console.log("initialized!");
716
}
817

9-
return res.json({ result: "success" });
18+
// Fetch all users
19+
const dbRef = admin.database().ref();
20+
const usersRef = dbRef.child("users");
21+
const usersQuery = await usersRef.once("value");
22+
const users = usersQuery.val();
23+
24+
return res.json({ users });
1025
};
1126

1227
export default test;

0 commit comments

Comments
 (0)