Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
JhumanJ committed Dec 15, 2023
1 parent a3a9254 commit fa20945
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 11 deletions.
2 changes: 1 addition & 1 deletion client/middleware/check-auth.global.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export default defineNuxtRouteMiddleware((to, from) => {
const authStore = useAuthStore()
authStore.loadTokenFromCookie()
useAuthStore().fetchUserIfNotFetched()
authStore.fetchUserIfNotFetched()
})
5 changes: 4 additions & 1 deletion client/plugins/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
},
Expand Down
18 changes: 11 additions & 7 deletions client/stores/auth.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {defineStore} from 'pinia'
import axios from 'axios'
import {useOpnFetch} from "~/composables/useOpnFetch.js";

export const useAuthStore = defineStore('auth', {
state: () => {
Expand Down Expand Up @@ -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() {
Expand Down
5 changes: 3 additions & 2 deletions client/stores/workspaces.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
},
Expand Down

0 comments on commit fa20945

Please sign in to comment.