Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
import { RouterView } from 'vue-router'
import LandingPageHeader from '@/views/pagedecorators/PageHeader.vue'
import LandingPageFooter from '@/views/pagedecorators/PageFooter.vue'
import { onMounted } from 'vue';
import useTokens from '@/stores/auth.store';

const { refreshSession } = useTokens();

onMounted(async () => {
await refreshSession();
});
</script>

<template>
Expand Down
29 changes: 28 additions & 1 deletion src/stores/auth.store.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

import { reactive, toRefs, computed } from 'vue'
import VueCookies from 'vue-cookies'
// Note: apicalls.js also imports useTokens — circular dep is safe here because
// authedFetchJson is only referenced inside function bodies, never at module init time.
import { authedFetchJson } from '@/functions/apicalls'

const apiLocation = import.meta.env.VITE_APP_API_ENDPOINT || 'https://api.neotomadb.org'
const userValidation = `${apiLocation}/v2.0/apps/orcids/validate`
Expand Down Expand Up @@ -140,10 +143,34 @@ export default function useTokens() {
return true
})

const refreshSession = async () => {
// Skip if there's no active session — nothing to verify
if (!state.user?.data?.neotoken?.sessionuuid) return null

try {
const json = await authedFetchJson('/v2.0/apps/orcids/me')
// Update in-memory user state with fresh data from the server
state.user = {
...state.user,
data: {
...state.user.data,
orcid: { name: json.data.name, orcid: json.data.orcid },
contactid: json.data.contactid,
},
}
return json.data
} catch (err) {
// Most likely a 401 — authedFetchJson already cleared the cookie
console.warn('Session check failed; user logged out.', err.message)
return null
}
}

return {
...toRefs(state),
fetchTokens,
logoutTokens,
hasValidTokens
hasValidTokens,
refreshSession,
}
}
6 changes: 5 additions & 1 deletion src/views/UserPage.vue
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<script setup>
import { onMounted, ref, defineAsyncComponent } from 'vue'
import { onMounted, ref, computed, defineAsyncComponent } from 'vue'
import Panel from 'primevue/panel'
import Card from 'primevue/card'
import { useRoute, useRouter } from 'vue-router'
import VueCookies from 'vue-cookies'
import { authedFetch } from '@/functions/apicalls'
import useTokens from '@/stores/auth.store'

const ContactDetails = defineAsyncComponent(() => import('@/views/resources/contactelements/ContactDetails.vue'))
const ContactPublications = defineAsyncComponent(
Expand All @@ -19,6 +20,9 @@ const ContactSearch = defineAsyncComponent(
() => import('@/views/resources/contactelements/ContactSearch.vue')
)

const { user } = useTokens()
const userName = computed(() => user.value?.data?.orcid?.name ?? null)

const route = useRoute()
const router = useRouter()

Expand Down
Loading