New Backend changes
This commit is contained in:
24
plugins/api.ts
Normal file
24
plugins/api.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
export default defineNuxtPlugin(() => {
|
||||
const api = $fetch.create({
|
||||
baseURL: "http://localhost:3100",
|
||||
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 } }
|
||||
})
|
||||
Reference in New Issue
Block a user