mobile favicon
Services

serverSupabaseSession

Get your Supabase user's session from serverside with the serverSupabaseUser service
This section assumes you're familiar with Nitro, the server engine powered by Nuxt.

This function is similar to the useSupabaseSession composable but is used in server routes.

Be advised that serverSupabaseSession is considered unsafe, since the session comes from the client and users can tamper with it. For checking if the user is logged in, always use serverSupabaseUser

Define your server route and import the serverSupabaseSession from #supabase/server.

server/api/session.ts
import { serverSupabaseSession } from '#supabase/server'

export default defineEventHandler(async (event) => {
  return await serverSupabaseSession(event)
})

Then call your api route from any vue file:

pages/index.vue
const session = ref(null)

const fetchSession = async () => {
   session.value = await $fetch('/api/session')
}

Be careful, if you want to call this route on SSR, please read this section, you must send your browser cookies including your supabase token.

pages/index.vue
const session = ref(null)

const { data } = await useFetch('/api/session', {
  headers: useRequestHeaders(['cookie'])
})

session.value = data

Made with Nuxt Studio