24 lines
766 B
TypeScript
24 lines
766 B
TypeScript
export default defineNuxtPlugin(() => {
|
|
const api = $fetch.create({
|
|
baseURL: "http://localhost:3100" /*"https://backend.fedeo.io"*/,
|
|
credentials: "include",
|
|
onRequest({ options }) {
|
|
// Token aus Cookie holen
|
|
let token = useCookie("token").value
|
|
|
|
// Falls im Request explizit ein anderer JWT übergeben wird → diesen verwenden
|
|
if (options.context && (options.context as any).jwt) {
|
|
token = (options.context as any).jwt
|
|
}
|
|
|
|
if (token) {
|
|
options.headers = {
|
|
...options.headers,
|
|
Authorization: `Bearer ${token}`,
|
|
}
|
|
}
|
|
}
|
|
})
|
|
|
|
return { provide: { api } }
|
|
}) |