Skip to content

Commit

Permalink
fix: don't ask logged in user for guest username
Browse files Browse the repository at this point in the history
Signed-off-by: Elizabeth Danzberger <[email protected]>
  • Loading branch information
elzody committed Apr 25, 2024
1 parent 200bb72 commit 5271247
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
30 changes: 30 additions & 0 deletions src/helpers/getLoggedInUser.js
Original file line number Diff line number Diff line change
@@ -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
6 changes: 4 additions & 2 deletions src/helpers/guestName.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
*/

import { getCurrentUser } from '@nextcloud/auth'
import getLoggedInUser from '../helpers/getLoggedInUser.js'

const cookieAlreadySet = (cookieName) => {
return document.cookie
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/view/Office.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit 5271247

Please sign in to comment.