I just unified a useSupabase composable to remove duplicate code and to have access to the supabase context.
But I get an error when I import the composable into the server/api folder:
message "useSupabase is not defined"
// composables/useSupabase.js
import { createClient } from '@supabase/supabase-js'
const useSupabase = () => {
const supabase = createClient(SUPABASE_URL, SUPABASE_KEY)
return {
supabase
}
}
export default useSupabase
// server/api/login.js
export default async (event) => {
const body = await useBody(event)
const { supabase } = useSupabase()
const { user } = await supabase.auth.signUp({
email: body.email,
password: body.password
})
return user
}
Is there a way to import the composable into the endpoint?
// server/api/logout.js
import { supabase } from '~/composables/useSupabase.js'
export default async () => {
let { error } = supabase.auth.signOut()
return error
}
// server/api/logout.js
import useSupabase from '~/composables/useSupabase.js'
export default async () => {
const supabase = useSupabase()
let { error } = supabase.auth.signOut()
return error
}
Error 500 "Cannot read property 'signOut' of undefined"
It is actually not Supabase related, it is Nuxt 3 currently doesn't support auto imports within the server directory, as Nuxt docs mentioned it here https://v3.nuxtjs.org/guide/concepts/auto-imports