Vue composables

Learn how to use auto-imported composables inside your vue files.


Checkout the Nuxt3 documentation for auto-imported composables to learn more.

useSupabaseClient

This composable is using supabase-js under the hood, it gives acces to the Supabase client.

The client is initialized with the SUPABASE_KEY you must have in your .env file. It establishes the connection with the database and make use of user JWT to apply RLS Policies implemented in Supabase. If you want to bypass policies, you can use the serverSupabaseServiceRole.

pages/index.vue
<script setup>const client = useSupabaseClient()// Example: client.from('librairies').eq('name', 'Vue').single()</script>

Authentification

All authentification methods are available on Supabase Auth Documentation.

Here is an example of the login using the signIn method with third-party providers.

pages/login.vue
<script setup lang="ts">const user = useSupabaseUser()const client = useSupabaseClient()const router = useRouter()// Login method using providersconst login = async (provider: 'github' | 'google' | 'gitlab' | 'bitbucket') => {  const { error } = await client.auth.signIn({ provider })  if (error) {    return alert('Something went wrong !')  }  router.push('/dashboard')}</script><template>  <button @click="login('github')">Login with GitHub</button></template>
Thanks to the Nuxt plugin, we are listening to the onAuthStateChange listener in order to update the user value according to the received event. We also keep the session consistency between client and server side.

Take a look at the advanced section to learn how to leverage Nuxt middleware to protect your routes for unauthenticated users.

Database Request

Please check Supabase documentation to fully use the power of Supabase client.

Here is an example of a fetch using the select method with Nuxt 3 useAsyncData composable.

<script setup lang="ts">const client = useSupabaseClient()const { data: restaurant } = await useAsyncData('restaurant', async () => {  const { data } = await client.from('restaurants').select('name, location').eq('name', 'My Restaurant Name').single()  return data})</script>

useSupabaseUser

Once logged in, you can access your user everywhere:

<script setup>const user = useSupabaseUser()</script>

Learn how to protect your routes by writing your own auth middleware composable.