Skip to content
This repository has been archived by the owner on Dec 12, 2023. It is now read-only.

feat: Add session to SSR #73

Merged
merged 2 commits into from
Dec 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions src/runtime/composables/useSession.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { useFetch, createError } from '#app'
import { useFetch, createError, useRequestHeaders, useNuxtApp, useRuntimeConfig } from '#app'
import { nanoid } from 'nanoid'
import { Ref, ref } from 'vue'
import type { Session, SupportedSessionApiMethods } from '../../types'
import { useRuntimeConfig } from '#imports'

type SessionData = Record<string, any>

// Key for the session value in the nuxt payload
const SESSION_VALUE_KEY = 'nuxt-session:session-value'

declare interface ComposableOptions {
fetchSessionOnInitialization: boolean
}
Expand All @@ -26,15 +28,19 @@ export default async (options: ComposableOptions = {
throw createError({ message, statusCode: 500 })
}

const nuxt = useNuxtApp()

// Return the fetch so that it is executed in the component context + to allow further introspection by the user if desired
return useFetch(config.api.basePath, {
// Pass the cookie from the current request to the session-api
headers: {
cookie: useRequestHeaders(['cookie']).cookie ?? ''
},

// Method must be capitalized for HTTP-request to work
method: method.toUpperCase(),
body,

// Do not fetch on server, as the cookie is stored and sent by the client
server: false,

// Never cache
key: nanoid(),

Expand All @@ -43,6 +49,12 @@ export default async (options: ComposableOptions = {
const data = response._data

session.value = data

// If we are on the server, store session value in nuxt payload to avoid hydration issues
if (process.server) {
nuxt.payload.state[SESSION_VALUE_KEY] = data
}

return data
}
})
Expand Down Expand Up @@ -75,7 +87,13 @@ export default async (options: ComposableOptions = {

// Initialize session object
if (options.fetchSessionOnInitialization) {
await refresh()
const nuxt = useNuxtApp()

if (nuxt.isHydrating) {
session.value = nuxt.payload.state[SESSION_VALUE_KEY]
} else {
await refresh()
}
}

return {
Expand Down