Skip to content

Commit

Permalink
fix(session): ssr getSession warning (#418)
Browse files Browse the repository at this point in the history
  • Loading branch information
th1m0 authored Oct 11, 2024
1 parent ab9a83a commit 95d905b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/runtime/composables/useSupabaseSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ import { useState, type Ref } from '#imports'
* Reactive `Session` state from Supabase. This is initialized in both client and server plugin
* and, on the client, also updated through `onAuthStateChange` events.
*/
export const useSupabaseSession = (): Ref<Session | null> => useState<Session | null>('supabase_session', () => null)
export const useSupabaseSession = (): Ref<Omit<Session, 'user'> | null> => useState<Omit<Session, 'user'> | null>('supabase_session', () => null)
15 changes: 8 additions & 7 deletions src/runtime/plugins/supabase.server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createServerClient, parseCookieHeader } from '@supabase/ssr'
import { getHeader, setCookie } from 'h3'
import { fetchWithRetry } from '../utils/fetch-retry'
import { serverSupabaseUser, serverSupabaseSession } from '../server/services'
import { defineNuxtPlugin, useRequestEvent, useRuntimeConfig, useSupabaseSession, useSupabaseUser } from '#imports'
import type { CookieOptions } from '#app'

Expand Down Expand Up @@ -33,13 +34,13 @@ export default defineNuxtPlugin({

// Initialize user and session states
const [
{
data: { session },
},
{
data: { user },
},
] = await Promise.all([client.auth.getSession(), client.auth.getUser()])
session,
user,
] = await Promise.all([
serverSupabaseSession(event).catch(() => null),
serverSupabaseUser(event).catch(() => null),
])

useSupabaseSession().value = session
useSupabaseUser().value = user

Expand Down
4 changes: 3 additions & 1 deletion src/runtime/server/services/serverSupabaseSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ import type { H3Event } from 'h3'
import { createError } from 'h3'
import { serverSupabaseClient } from '../services/serverSupabaseClient'

export const serverSupabaseSession = async (event: H3Event): Promise<Session | null> => {
export const serverSupabaseSession = async (event: H3Event): Promise<Omit<Session, 'user'> | null> => {
const client = await serverSupabaseClient(event)

const { data: { session }, error } = await client.auth.getSession()
if (error) {
throw createError({ statusMessage: error?.message })
}

// @ts-expect-error we need to delete user from the session object here to suppress the warning coming from GoTrueClient
delete session?.user
return session
}

0 comments on commit 95d905b

Please sign in to comment.