SolidJSS
SolidJSโ€ข4y agoโ€ข
1 reply
Ping for toast

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
})
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}`
      }
    }
  }
})

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
Was this page helpful?