diff --git a/src/helpers/getLoggedInUser.js b/src/helpers/getLoggedInUser.js new file mode 100644 index 0000000000..159ac65b48 --- /dev/null +++ b/src/helpers/getLoggedInUser.js @@ -0,0 +1,30 @@ +import axios from '@nextcloud/axios' +import { generateUrl } from '@nextcloud/router' + +/** + * Gets the current user's display name if logged in. + * + * @return null | string + */ +async function getLoggedInUser() { + try { + const res = await axios.get(generateUrl('apps/files/public')) + + if (res.status !== 200) { + return null + } + + const el = document.createElement('html') + el.innerHTML = res.data + + const userAttributes = el.querySelector('head[data-user]').attributes + + return userAttributes.getNamedItem('data-user-displayname').textContent + } catch (err) { + if (err.status === 401) { + console.error(err.statusText) + } + } +} + +export default getLoggedInUser diff --git a/src/helpers/guestName.js b/src/helpers/guestName.js index 59ee0a609d..3fa6d5ae9e 100644 --- a/src/helpers/guestName.js +++ b/src/helpers/guestName.js @@ -21,6 +21,7 @@ */ import { getCurrentUser } from '@nextcloud/auth' +import getLoggedInUser from '../helpers/getLoggedInUser.js' const cookieAlreadySet = (cookieName) => { return document.cookie @@ -36,11 +37,12 @@ const setGuestName = function(username) { } } -const shouldAskForGuestName = () => { +const shouldAskForGuestName = async () => { + const loggedInUser = Boolean(await getLoggedInUser()) const noGuestCookie = !cookieAlreadySet('guestUser') const noCurrentUser = !getCurrentUser() || getCurrentUser()?.uid === '' - return noCurrentUser && noGuestCookie + return !loggedInUser && noGuestCookie && noCurrentUser } export { diff --git a/src/view/Office.vue b/src/view/Office.vue index 7d9676be87..c853683d38 100644 --- a/src/view/Office.vue +++ b/src/view/Office.vue @@ -282,7 +282,7 @@ export default { } this.postMessage.registerPostMessageHandler(this.postMessageHandler) - if (shouldAskForGuestName()) { + if (await shouldAskForGuestName()) { const { default: GuestNamePicker } = await import( /* webpackChunkName: 'GuestNamePicker' */ '../components/GuestNamePicker.vue')