fix: pwa, firebase and auth initial load issues

This commit is contained in:
2026-04-28 14:50:13 +05:00
parent 669d8571ae
commit fad9cacd3e
8 changed files with 72 additions and 23 deletions
+4 -1
View File
@@ -14,7 +14,10 @@ export const useAchievements = () => {
const loadAchievements = async () => {
pending.value = true
try {
const data = await $fetch<{ achievements: Achievement[], stats: { total: number, completedTasks: number } }>('/api/achievements')
const auth = useAuth()
const data = await $fetch<{ achievements: Achievement[], stats: { total: number, completedTasks: number } }>('/api/achievements', {
headers: { Authorization: `Bearer ${auth.token.value}` }
})
achievements.value = data.achievements || []
stats.value = data.stats || { total: 0, completedTasks: 0 }
} catch (e) {
+14 -3
View File
@@ -51,10 +51,21 @@ export const useAuth = () => {
const initAuth = () => {
const auth = getAuth()
onAuthStateChanged(auth, async (firebaseUser) => {
if (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()
if (idToken !== token.value) {
token.value = idToken
const res = await $fetch<{ token: string; user: User }>('/api/auth/google', {
method: 'POST',
body: { idToken }
})
token.value = res.token
if (useRoute().path === '/login') {
navigateTo('/')
}
} catch (e) {
console.error('Failed to restore backend auth from Firebase', e)
}
}
})
+2 -2
View File
@@ -87,9 +87,9 @@ const createNewCanvas = () => {
// Redirect to specific canvas if ID is provided
onMounted(() => {
if (auth.user) {
if (auth.isAuthenticated.value) {
// Load user's canvases
console.log('Loading canvases for user:', auth.user.email)
console.log('Loading canvases for user:', auth.user.value?.email)
}
})
</script>
+20 -3
View File
@@ -119,10 +119,19 @@ definePageMeta({
const auth = useAuth()
const user = computed(() => auth.user)
const authFetch = (url, opts = {}) => $fetch(url, {
const authFetch = async (url, opts = {}) => {
try {
return await $fetch(url, {
...opts,
headers: { Authorization: `Bearer ${auth.token.value}`, ...opts.headers }
})
})
} catch (error) {
if (error.response?.status === 401) {
auth.logout()
}
throw error
}
}
const canvases = ref([])
const userGroups = ref([])
@@ -287,5 +296,13 @@ const getGroupName = (groupId) => {
return group?.name || 'Unknown Group'
}
onMounted(loadData)
onMounted(() => {
if (auth.isAuthenticated.value) {
loadData()
}
})
watch(() => auth.isAuthenticated.value, (isAuth) => {
if (isAuth) loadData()
})
</script>
+20 -3
View File
@@ -170,10 +170,19 @@ definePageMeta({
const auth = useAuth()
const route = useRoute()
const authFetch = (url, opts = {}) => $fetch(url, {
const authFetch = async (url, opts = {}) => {
try {
return await $fetch(url, {
...opts,
headers: { Authorization: `Bearer ${auth.token.value}`, ...opts.headers }
})
})
} catch (error) {
if (error.response?.status === 401) {
auth.logout()
}
throw error
}
}
const profileId = computed(() => route.params.id || auth.user.value?.id)
const currentUserId = computed(() => auth.user.value?.id || '')
@@ -329,6 +338,14 @@ const followButtonClass = computed(() => {
: 'bg-blue-600 hover:bg-blue-500 text-white'
})
onMounted(loadProfile)
onMounted(() => {
if (auth.isAuthenticated.value) {
loadProfile()
}
})
watch(() => auth.isAuthenticated.value, (isAuth) => {
if (isAuth) loadProfile()
})
watch(profileId, loadProfile)
</script>
-1
View File
@@ -17,7 +17,6 @@ export default defineNuxtConfig({
pwa: {
registerType: 'autoUpdate',
workbox: {
navigateFallback: '/',
globPatterns: ['**/*.{js,css,html,png,svg,ico}'],
},
client: {
+5 -5
View File
@@ -77,11 +77,11 @@ services:
GITEA_MINDMAP_REPO: ${GITEA_MINDMAP_REPO:-MMAVTEX}
GITEA_PROJECT_REPO: ${GITEA_PROJECT_REPO:-XFlow}
DATABASE_URL: postgresql://xflow:${XFLOW_DB_PASSWORD:-xflow_pass}@postgres:5432/xflow
NUXT_PUBLIC_FIREBASE_API_KEY: ${NUXT_PUBLIC_FIREBASE_API_KEY}
NUXT_PUBLIC_FIREBASE_AUTH_DOMAIN: ${NUXT_PUBLIC_FIREBASE_AUTH_DOMAIN}
NUXT_PUBLIC_FIREBASE_PROJECT_ID: ${NUXT_PUBLIC_FIREBASE_PROJECT_ID}
NUXT_PUBLIC_FIREBASE_APP_ID: ${NUXT_PUBLIC_FIREBASE_APP_ID}
NUXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID: ${NUXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID}
NUXT_PUBLIC_FIREBASE_API_KEY: ${NUXT_PUBLIC_FIREBASE_API_KEY:-${FIREBASE_API_KEY}}
NUXT_PUBLIC_FIREBASE_AUTH_DOMAIN: ${NUXT_PUBLIC_FIREBASE_AUTH_DOMAIN:-${FIREBASE_AUTH_DOMAIN}}
NUXT_PUBLIC_FIREBASE_PROJECT_ID: ${NUXT_PUBLIC_FIREBASE_PROJECT_ID:-${FIREBASE_PROJECT_ID}}
NUXT_PUBLIC_FIREBASE_APP_ID: ${NUXT_PUBLIC_FIREBASE_APP_ID:-${FIREBASE_APP_ID}}
NUXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID: ${NUXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID:-${FIREBASE_MESSAGING_SENDER_ID}}
volumes:
- /opt/xflow/.output:/app/.output
- /opt/xflow/prisma:/app/prisma:ro
+2
View File
@@ -156,3 +156,5 @@
{ "id": "e16", "fromNode": "solution", "fromSide": "right", "toNode": "arch", "toSide": "left" }
]
}