Getting Started

Build a User Management App with Vue 3

This tutorial demonstrates how to build a basic user management app. The app authenticates and identifies the user, stores their profile information in the database, and allows the user to log in, update their profile details, and upload a profile photo. The app uses:

  • Supabase Database - a Postgres database for storing your user data and Row Level Security so data is protected and users can only access their own information.
  • Supabase Auth - users log in through magic links sent to their email (without having to set up passwords).
  • Supabase Storage - users can upload a profile photo.

Supabase User Management example

Project setup

Before we start building we're going to set up our Database and API. This is as simple as starting a new Project in Supabase and then creating a "schema" inside the database.

Create a project

  1. Create a new project in the Supabase Dashboard.
  2. Enter your project details.
  3. Wait for the new database to launch.

Set up the database schema

Now we are going to set up the database schema. We can use the "User Management Starter" quickstart in the SQL Editor, or you can just copy/paste the SQL from below and run it yourself.

  1. Go to the SQL Editor page in the Dashboard.
  2. Click User Management Starter.
  3. Click Run.

_10
supabase link --project-ref <project-id>
_10
# You can get <project-id> from your project's dashboard URL: https://supabase.com/dashboard/project/<project-id>
_10
supabase db pull

Get the API Keys

Now that you've created some database tables, you are ready to insert data using the auto-generated API. We just need to get the Project URL and anon key from the API settings.

  1. Go to the API Settings page in the Dashboard.
  2. Find your Project URL, anon, and service_role keys on this page.

Building the app

Let's start building the Vue 3 app from scratch.

Initialize a Vue 3 app

We can quickly use Vite with Vue 3 Template to initialize an app called supabase-vue-3:


_10
# npm 6.x
_10
npm create vite@latest supabase-vue-3 --template vue
_10
_10
# npm 7+, extra double-dash is needed:
_10
npm create vite@latest supabase-vue-3 -- --template vue
_10
_10
cd supabase-vue-3

Then let's install the only additional dependency: supabase-js


_10
npm install @supabase/supabase-js

And finally we want to save the environment variables in a .env. All we need are the API URL and the anon key that you copied earlier.

.env

_10
VITE_SUPABASE_URL=YOUR_SUPABASE_URL
_10
VITE_SUPABASE_ANON_KEY=YOUR_SUPABASE_ANON_KEY

With the API credentials in place, create an src/supabase.js helper file to initialize the Supabase client. These variables are exposed on the browser, and that's completely fine since we have Row Level Security enabled on our Database.

src/supabase.js

_10
import { createClient } from '@supabase/supabase-js'
_10
_10
const supabaseUrl = import.meta.env.VITE_SUPABASE_URL
_10
const supabaseAnonKey = import.meta.env.VITE_SUPABASE_ANON_KEY
_10
_10
export const supabase = createClient(supabaseUrl, supabaseAnonKey)

Optionally, update src/style.css to style the app.

Set up a login component

Set up an src/components/Auth.vue component to manage logins and sign ups. We'll use Magic Links, so users can sign in with their email without using passwords.

/src/components/Auth.vue

_44
<script setup>
_44
import { ref } from 'vue'
_44
import { supabase } from '../supabase'
_44
_44
const loading = ref(false)
_44
const email = ref('')
_44
_44
const handleLogin = async () => {
_44
try {
_44
loading.value = true
_44
const { error } = await supabase.auth.signInWithOtp({
_44
email: email.value,
_44
})
_44
if (error) throw error
_44
alert('Check your email for the login link!')
_44
} catch (error) {
_44
if (error instanceof Error) {
_44
alert(error.message)
_44
}
_44
} finally {
_44
loading.value = false
_44
}
_44
}
_44
</script>
_44
_44
<template>
_44
<form class="row flex-center flex" @submit.prevent="handleLogin">
_44
<div class="col-6 form-widget">
_44
<h1 class="header">Supabase + Vue 3</h1>
_44
<p class="description">Sign in via magic link with your email below</p>
_44
<div>
_44
<input class="inputField" required type="email" placeholder="Your email" v-model="email" />
_44
</div>
_44
<div>
_44
<input
_44
type="submit"
_44
class="button block"
_44
:value="loading ? 'Loading' : 'Send magic link'"
_44
:disabled="loading"
_44
/>
_44
</div>
_44
</div>
_44
</form>
_44
</template>

Account page

After a user is signed in we can allow them to edit their profile details and manage their account. Create a new src/components/Account.vue component to handle this.

src/components/Account.vue

