Skip to content

Commit

Permalink
fix(server): fetch user from session
Browse files Browse the repository at this point in the history
  • Loading branch information
larbish committed Dec 28, 2023
1 parent 0693f4c commit 7a3b73c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
6 changes: 5 additions & 1 deletion playground/server/api/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ import { serverSupabaseClient, serverSupabaseUser } from '#supabase/server'

export default defineEventHandler(async (event) => {
const supabase = await serverSupabaseClient(event)
const user = await serverSupabaseUser(event)
if (!supabase) {
throw createError({ statusMessage: 'Supabase client not found' })
}

const user = await serverSupabaseUser(event)
if (!user) {
throw createError({ statusCode: 404, statusMessage: 'User not found' })
}

// const { data, error } = await supabase.from('test').select('*')
const { data, error } = await supabase.from('pushupers')
.select('firstname, lastname, avatar, email')
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/server/services/serverSupabaseUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import { serverSupabaseClient } from '../services/serverSupabaseClient'
export const serverSupabaseUser = async (event: H3Event): Promise<User | null> => {
const client = await serverSupabaseClient(event)

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

event.context._user = error ? null : supabaseUser
event.context._user = session?.user ?? null

return event.context._user
}

0 comments on commit 7a3b73c

Please sign in to comment.