DEV Community

Cover image for How to add Supabase to Nuxt.js
Saunved
Saunved

Posted on

6 1

How to add Supabase to Nuxt.js

Yesterday, I tried Supabase for the first time. Within about 30 minutes, I had social login and password-based authentication set up. There was almost zero friction during the whole process and I was super impressed, especially after my horrible developer experience with AWS Cognito and Amplify Auth.

This is a quick guide on how you can add Supabase to a Nuxt.js application.

Prerequisites

(1) You have a Nuxt.js application created using npx create-nuxt-app or through another method
(2) You have created a Supabase project and the Supabase key, Supabase URL

How to add Supabase to Nuxt

(1) Install Supabase

yarn add @supabase/supabase-js

(2) Add Supabase to plugins
In the plugins folder

  • Create a file supabase.client.js
  • Add the following code snippet (replace the key, url with your own)
import { createClient } from '@supabase/supabase-js'
const SUPABASE_KEY = '<YOUR-KEY>'
export default ({ app }, inject) => {
  const supabaseUrl = '<YOUR-URL>'
  const supabaseKey = SUPABASE_KEY
  const supabase = createClient(supabaseUrl, supabaseKey)

  inject('supabase', supabase)
}
Enter fullscreen mode Exit fullscreen mode

This will inject Supabase to our Nuxt project, and will be available as this.$supabase for us to use

(3) Add the plugins file to nuxt.config.js
In nuxt.config.js, add the following to plugins

// ...
  plugins: [
    '@/plugins/supabase.client.js'
  ],
// ...
Enter fullscreen mode Exit fullscreen mode

You can also use the alternative syntax

// ...
  plugins: [
    { src: '@/plugins/supabase.client.js', ssr: false }
  ],
// ...
Enter fullscreen mode Exit fullscreen mode

(4) Use Supabase on any page

//...
methods: {
  signIn(){
    const { user, error } = await this.$supabase.auth.signIn({
        email: this.email,
        password: this.password
      })
      console.log(user, error)
  }
}
//...
Enter fullscreen mode Exit fullscreen mode

That's it!


I'm really excited to see where @supabase_io goes in the coming weeks and months! I hope it keeps growing and adding more useful features.

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay