fix: update Firebase initialization and runtime config for environment variables

This commit is contained in:
Frank John Begornia
2025-11-07 23:42:09 +08:00
parent 50f08f8177
commit 02b85eefc7
3 changed files with 41 additions and 27 deletions

View File

@@ -1,14 +1,16 @@
<script setup lang="ts"> <script setup lang="ts">
const { signInWithEmail, signInWithGoogle, error, isLoading } = useAuth() const { signInWithEmail, signInWithGoogle, error } = useAuth()
const isOpen = defineModel<boolean>() const isOpen = defineModel<boolean>()
const email = ref('') const email = ref('')
const password = ref('') const password = ref('')
const loginError = ref<string | null>(null) const loginError = ref<string | null>(null)
const isSubmitting = ref(false)
const handleEmailLogin = async () => { const handleEmailLogin = async () => {
try { try {
loginError.value = null loginError.value = null
isSubmitting.value = true
await signInWithEmail(email.value, password.value) await signInWithEmail(email.value, password.value)
isOpen.value = false isOpen.value = false
// Reset form // Reset form
@@ -16,24 +18,30 @@ const handleEmailLogin = async () => {
password.value = '' password.value = ''
} catch (err) { } catch (err) {
loginError.value = 'Login failed. Please check your credentials.' loginError.value = 'Login failed. Please check your credentials.'
} finally {
isSubmitting.value = false
} }
} }
const handleGoogleLogin = async () => { const handleGoogleLogin = async () => {
try { try {
loginError.value = null loginError.value = null
isSubmitting.value = true
await signInWithGoogle() await signInWithGoogle()
isOpen.value = false isOpen.value = false
} catch (err) { } catch (err) {
loginError.value = 'Google login failed. Please try again.' loginError.value = 'Google login failed. Please try again.'
} finally {
isSubmitting.value = false
} }
} }
</script> </script>
<template> <template>
<Teleport to="body">
<div <div
v-if="isOpen" v-if="isOpen"
class="fixed inset-0 z-50 flex items-center justify-center bg-black bg-opacity-50" class="fixed left-0 top-0 z-50 flex min-h-screen w-full items-center justify-center bg-black/60 px-4"
@click.self="isOpen = false" @click.self="isOpen = false"
> >
<div class="w-full max-w-md rounded-lg bg-slate-900 p-6 shadow-xl"> <div class="w-full max-w-md rounded-lg bg-slate-900 p-6 shadow-xl">
@@ -62,10 +70,10 @@ const handleGoogleLogin = async () => {
<button <button
type="submit" type="submit"
:disabled="isLoading" :disabled="isSubmitting"
class="w-full rounded-md bg-sky-600 px-4 py-2 text-white hover:bg-sky-700 disabled:opacity-50" class="w-full rounded-md bg-sky-600 px-4 py-2 text-white hover:bg-sky-700 disabled:opacity-50"
> >
{{ isLoading ? 'Signing in...' : 'Sign In' }} {{ isSubmitting ? 'Signing in...' : 'Sign In' }}
</button> </button>
</form> </form>
@@ -77,7 +85,7 @@ const handleGoogleLogin = async () => {
<button <button
@click="handleGoogleLogin" @click="handleGoogleLogin"
:disabled="isLoading" :disabled="isSubmitting"
class="w-full rounded-md border border-slate-700 bg-slate-800 px-4 py-2 text-white hover:bg-slate-700 disabled:opacity-50" class="w-full rounded-md border border-slate-700 bg-slate-800 px-4 py-2 text-white hover:bg-slate-700 disabled:opacity-50"
> >
Sign in with Google Sign in with Google
@@ -95,4 +103,5 @@ const handleGoogleLogin = async () => {
</button> </button>
</div> </div>
</div> </div>
</Teleport>
</template> </template>

View File

@@ -17,17 +17,21 @@ export const useAuth = () => {
const config = useRuntimeConfig() const config = useRuntimeConfig()
// Initialize Firebase if not already initialized // Initialize Firebase if not already initialized
const initializeFirebase = () => { const initializeFirebase = async () => {
if (process.client && getApps().length === 0) { if (process.client && getApps().length === 0) {
console.log('Initializing Firebase with config:')
const firebaseConfig = { const firebaseConfig = {
apiKey: config.public.firebaseApiKey, apiKey: config.public.firebaseApiKey,
authDomain: config.public.firebaseAuthDomain, authDomain: config.public.firebaseAuthDomain,
projectId: config.public.firebaseProjectId, projectId: config.public.firebaseProjectId,
storageBucket: config.public.firebaseStorageBucket, storageBucket: config.public.firebaseStorageBucket,
messagingSenderId: config.public.firebaseMessagingSenderId, messagingSenderId: config.public.firebaseMessagingSenderId,
appId: config.public.firebaseAppId appId: config.public.firebaseAppId,
...(config.public.firebaseMeasurementId
? { measurementId: config.public.firebaseMeasurementId }
: {})
} }
initializeApp(firebaseConfig) await initializeApp(firebaseConfig)
} }
} }

View File

@@ -10,13 +10,14 @@ export default defineNuxtConfig({
}, },
runtimeConfig: { runtimeConfig: {
public: { public: {
firebaseApiKey: process.env.FIREBASE_API_KEY, firebaseApiKey: process.env.NUXT_PUBLIC_FIREBASE_API_KEY,
firebaseAuthDomain: process.env.FIREBASE_AUTH_DOMAIN, firebaseAuthDomain: process.env.NUXT_PUBLIC_FIREBASE_AUTH_DOMAIN,
firebaseProjectId: process.env.FIREBASE_PROJECT_ID, firebaseProjectId: process.env.NUXT_PUBLIC_FIREBASE_PROJECT_ID,
firebaseStorageBucket: process.env.FIREBASE_STORAGE_BUCKET, firebaseStorageBucket: process.env.NUXT_PUBLIC_FIREBASE_STORAGE_BUCKET,
firebaseMessagingSenderId: process.env.FIREBASE_MESSAGING_SENDER_ID, firebaseMessagingSenderId: process.env.NUXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID,
firebaseAppId: process.env.FIREBASE_APP_ID, firebaseAppId: process.env.NUXT_PUBLIC_FIREBASE_APP_ID,
backendUrl: process.env.BACKEND_URL || 'http://localhost:3000' firebaseMeasurementId: process.env.NUXT_PUBLIC_FIREBASE_MEASUREMENT_ID,
backendUrl: process.env.NUXT_PUBLIC_BACKEND_URL || 'http://localhost:3000'
} }
} }
}); });