Skip to content

Commit

Permalink
init firebase with firebase-admin
Browse files Browse the repository at this point in the history
  • Loading branch information
voronin-ivan committed Sep 27, 2020
1 parent 781207e commit 72d1692
Show file tree
Hide file tree
Showing 5 changed files with 275 additions and 27 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"@prefresh/next": "1.2.0",
"axios": "0.20.0",
"firebase": "7.21.0",
"firebase-admin": "9.2.0",
"next": "9.5.3",
"preact": "10.5.2",
"preact-render-to-string": "5.1.10",
Expand Down
7 changes: 5 additions & 2 deletions pages/api/hackathons.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { NextApiRequest, NextApiResponse } from 'next';
import db from '../../utils/server/database';
import getFirebaseInstance from '../../utils/server/getFirebaseInstance';
import { Hackathon } from '../../types/hackathon';

const getHackathons = async (_: NextApiRequest, res: NextApiResponse) => {
console.log(process.env);
try {
const hackathonsRef = db.ref('hackathons');
const firebase = getFirebaseInstance();
const hackathonsRef = firebase.database().ref('hackathons');
const snapshot = await hackathonsRef.once('value');
const hackathons: Hackathon[] = snapshot.val();

await firebase.delete();

res.status(200).json({ hackathons });
} catch (e) {
console.error(e);
Expand Down
16 changes: 0 additions & 16 deletions utils/server/database.ts

This file was deleted.

19 changes: 19 additions & 0 deletions utils/server/getFirebaseInstance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import firebase from 'firebase-admin';
import 'firebase/database';

const initialConfig = {
apiKey: process.env.API_KEY,
authDomain: process.env.AUTH_DOMAIN,
databaseURL: process.env.DATABASE_URL,
projectId: process.env.PROJECT_ID,
appId: process.env.APP_ID,
credential: firebase.credential.cert({
clientEmail: process.env.CLIENT_EMAIL,
projectId: process.env.PROJECT_ID,
privateKey: process.env.PRIVATE_KEY?.replace(/\\n/g, '\n'),
}),
};

const getFirebaseInstance = () => firebase.apps[0] || firebase.initializeApp(initialConfig);

export default getFirebaseInstance;
Loading

0 comments on commit 72d1692

Please sign in to comment.