_106
<script setup>
_106
import { supabase } from '../supabase'
_106
import { onMounted, ref, toRefs } from 'vue'
_106
_106
const props = defineProps(['session'])
_106
const { session } = toRefs(props)
_106
_106
const loading = ref(true)
_106
const username = ref('')
_106
const website = ref('')
_106
const avatar_url = ref('')
_106
_106
onMounted(() => {
_106
getProfile()
_106
})
_106
_106
async function getProfile() {
_106
try {
_106
loading.value = true
_106
const { user } = session.value
_106
_106
const { data, error, status } = await supabase
_106
.from('profiles')
_106
.select(`username, website, avatar_url`)
_106
.eq('id', user.id)
_106
.single()
_106
_106
if (error && status !== 406) throw error
_106
_106
if (data) {
_106
username.value = data.username
_106
website.value = data.website
_106
avatar_url.value = data.avatar_url
_106
}
_106
} catch (error) {
_106
alert(error.message)
_106
} finally {
_106
loading.value = false
_106
}
_106
}
_106
_106
async function updateProfile() {
_106
try {
_106
loading.value = true
_106
const { user } = session.value
_106
_106
const updates = {
_106
id: user.id,
_106
username: username.value,
_106
website: website.value,
_106
avatar_url: avatar_url.value,
_106
updated_at: new Date(),
_106
}
_106
_106
const { error } = await supabase.from('profiles').upsert(updates)
_106
_106
if (error) throw error
_106
} catch (error) {
_106
alert(error.message)
_106
} finally {
_106
loading.value = false
_106
}
_106
}
_106
_106
async function signOut() {
_106
try {
_106
loading.value = true
_106
const { error } = await supabase.auth.signOut()
_106
if (error) throw error
_106
} catch (error) {
_106
alert(error.message)
_106
} finally {
_106
loading.value = false
_106
}
_106
}
_106
</script>
_106
_106
<template>
_106
<form class="form-widget" @submit.prevent="updateProfile">
_106
<div>
_106
<label for="email">Email</label>
_106
<input id="email" type="text" :value="session.user.email" disabled />
_106
</div>
_106
<div>
_106
<label for="username">Name</label>
_106
<input id="username" type="text" v-model="username" />
_106
</div>
_106
<div>
_106
<label for="website">Website</label>
_106
<input id="website" type="url" v-model="website" />
_106
</div>
_106
_106
<div>
_106
<input
_106
type="submit"
_106
class="button primary block"
_106
:value="loading ? 'Loading ...' : 'Update'"
_106
:disabled="loading"
_106
/>
_106
</div>
_106
_106
<div>
_106
<button class="button block" @click="signOut" :disabled="loading">Sign Out</button>
_106
</div>
_106
</form>
_106
</template>

Launch!

Now that we have all the components in place, let's update App.vue:

src/App.vue

_25
<script setup>
_25
import { onMounted, ref } from 'vue'
_25
import Account from './components/Account.vue'
_25
import Auth from './components/Auth.vue'
_25
import { supabase } from './supabase'
_25
_25
const session = ref()
_25
_25
onMounted(() => {
_25
supabase.auth.getSession().then(({ data }) => {
_25
session.value = data.session
_25
})
_25
_25
supabase.auth.onAuthStateChange((_, _session) => {
_25
session.value = _session
_25
})
_25
})
_25
</script>
_25
_25
<template>
_25
<div class="container" style="padding: 50px 0 100px 0">
_25
<Account v-if="session" :session="session" />
_25
<Auth v-else />
_25
</div>
_25
</template>

Once that's done, run this in a terminal window:


_10
npm run dev

And then open the browser to localhost:5173 and you should see the completed app.

Supabase Vue 3

Bonus: Profile photos

Every Supabase project is configured with Storage for managing large files like photos and videos.

Create an upload widget

Create a new src/components/Avatar.vue component that allows users to upload profile photos:

src/components/Avatar.vue

_77
<script setup>
_77
import { ref, toRefs, watch } from 'vue'
_77
import { supabase } from '../supabase'
_77
_77
const prop = defineProps(['path', 'size'])
_77
const { path, size } = toRefs(prop)
_77
_77
const emit = defineEmits(['upload', 'update:path'])
_77
const uploading = ref(false)
_77
const src = ref('')
_77
const files = ref()
_77
_77
const downloadImage = async () => {
_77
try {
_77
const { data, error } = await supabase.storage.from('avatars').download(path.value)
_77
if (error) throw error
_77
src.value = URL.createObjectURL(data)
_77
} catch (error) {
_77
console.error('Error downloading image: ', error.message)
_77
}
_77
}
_77
_77
const uploadAvatar = async (evt) => {
_77
files.value = evt.target.files
_77
try {
_77
uploading.value = true
_77
if (!files.value || files.value.length === 0) {
_77
throw new Error('You must select an image to upload.')
_77
}
_77
_77
const file = files.value[0]
_77
const fileExt = file.name.split('.').pop()
_77
const filePath = `${Math.random()}.${fileExt}`
_77
_77
const { error: uploadError } = await supabase.storage.from('avatars').upload(filePath, file)
_77
_77
if (uploadError) throw uploadError
_77
emit('update:path', filePath)
_77
emit('upload')
_77
} catch (error) {
_77
alert(error.message)
_77
} finally {
_77
uploading.value = false
_77
}
_77
}
_77
_77
watch(path, () => {
_77
if (path.value) downloadImage()
_77
})
_77
</script>
_77
_77
<template>
_77
<div>
_77
<img
_77
v-if="src"
_77
:src="src"
_77
alt="Avatar"
_77
class="avatar image"
_77
:style="{ height: size + 'em', width: size + 'em' }"
_77
/>
_77
<div v-else class="avatar no-image" :style="{ height: size + 'em', width: size + 'em' }" />
_77
_77
<div :style="{ width: size + 'em' }">
_77
<label class="button primary block" for="single">
_77
{{ uploading ? 'Uploading ...' : 'Upload' }}
_77
</label>
_77
<input
_77
style="visibility: hidden; position: absolute"
_77
type="file"
_77
id="single"
_77
accept="image/*"
_77
@change="uploadAvatar"
_77
:disabled="uploading"
_77
/>
_77
</div>
_77
</div>
_77
</template>

Add the new widget

And then we can add the widget to the Account page in src/components/Account.vue:

src/components/Account.vue

_13
<script>
_13
// Import the new component
_13
import Avatar from './Avatar.vue'
_13
</script>
_13
_13
<template>
_13
<form class="form-widget" @submit.prevent="updateProfile">
_13
<!-- Add to body -->
_13
<Avatar v-model:path="avatar_url" @upload="updateProfile" size="10" />
_13
_13
<!-- Other form elements -->
_13
</form>
_13
</template>

At this stage you have a fully functional application!