From 388d98922995b20a6701c23fcfc2d3798f15fcf0 Mon Sep 17 00:00:00 2001 From: Xeovalyte Date: Sun, 22 Jan 2023 17:37:10 +0100 Subject: [PATCH] fixed firebase admin error --- frontend/server/api/getrelatiecodes.post.js | 5 ++--- frontend/server/api/sendmessage.post.js | 8 +++----- frontend/server/api/subscribetotopic.post.js | 4 ++-- frontend/server/utils/firebase.js | 15 +++++++++------ 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/frontend/server/api/getrelatiecodes.post.js b/frontend/server/api/getrelatiecodes.post.js index 8604bb3..c91b86a 100644 --- a/frontend/server/api/getrelatiecodes.post.js +++ b/frontend/server/api/getrelatiecodes.post.js @@ -1,5 +1,4 @@ -import { db } from '../utils/firebase' -import { getAuth } from 'firebase-admin/auth' +import { db, auth } from '../utils/firebase' export default defineEventHandler(async event => { const { email, token } = await readBody(event); @@ -8,7 +7,7 @@ export default defineEventHandler(async event => { if (!token) throw createError({ statusCode: 400, statusMessage: 'no-token'}) try { - await getAuth().verifyIdToken(token) + await auth.verifyIdToken(token) } catch (e) { console.log(e); throw createError({ statusCode: 500, statusMessage: 'error-verify-id'}) diff --git a/frontend/server/api/sendmessage.post.js b/frontend/server/api/sendmessage.post.js index a45b282..7b6991f 100644 --- a/frontend/server/api/sendmessage.post.js +++ b/frontend/server/api/sendmessage.post.js @@ -1,6 +1,4 @@ -import { db } from '../utils/firebase' -import { getAuth } from 'firebase-admin/auth' -import { getMessaging } from 'firebase-admin/messaging' +import { db, auth, messaging } from '../utils/firebase' export default defineEventHandler(async event => { const { token, body, title, topic } = await readBody(event); @@ -14,7 +12,7 @@ export default defineEventHandler(async event => { let decodedToken = null; try { - decodedToken = await getAuth().verifyIdToken(token) + decodedToken = await auth.verifyIdToken(token) } catch (e) { console.log(e) throw createError({ statusCode: 500, statusMessage: 'error-verify-id'}) @@ -53,7 +51,7 @@ export default defineEventHandler(async event => { } } }; - const response = await getMessaging().send(message) + const response = await messaging.send(message) console.log('Successfully sent message:', response); diff --git a/frontend/server/api/subscribetotopic.post.js b/frontend/server/api/subscribetotopic.post.js index 373c4b8..69acbe2 100644 --- a/frontend/server/api/subscribetotopic.post.js +++ b/frontend/server/api/subscribetotopic.post.js @@ -1,4 +1,4 @@ -import { getMessaging } from 'firebase-admin/messaging' +import { messaging } from '../utils/firebase' export default defineEventHandler(async event => { const { topic, registrationToken } = await readBody(event); @@ -8,7 +8,7 @@ export default defineEventHandler(async event => { if (!registrationToken) throw createError({ statusCode: 400, statusMessage: 'no-registrationtoken'}) try { - await getMessaging().subscribeToTopic([registrationToken], topic) + await messaging.subscribeToTopic([registrationToken], topic) return { code: 'success'} } catch (e) { diff --git a/frontend/server/utils/firebase.js b/frontend/server/utils/firebase.js index fe9a4ed..1f82055 100644 --- a/frontend/server/utils/firebase.js +++ b/frontend/server/utils/firebase.js @@ -1,13 +1,14 @@ -import { createRequire } from 'module'; -const require = createRequire(import.meta.url); -const admin = require("firebase-admin"); - +import { initializeApp, cert } from 'firebase-admin/app'; import { getFirestore } from 'firebase-admin/firestore'; +import { getMessaging } from 'firebase-admin/messaging'; +import { getAuth } from 'firebase-admin/auth' const config = useRuntimeConfig() -export const app = admin.initializeApp({ - credential: admin.credential.cert({ +console.log('testing', config) + +export const app = initializeApp({ + credential: cert({ "project_id": "wrbapp", "private_key_id": config.privateKeyId, "private_key": config.privateKey, @@ -23,3 +24,5 @@ export const app = admin.initializeApp({ export const firestore = getFirestore() export const db = getFirestore(); +export const messaging = getMessaging(); +export const auth = getAuth()