,

Building a Workout Tracker App with Vue 3 & Supabase

Posted by






Vue 3 & Supabase | Workout Tracker App

Vue 3 & Supabase | Workout Tracker App

Introduction

In this article, we will explore how to build a workout tracker app using Vue 3 and Supabase. Vue 3 is the latest version of the popular JavaScript framework, Vue.js, and Supabase is an open-source Backend-as-a-Service (BaaS) alternative to Firebase. By combining these two technologies, we can create a powerful and scalable workout tracking application.

Setting up Vue 3

To get started, make sure you have Vue 3 installed on your machine. You can install Vue 3 by running the following command in your terminal:

npm install -g @vue/cli

Once you have Vue 3 installed, you can create a new Vue project by running the following command:

vue create workout-tracker

This will create a new Vue project with the name “workout-tracker”.

Integrating Supabase

Now that we have our Vue project set up, we can integrate Supabase as our backend. Supabase provides a RESTful API and a real-time database, making it a perfect fit for our workout tracker app.

First, sign up for a free Supabase account and create a new project. Once you have your project set up, you can retrieve your Supabase URL and API key. These will be used to connect to your Supabase backend from your Vue app.

To connect to Supabase from your Vue app, you can use the official Supabase JavaScript library. Install it by running the following command:

npm install @supabase/supabase-js

After installing the library, you can import it in your Vue app and initialize a new Supabase client using your Supabase URL and API key.


import { createClient } from '@supabase/supabase-js';

const supabaseUrl = 'YOUR_SUPABASE_URL';
const supabaseKey = 'YOUR_SUPABASE_API_KEY';
const supabase = createClient(supabaseUrl, supabaseKey);

export default {
  data() {
    return {
      workouts: []
    };
  },
  created() {
    this.fetchWorkouts();
  },
  methods: {
    async fetchWorkouts() {
      const { data: workouts, error } = await supabase
        .from('workouts')
        .select('*');
      
      if (error) {
        console.error(error);
      } else {
        this.workouts = workouts;
      }
    }
  }
};
    

This code fetches the workouts data from the “workouts” table in your Supabase project and assigns it to the “workouts” variable in your Vue component’s data object. You can then use this data to display and manipulate workouts in your app.

Building the Workout Tracker App

With Vue 3 set up and Supabase integrated, you can now start building your workout tracker app. The app will allow users to create, read, update, and delete workouts, and all changes will be synced in real time across devices.

Use Vue’s powerful reactivity system to update the UI whenever the workouts data changes. You can bind the data to your templates and use the built-in directives, such as v-for and v-model, to render and update the workouts.

Additionally, you can use Supabase’s real-time subscriptions to listen for changes in the workouts data. This allows you to instantly update the UI when a new workout is added or an existing workout is modified or deleted.

Conclusion

Vue 3 and Supabase provide a powerful combination for building a workout tracker app. With Vue’s reactivity and Supabase’s real-time database, you can create a seamless and scalable user experience. Whether you are tracking your own workouts or building a fitness app for others, Vue 3 and Supabase are powerful tools to have in your arsenal.

Published on:

Author: John Doe


0 0 votes
Article Rating
20 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
John Komarnicki
1 year ago

Thanks for the opportunity to create this project, Brad. Hopefully you all enjoy!

Maciej Szarłat
1 year ago

Hi can anybody help me to understand the auth flow regarding tokens? I want to use us-abase auth with my backend written in golang. If I login with supabase I'm getting token that is stored in local storage. From the security side It would be better store jwt in http only cookie with samesite. Is there any possibility to implement auth flow in that way? I have checked the documentation but couldn't find anything regarding this 🙁

ore_bear
1 year ago

thank you great tutorial!

If you are working with the latest v2 version of supabase you cant use supabase.auth.user(), insteed you need to use: const { data: { user } } = await supabase.auth.getUser();

