From fa20945571d631e35a3680fcf9087ef2efff4a6e Mon Sep 17 00:00:00 2001 From: Julien Nahum Date: Fri, 15 Dec 2023 12:32:17 +0100 Subject: [PATCH] WIP --- client/middleware/check-auth.global.js | 2 +- client/plugins/fetch.js | 5 ++++- client/stores/auth.js | 18 +++++++++++------- client/stores/workspaces.js | 5 +++-- 4 files changed, 19 insertions(+), 11 deletions(-) diff --git a/client/middleware/check-auth.global.js b/client/middleware/check-auth.global.js index 77d9f3acc..6998e6579 100644 --- a/client/middleware/check-auth.global.js +++ b/client/middleware/check-auth.global.js @@ -1,5 +1,5 @@ export default defineNuxtRouteMiddleware((to, from) => { const authStore = useAuthStore() authStore.loadTokenFromCookie() - useAuthStore().fetchUserIfNotFetched() + authStore.fetchUserIfNotFetched() }) diff --git a/client/plugins/fetch.js b/client/plugins/fetch.js index 05f2fcc3a..84bffcc9d 100644 --- a/client/plugins/fetch.js +++ b/client/plugins/fetch.js @@ -2,8 +2,9 @@ import { ofetch } from 'ofetch' import {useAuthStore} from "~/stores/auth.js"; function addAuthHeader(request, options) { + console.log('insidecookie', useRequestHeaders('cookie')) const authStore = useAuthStore() - if (authStore.check) { + if (authStore.token) { options.headers = { Authorization: `Bearer ${authStore.token}` } console.log('addidng auth',options) } @@ -24,6 +25,8 @@ export default defineNuxtPlugin((_nuxtApp) => { globalThis.$fetch = ofetch.create({ onRequest ({ request, options }) { // TODO: check that it's our own domain called + console.log(request) + options.headers = { accept: 'application/json', ...options.headers } addAuthHeader(request, options) addPasswordToFormRequest(request) }, diff --git a/client/stores/auth.js b/client/stores/auth.js index 3e446ba8c..48cdf853c 100644 --- a/client/stores/auth.js +++ b/client/stores/auth.js @@ -1,5 +1,6 @@ import {defineStore} from 'pinia' import axios from 'axios' +import {useOpnFetch} from "~/composables/useOpnFetch.js"; export const useAuthStore = defineStore('auth', { state: () => { @@ -41,15 +42,18 @@ export const useAuthStore = defineStore('auth', { }, async fetchUser() { - try { - const {data} = await axios.get('/api/user') - this.user = data + useOpnFetch('/user').then(({data, error}) => { + console.log('fetch user', data,error) + if (error.value) { + console.error('Error fetching user', error.value) + this.setToken(null) + } + + this.user = data.value this.initServiceClients() - return data - } catch (e) { - this.setToken(null) - } + return this.user + }) }, async fetchUserIfNotFetched() { diff --git a/client/stores/workspaces.js b/client/stores/workspaces.js index 2172193da..59a2d1e49 100644 --- a/client/stores/workspaces.js +++ b/client/stores/workspaces.js @@ -71,8 +71,9 @@ export const useWorkspacesStore = defineStore('workspaces', { load() { this.set([]) this.startLoading() - return useOpnFetch(workspaceEndpoint).then((response) => { - this.set(response.data) + return useOpnFetch(workspaceEndpoint,{server: false}).then(({data,error}) => { + console.log(data,error) + this.set(data) this.stopLoading() }) },