Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

vue3-ts-todo: make user sessions persistent upon refresh

ykdojo9f66c1535d28
diff --git a/examples/vue3-ts-todo-list/src/App.vue b/examples/vue3-ts-todo-list/src/App.vue
index afa7fbacde5..b458c0aceef 100644
--- a/examples/vue3-ts-todo-list/src/App.vue
+++ b/examples/vue3-ts-todo-list/src/App.vue
@@ -40,6 +40,19 @@ export default {
Footer,
},
setup() {
+ // Get the auth token in the local storage.
+ const json = localStorage.getItem('supabase.auth.token')
+ if (json) {
+ const data = JSON.parse(json)
+ const { currentSession, expiresAt } = data
+ const timeNow = Math.round(Date.now() / 1000)
+ // If it hasn't expired yet, update userSession.
+ if (timeNow < expiresAt) {
+ userSession.value = currentSession
+ }
+ }
+ // NOTE: The pattern above is similar to that of the _recoverSession() function
+ // in supabase/gotrue-js: https://github.com/supabase/gotrue-js/blob/dc6cf10dcac016ba4831efdb9b8683bda109dab0/src/GoTrueClient.ts#L328
return { userSession, handleLogout }
},
}
diff --git a/examples/vue3-ts-todo-list/src/vuetils/useTodo.ts b/examples/vue3-ts-todo-list/src/vuetils/useTodo.ts
index 67f20acab7d..979b077659a 100644
--- a/examples/vue3-ts-todo-list/src/vuetils/useTodo.ts
+++ b/examples/vue3-ts-todo-list/src/vuetils/useTodo.ts
@@ -9,6 +9,10 @@ const allTodos = ref<Todo[]>([])
*/
async function fetchTodos() {
try {
+ // Wait for the supabase client to be fully initialized
+ // before making the following query. Without this line,
+ // this query would return an empty array.
+ await supabase
const { data: todos, error } = await supabase
.from('todos')
.select('*')