fix(pwa,auth): serve manifest and icons from apps/web/public and prevent Firebase uninitialized crash
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { jwtDecode } from 'jwt-decode'
|
||||
import { getApps } from 'firebase/app'
|
||||
import { getAuth, signInWithPopup, GoogleAuthProvider, signOut, onAuthStateChanged } from 'firebase/auth'
|
||||
|
||||
interface User {
|
||||
@@ -15,8 +16,6 @@ export const useAuth = () => {
|
||||
sameSite: 'lax'
|
||||
})
|
||||
|
||||
const isInitializing = useState('auth_initializing', () => true)
|
||||
|
||||
const user = computed<User | null>(() => {
|
||||
if (!token.value) return null
|
||||
try {
|
||||
@@ -30,6 +29,10 @@ export const useAuth = () => {
|
||||
const isAdmin = computed(() => user.value?.role === 'admin')
|
||||
|
||||
const googleSignIn = async () => {
|
||||
if (!getApps().length) {
|
||||
alert('Ошибка: Firebase не настроен. Проверьте переменные окружения FIREBASE_API_KEY в Coolify.')
|
||||
return
|
||||
}
|
||||
const auth = getAuth()
|
||||
const provider = new GoogleAuthProvider()
|
||||
const result = await signInWithPopup(auth, provider)
|
||||
@@ -46,8 +49,10 @@ export const useAuth = () => {
|
||||
|
||||
const logout = async () => {
|
||||
try {
|
||||
const auth = getAuth()
|
||||
await signOut(auth)
|
||||
if (getApps().length) {
|
||||
const auth = getAuth()
|
||||
await signOut(auth)
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn('[Firebase] Sign out failed or not initialized', e)
|
||||
} finally {
|
||||
@@ -58,37 +63,44 @@ export const useAuth = () => {
|
||||
|
||||
const initAuth = () => {
|
||||
if (process.server) return
|
||||
|
||||
const auth = getAuth()
|
||||
const unsubscribe = onAuthStateChanged(auth, async (firebaseUser) => {
|
||||
// If user is authenticated in Firebase but we lost our backend token, try to restore
|
||||
if (firebaseUser && !token.value) {
|
||||
try {
|
||||
const idToken = await firebaseUser.getIdToken()
|
||||
const res = await $fetch<{ token: string; user: User }>('/api/auth/google', {
|
||||
method: 'POST',
|
||||
body: { idToken }
|
||||
})
|
||||
token.value = res.token
|
||||
|
||||
if (useRoute().path === '/login') {
|
||||
await navigateTo('/')
|
||||
if (!getApps().length) {
|
||||
console.warn('[Firebase] Firebase App not initialized, skipping onAuthStateChanged.')
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
const auth = getAuth()
|
||||
const unsubscribe = onAuthStateChanged(auth, async (firebaseUser) => {
|
||||
// If user is authenticated in Firebase but we lost our backend token, try to restore
|
||||
if (firebaseUser && !token.value) {
|
||||
try {
|
||||
const idToken = await firebaseUser.getIdToken()
|
||||
const res = await $fetch<{ token: string; user: User }>('/api/auth/google', {
|
||||
method: 'POST',
|
||||
body: { idToken }
|
||||
})
|
||||
token.value = res.token
|
||||
|
||||
if (useRoute().path === '/login') {
|
||||
await navigateTo('/')
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('Failed to restore backend auth from Firebase', e)
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('Failed to restore backend auth from Firebase', e)
|
||||
}
|
||||
}
|
||||
isInitializing.value = false
|
||||
})
|
||||
return unsubscribe
|
||||
})
|
||||
|
||||
onUnmounted(() => unsubscribe())
|
||||
} catch (err) {
|
||||
console.warn('[Firebase] Auth initialization skipped:', err)
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
token,
|
||||
user,
|
||||
token,
|
||||
isAuthenticated,
|
||||
isAdmin,
|
||||
isInitializing,
|
||||
googleSignIn,
|
||||
logout,
|
||||
initAuth
|
||||
|
||||
Reference in New Issue
Block a user