Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
intermediate save
Browse files Browse the repository at this point in the history
flxdot committed Apr 13, 2024
1 parent 89ec042 commit 93c220d
Showing 4 changed files with 60 additions and 8 deletions.
8 changes: 8 additions & 0 deletions services/frontend/src/api/axios.ts
Original file line number Diff line number Diff line change
@@ -21,4 +21,12 @@ applyAuthTokenInterceptor(carlosApi, {
requestRefresh: refreshToken,
});

export function setToken(token: string | undefined) {
if (token) {
carlosApi.defaults.headers.common.Authorization = `Bearer ${token}`;
return;
}
delete carlosApi.defaults.headers.common.Authorization;
}

export default carlosApi;
32 changes: 32 additions & 0 deletions services/frontend/src/app.vue
Original file line number Diff line number Diff line change
@@ -9,11 +9,43 @@
</template>

<script setup lang="ts">
import {
watch,
} from 'vue';
import {
RouterView,
} from 'vue-router';
import {
useAuth0,
} from '@auth0/auth0-vue';
import PageHeader from './components/main-layout/header.vue';
import PageFooter from './components/main-layout/footer.vue';
import {
useAuthStore,
} from '@/store/auth.ts';
import {
setToken,
} from '@/api/axios.ts';
const authStore = useAuthStore();
const {
isAuthenticated,
isLoading,
getAccessTokenSilently,
} = useAuth0();
watch([
isAuthenticated,
isLoading,
], async () => {
if (isAuthenticated && !isLoading) {
authStore.token = await getAccessTokenSilently();
} else {
authStore.token = undefined;
}
setToken(authStore.token);
});
</script>

26 changes: 19 additions & 7 deletions services/frontend/src/components/main-layout/header.vue
Original file line number Diff line number Diff line change
@@ -139,7 +139,7 @@
<script setup lang="ts">
import {
ref,
computed,
computed, watch,
} from 'vue';
import Menubar from 'primevue/menubar';
import {
@@ -171,15 +171,27 @@ const {
const devicesStore = useDevicesStore();
const menuItems = computed<MenuItem[]>(() => {
if (!isAuthenticated || isLoading) {
return [];
const menuItems = ref<MenuItem[]>([]);
watch([
isAuthenticated,
isLoading,
], ([
newIsAuthenticated,
newIsLoading,
]) => {
if (!newIsLoading && newIsAuthenticated) {
devicesStore.fetchDevicesList().then((devices) => {
menuItems.value = devices.map((device) => {
return {
label: device.displayName,
icon: 'pi pi-desktop',
};
});
});
}
return [];
});
const devices = devicesStore.fetchDevicesList();
const userMenu = ref<InstanceType<typeof PrmMenu> | null>(null);
const avatarMenuItems = ref([
2 changes: 1 addition & 1 deletion services/frontend/src/store/auth.ts
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ import {

export const useAuthStore = defineStore('auth', {
state: () => ({
token: null as string | null,
token: undefined as string | undefined,
}),
});

0 comments on commit 93c220d

Please sign in to comment.