diff --git a/server/api/google-wallet.ts b/server/api/google-wallet.ts index e0862940e046782c3dac2f90dc8a3d8f9a1fc466..8d71b2a52195b8ebbcc8e7ac6a29496dae729f9d 100644 --- a/server/api/google-wallet.ts +++ b/server/api/google-wallet.ts @@ -1,3 +1,5 @@ +import { readFileSync } from 'fs'; + import { GoogleAuth } from 'google-auth-library'; import { defineEventHandler, readBody } from 'h3'; import jwt from 'jsonwebtoken'; @@ -8,7 +10,13 @@ const classId = `${issuerId}.codelab_class`; const init = () => { const config = useRuntimeConfig(); - const credentials = config.googleApplicationCredentialsJson; + const credentialsPath = config.googleApplicationCredentialsJson; + + if (typeof credentialsPath !== 'string' || !credentialsPath) + throw new Error('Missing Google Application Credentials path'); + + const credentialsFile = readFileSync(credentialsPath, 'utf-8'); + const credentials = JSON.parse(credentialsFile); if (typeof credentials !== 'object') throw new Error('Invalid Google Application Credentials'); diff --git a/src/components/wallet/AddToGoogleWallet.vue b/src/components/wallet/AddToGoogleWallet.vue index 44355b57e90710877e027d2843b0288b634444d1..efcd80cc9d5f0ad8216663a1a1cf51d9cc278c64 100644 --- a/src/components/wallet/AddToGoogleWallet.vue +++ b/src/components/wallet/AddToGoogleWallet.vue @@ -16,22 +16,19 @@ const addToGoogleWallet = async () => { try { const { operationalKey, name } = await tokensStore.getCurrentUserMetadata(); const baseUrl = window.location.origin; - const res = await fetch('/api/google-wallet', { + const data = await $fetch<{ url?: string; error?: string; message?: string }>('/api/google-wallet', { method: 'POST', - - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ + body: { operationalPublicKey: operationalKey, displayName: name || 'User', baseUrl - }) + } }); - const data = await res.text(); - const parsed = JSON.parse(data); - if (parsed.url && !parsed.error) - window.open(parsed.url, '_blank'); + + if (data.url && !data.error) + window.open(data.url, '_blank'); else - toastError('Failed to generate Google Wallet pass', new Error(parsed.message || data, { cause: parsed })); + toastError('Failed to generate Google Wallet pass', new Error(data.message || JSON.stringify(data), { cause: data })); } catch (error) { toastError('Error generating Google Wallet pass', error); } finally {