This commit is contained in:
Julien Nahum
2023-12-14 16:53:05 +01:00
parent 5c4dc2a3d6
commit a3a9254665
24 changed files with 445 additions and 122 deletions

45
client/plugins/fetch.js vendored Normal file
View File

@@ -0,0 +1,45 @@
import { ofetch } from 'ofetch'
import {useAuthStore} from "~/stores/auth.js";
function addAuthHeader(request, options) {
const authStore = useAuthStore()
if (authStore.check) {
options.headers = { Authorization: `Bearer ${authStore.token}` }
console.log('addidng auth',options)
}
}
function addPasswordToFormRequest (request) {
const url = request.url
if (!url || !url.startsWith('/api/forms/')) return
const slug = url.split('/')[3]
const passwordCookie = useCookie('password-' + slug, { maxAge: 60 * 60 * 24 * 30 }) // 30 days
if (slug !== undefined && slug !== '' && passwordCookie.value !== undefined) {
request.headers['form-password'] = passwordCookie.value
}
}
export default defineNuxtPlugin((_nuxtApp) => {
globalThis.$fetch = ofetch.create({
onRequest ({ request, options }) {
// TODO: check that it's our own domain called
addAuthHeader(request, options)
addPasswordToFormRequest(request)
},
onResponseError ({ response }) {
const authStore = useAuthStore()
const { status } = response
if (status === 401 && authStore.check) {
// TODO: check that it's our own domain called
authStore.logout()
useRouter().push({ name: 'login' })
}
if (status >= 500) {
console.error('Request error', status)
}
}
})
})