This commit is contained in:
Julien Nahum 2023-12-15 12:32:17 +01:00
parent a3a9254665
commit fa20945571
4 changed files with 19 additions and 11 deletions

View File

@ -1,5 +1,5 @@
export default defineNuxtRouteMiddleware((to, from) => { export default defineNuxtRouteMiddleware((to, from) => {
const authStore = useAuthStore() const authStore = useAuthStore()
authStore.loadTokenFromCookie() authStore.loadTokenFromCookie()
useAuthStore().fetchUserIfNotFetched() authStore.fetchUserIfNotFetched()
}) })

View File

@ -2,8 +2,9 @@ import { ofetch } from 'ofetch'
import {useAuthStore} from "~/stores/auth.js"; import {useAuthStore} from "~/stores/auth.js";
function addAuthHeader(request, options) { function addAuthHeader(request, options) {
console.log('insidecookie', useRequestHeaders('cookie'))
const authStore = useAuthStore() const authStore = useAuthStore()
if (authStore.check) { if (authStore.token) {
options.headers = { Authorization: `Bearer ${authStore.token}` } options.headers = { Authorization: `Bearer ${authStore.token}` }
console.log('addidng auth',options) console.log('addidng auth',options)
} }
@ -24,6 +25,8 @@ export default defineNuxtPlugin((_nuxtApp) => {
globalThis.$fetch = ofetch.create({ globalThis.$fetch = ofetch.create({
onRequest ({ request, options }) { onRequest ({ request, options }) {
// TODO: check that it's our own domain called // TODO: check that it's our own domain called
console.log(request)
options.headers = { accept: 'application/json', ...options.headers }
addAuthHeader(request, options) addAuthHeader(request, options)
addPasswordToFormRequest(request) addPasswordToFormRequest(request)
}, },

18
client/stores/auth.js vendored
View File

@ -1,5 +1,6 @@
import {defineStore} from 'pinia' import {defineStore} from 'pinia'
import axios from 'axios' import axios from 'axios'
import {useOpnFetch} from "~/composables/useOpnFetch.js";
export const useAuthStore = defineStore('auth', { export const useAuthStore = defineStore('auth', {
state: () => { state: () => {
@ -41,15 +42,18 @@ export const useAuthStore = defineStore('auth', {
}, },
async fetchUser() { async fetchUser() {
try { useOpnFetch('/user').then(({data, error}) => {
const {data} = await axios.get('/api/user') console.log('fetch user', data,error)
this.user = data if (error.value) {
this.initServiceClients() console.error('Error fetching user', error.value)
return data
} catch (e) {
this.setToken(null) this.setToken(null)
} }
this.user = data.value
this.initServiceClients()
return this.user
})
}, },
async fetchUserIfNotFetched() { async fetchUserIfNotFetched() {

View File

@ -71,8 +71,9 @@ export const useWorkspacesStore = defineStore('workspaces', {
load() { load() {
this.set([]) this.set([])
this.startLoading() this.startLoading()
return useOpnFetch(workspaceEndpoint).then((response) => { return useOpnFetch(workspaceEndpoint,{server: false}).then(({data,error}) => {
this.set(response.data) console.log(data,error)
this.set(data)
this.stopLoading() this.stopLoading()
}) })
}, },