setup() {

const appReady = ref(false);

onMounted(async () => {

try {

const { data: { user } } = await supabase.auth.getUser();

if (!user) {

appReady.value = true;

console.log("no user")

}

supabase.auth.onAuthStateChange((session) => {

store.methods.setUser(session);

appReady.value = true;

console.log("user loged in")

});

} catch(error) {

console.error(error);

}

});

return { appReady };

},

Miguel Coria
1 year ago

Finally I got it!! Thanks a lot for the project 🙂 Regards from México.

Enrique Moreno
1 year ago

Great video. Only thing missing was server side validation.

Алекс
1 year ago

Heroku static buildpack doesn't work anymore :((((

Алекс
1 year ago

Thanks for course!
But build markup so borring, would be awesome to include markup and css into boiler plate
And concentrate on work with vue3 and supabase. 1 step in a time.

Tazim Rahbar
1 year ago

In this app any logged in user can edit/delete anyone's exercise.
How we can implement that only user who created the workout that particular user can delete or edit that workout

Yergazy Nurmaganbetov
1 year ago

Thank you so much tor this tutorial. Looking forward to new ones.

Pfod Takem
1 year ago

Thank you very much John. It's really a shame that there are so many things left unexplained though. Don't take it personally, but saying literally what we see you writing on screen isn't really helping. Still I was able to implement authentication on my app, so thanks again.

Man949
1 year ago

Project is pretty good but you are not explaining, you are narrating what you are doing. I hope your next videos will be more like a tutorial

Romario oliveira
1 year ago

You dindn't separete the exercises for each user. How can I make it? 🙁

reza garmabi
1 year ago

Hello everyone and thank you for this tutorial

I have a question, how should we use Supabase for photos and videos? Can anyone guide me?

LeagueOfAI
1 year ago

Hi, I have trouble importing supabase at 52:00 . Can you help me? It says

Failed to resolve import "../supabase/init" from "srcviewsuserRegister.vue". Does the file exist?

I already changed my init.js file to
const supabaseUrl = process.env.VUE_APP_SUPABASE_URL;

const supabaseAnonKey = process.env.VUE_APP_SUPABASE_ANON_KEY;

Sadi Amine
1 year ago

is there any tutorial react with supabase ??

druggero77
1 year ago

Outstanding tutorial! I just finished it and everything deployed as advertised. I'm now starting to redo the site and make it a parts site for my job where we can add machine part information such as part numbers and stock room locations which will make often used parts easily accessible. Thank you for all you do.

Malathi P
1 year ago

Tried a lot, could not install on windows, says error on fund pkgs. Is it not open source?

Get No
1 year ago

I came for superbase, but I watched css all the time :/

Samuel Antwi
1 year ago

For those of you who are concerned about someone able to delete your workout, you have to understand that, the way he built the app, he didn't implement any user centric authorisation or user role level security. This means every user has access to whatever workout you create. However, it is easy to implement user role level security. By doing so, you can only create, read, update, and delete workout that only you have created.

I will do my best to explain how you could achieve this. First of all inside Create.vue file, as a logged in user, try to access the user object that comes from supabase after you have logged in. This user object contains an id that looks something like this '5cd7412c-8859-4369-bdc9-76d6890b3e5q'. Declear a variable let say userId and set it value to the id that comes with the user object as a logged in user. So when you do console.log(userId), you will end up with something like this: userId = '5cd7412c-8859-4369-bdc9-76d6890b3e5q'.

Inside the Create.vue file, find the function that he used to create new workout and add 'user_id' and set it value to the userId that you decleared. This means anytime you create a new workout supabase will add extra column called user_id and it value will be equal to whatever the logged in user.id is.

Now head over to Supabase and change how he set the policy. Instead of (role() = 'authenticated'::text), put this (uid() = user_id). Make sure you do this for read, create, update and delete.
That is it. You have now implemented full authentication and authorization in you app. Nobody will be able to read, update, or delete your workout.

Note that Supabase uses JWT for authentication but you don't have to worry about how to set it up. Everything is set up for you which gives good develper experience. I will finnd time to write an article to demostrate how this could be achieved.

Josué Daniel Flores
1 year ago

Good tutorial!