Fix ref for watch

This commit is contained in:
René Preuß
2023-04-08 12:16:01 +02:00
parent 81b48ac806
commit 29915ebd3b
5 changed files with 124 additions and 5 deletions

View File

@@ -1,5 +1,5 @@
<script setup lang="ts">
import {useAuth} from "#imports";
import {useAuth, useNuxtApp} from "#imports";
const {user, signOut} = await useAuth();
@@ -7,6 +7,15 @@ definePageMeta({
middleware: ["auth"]
})
const { $api } = useNuxtApp()
$api.get('users/@me')
.then((response: any) => {
console.log(response.data)
})
.catch((error: any) => {
console.log(error)
})
</script>
<template>

View File

@@ -0,0 +1,28 @@
import axios from "axios";
import {useAuth} from "#imports";
import {defineNuxtPlugin} from '#app';
import {watch} from 'vue';
export default defineNuxtPlugin(async () => {
const {bearerToken, accessToken} = await useAuth();
const api = axios.create({
baseURL: 'https://id.stream.tv/api/',
headers: {
common: {
'Authorization': bearerToken(),
},
},
});
watch(accessToken, () => {
console.log('access token rotated')
api.defaults.headers.common['Authorization'] = bearerToken();
});
return {
provide: {
api: api,
},
};
});