Merge stable version before large change #16

Merged
xeovalyte merged 28 commits from dev into main 2023-02-14 15:37:45 +01:00
4 changed files with 16 additions and 16 deletions
Showing only changes of commit 388d989229 - Show all commits

View File

@ -1,5 +1,4 @@
import { db } from '../utils/firebase' import { db, auth } from '../utils/firebase'
import { getAuth } from 'firebase-admin/auth'
export default defineEventHandler(async event => { export default defineEventHandler(async event => {
const { email, token } = await readBody(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'}) if (!token) throw createError({ statusCode: 400, statusMessage: 'no-token'})
try { try {
await getAuth().verifyIdToken(token) await auth.verifyIdToken(token)
} catch (e) { } catch (e) {
console.log(e); console.log(e);
throw createError({ statusCode: 500, statusMessage: 'error-verify-id'}) throw createError({ statusCode: 500, statusMessage: 'error-verify-id'})

View File

@ -1,6 +1,4 @@
import { db } from '../utils/firebase' import { db, auth, messaging } from '../utils/firebase'
import { getAuth } from 'firebase-admin/auth'
import { getMessaging } from 'firebase-admin/messaging'
export default defineEventHandler(async event => { export default defineEventHandler(async event => {
const { token, body, title, topic } = await readBody(event); const { token, body, title, topic } = await readBody(event);
@ -14,7 +12,7 @@ export default defineEventHandler(async event => {
let decodedToken = null; let decodedToken = null;
try { try {
decodedToken = await getAuth().verifyIdToken(token) decodedToken = await auth.verifyIdToken(token)
} catch (e) { } catch (e) {
console.log(e) console.log(e)
throw createError({ statusCode: 500, statusMessage: 'error-verify-id'}) 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); console.log('Successfully sent message:', response);

View File

@ -1,4 +1,4 @@
import { getMessaging } from 'firebase-admin/messaging' import { messaging } from '../utils/firebase'
export default defineEventHandler(async event => { export default defineEventHandler(async event => {
const { topic, registrationToken } = await readBody(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'}) if (!registrationToken) throw createError({ statusCode: 400, statusMessage: 'no-registrationtoken'})
try { try {
await getMessaging().subscribeToTopic([registrationToken], topic) await messaging.subscribeToTopic([registrationToken], topic)
return { code: 'success'} return { code: 'success'}
} catch (e) { } catch (e) {

View File

@ -1,13 +1,14 @@
import { createRequire } from 'module'; import { initializeApp, cert } from 'firebase-admin/app';
const require = createRequire(import.meta.url);
const admin = require("firebase-admin");
import { getFirestore } from 'firebase-admin/firestore'; import { getFirestore } from 'firebase-admin/firestore';
import { getMessaging } from 'firebase-admin/messaging';
import { getAuth } from 'firebase-admin/auth'
const config = useRuntimeConfig() const config = useRuntimeConfig()
export const app = admin.initializeApp({ console.log('testing', config)
credential: admin.credential.cert({
export const app = initializeApp({
credential: cert({
"project_id": "wrbapp", "project_id": "wrbapp",
"private_key_id": config.privateKeyId, "private_key_id": config.privateKeyId,
"private_key": config.privateKey, "private_key": config.privateKey,
@ -23,3 +24,5 @@ export const app = admin.initializeApp({
export const firestore = getFirestore() export const firestore = getFirestore()
export const db = getFirestore(); export const db = getFirestore();
export const messaging = getMessaging();
export const auth = getAuth()