move auth init to +layout.ts load function
init authStore in load function before components mount removes race condition with tanstack query mutations
This commit is contained in:
parent
f29fd065b6
commit
d61908fcb7
2 changed files with 13 additions and 16 deletions
|
|
@ -8,8 +8,7 @@
|
||||||
import { sidebar } from '$lib/stores/sidebar.svelte'
|
import { sidebar } from '$lib/stores/sidebar.svelte'
|
||||||
import { Tooltip } from 'bits-ui'
|
import { Tooltip } from 'bits-ui'
|
||||||
import { beforeNavigate, afterNavigate } from '$app/navigation'
|
import { beforeNavigate, afterNavigate } from '$app/navigation'
|
||||||
import { authStore } from '$lib/stores/auth.store'
|
import { dev } from '$app/environment'
|
||||||
import { browser, dev } from '$app/environment'
|
|
||||||
import { QueryClientProvider } from '@tanstack/svelte-query'
|
import { QueryClientProvider } from '@tanstack/svelte-query'
|
||||||
import { SvelteQueryDevtools } from '@tanstack/svelte-query-devtools'
|
import { SvelteQueryDevtools } from '@tanstack/svelte-query-devtools'
|
||||||
import type { LayoutData } from './$types'
|
import type { LayoutData } from './$types'
|
||||||
|
|
@ -27,19 +26,6 @@
|
||||||
// Store scroll positions for each visited route
|
// Store scroll positions for each visited route
|
||||||
const scrollPositions = new Map<string, number>();
|
const scrollPositions = new Map<string, number>();
|
||||||
|
|
||||||
// Initialize auth store from server data when data prop is populated
|
|
||||||
// Use $effect to ensure we react to when server data is available
|
|
||||||
$effect(() => {
|
|
||||||
if (browser && data?.auth) {
|
|
||||||
console.log('[+layout] Initializing authStore with token:', data.auth.accessToken ? 'present' : 'missing')
|
|
||||||
authStore.initFromServer(
|
|
||||||
data.auth.accessToken,
|
|
||||||
data.auth.user,
|
|
||||||
data.auth.expiresAt
|
|
||||||
)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
// Save scroll position before navigating away and close sidebar
|
// Save scroll position before navigating away and close sidebar
|
||||||
beforeNavigate(({ from }) => {
|
beforeNavigate(({ from }) => {
|
||||||
// Close sidebar when navigating
|
// Close sidebar when navigating
|
||||||
|
|
|
||||||
|
|
@ -11,8 +11,19 @@
|
||||||
import type { LayoutLoad } from './$types'
|
import type { LayoutLoad } from './$types'
|
||||||
import { browser } from '$app/environment'
|
import { browser } from '$app/environment'
|
||||||
import { QueryClient } from '@tanstack/svelte-query'
|
import { QueryClient } from '@tanstack/svelte-query'
|
||||||
|
import { authStore } from '$lib/stores/auth.store'
|
||||||
|
|
||||||
|
export const load: LayoutLoad = async ({ data }) => {
|
||||||
|
// Initialize auth store from server data BEFORE creating QueryClient
|
||||||
|
// This ensures auth is ready when mutations initialize
|
||||||
|
if (browser && data.auth) {
|
||||||
|
authStore.initFromServer(
|
||||||
|
data.auth.accessToken,
|
||||||
|
data.auth.user,
|
||||||
|
data.auth.expiresAt
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
export const load: LayoutLoad = async () => {
|
|
||||||
const queryClient = new QueryClient({
|
const queryClient = new QueryClient({
|
||||||
defaultOptions: {
|
defaultOptions: {
|
||||||
queries: {
|
queries: {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue