Await Resource

I have an authentication context wrapping the router
export const [SessionProvider, useSession] = createContextProvider(() => {
const cookie = isServer
? useRequest().request.headers.get("cookie") ?? ""
: document.cookie
const [userSession] = createResource(async () => {
const session = await storage.getSession(cookie)
return {
instance: session.get('instance') as string || null,
key: session.get('key') as string || null
}
}, {
deferStream: true
})
return userSession
})
export const [SessionProvider, useSession] = createContextProvider(() => {
const cookie = isServer
? useRequest().request.headers.get("cookie") ?? ""
: document.cookie
const [userSession] = createResource(async () => {
const session = await storage.getSession(cookie)
return {
instance: session.get('instance') as string || null,
key: session.get('key') as string || null
}
}, {
deferStream: true
})
return userSession
})
And later use the session context in certain functions
const gclc = () => createClient({
url: `https://${useSession()().instance}/api/graphql`,
fetchOptions: () => {
return {
headers: {
'Authorization': `Bearer ${useSession()().key}`
}
}
}
})
const gclc = () => createClient({
url: `https://${useSession()().instance}/api/graphql`,
fetchOptions: () => {
return {
headers: {
'Authorization': `Bearer ${useSession()().key}`
}
}
}
})
But it appears that in some contexts, useSession is still unloaded when it is called. I need some way to await the resource ready state if it isn't ready already, or some better way to do this
1 Reply
Ping for toast
Ping for toastOP2y ago
I tried
<SessionProvider>
<Show when={!useSession().loading}>
<Routes>
<FileRoutes />
</Routes>
</Show>
</SessionProvider>
<SessionProvider>
<Show when={!useSession().loading}>
<Routes>
<FileRoutes />
</Routes>
</Show>
</SessionProvider>
but this doesn't work
Want results from more Discord servers?
Add your server