diff options
author | dartcafe <github@dartcafe.de> | 2022-06-26 19:25:42 +0300 |
---|---|---|
committer | dartcafe <github@dartcafe.de> | 2022-06-26 19:25:42 +0300 |
commit | 96493d7026906d06cb09dfed1c131cc20b4d514e (patch) | |
tree | 0612c49dc16cf408c38ac7128fe0a88e031ed07d /src | |
parent | e4fe40d48a83e05abbc8800b655865199a28fae6 (diff) |
logout from public pollenh/logout-from-public-poll
Signed-off-by: dartcafe <github@dartcafe.de>
Diffstat (limited to 'src')
-rw-r--r-- | src/js/components/User/UserMenu.vue | 22 | ||||
-rw-r--r-- | src/js/helpers/cookieHelper.js | 60 | ||||
-rw-r--r-- | src/js/router.js | 6 |
3 files changed, 67 insertions, 21 deletions
diff --git a/src/js/components/User/UserMenu.vue b/src/js/components/User/UserMenu.vue index dad7a9fd..22b2d0b6 100644 --- a/src/js/components/User/UserMenu.vue +++ b/src/js/components/User/UserMenu.vue @@ -28,7 +28,7 @@ <ActionButton v-if="$route.name === 'publicVote'" icon="icon-md-link" @click="copyLink()"> {{ t('polls', 'Copy your personal link to clipboard') }} </ActionButton> - <ActionSeparator /> + <ActionSeparator v-if="$route.name === 'publicVote'" /> <ActionInput v-if="$route.name === 'publicVote'" :class="check.status" :value="emailAddressTemp" @@ -75,6 +75,12 @@ </template> {{ t('polls', 'Reset your votes') }} </ActionButton> + <ActionButton v-if="$route.name === 'publicVote' && hasCookie" @click="logout()"> + <template #icon> + <LogoutIcon /> + </template> + {{ t('polls', 'Logout as {name} (delete cookie)', { name: acl.displayName }) }} + </ActionButton> </Actions> </template> @@ -91,6 +97,8 @@ import SendLinkPerEmailIcon from 'vue-material-design-icons/LinkVariant.vue' import DeleteIcon from 'vue-material-design-icons/Delete.vue' import ClippyIcon from 'vue-material-design-icons/ClipboardArrowLeftOutline.vue' import ResetVotesIcon from 'vue-material-design-icons/Undo.vue' +import LogoutIcon from 'vue-material-design-icons/Logout.vue' +import { deleteCookieByValue, findCookieByValue } from '../../helpers/cookieHelper.js' export default { name: 'UserMenu', @@ -103,6 +111,7 @@ export default { ActionSeparator, SettingsIcon, EditEmailIcon, + LogoutIcon, SendLinkPerEmailIcon, DeleteIcon, ClippyIcon, @@ -126,6 +135,10 @@ export default { emailAddress: (state) => state.share.emailAddress, }), + hasCookie() { + return !!findCookieByValue(this.$route.params.token) + }, + emailAddressUnchanged() { return this.emailAddress === this.emailAddressTemp }, @@ -171,6 +184,13 @@ export default { }, methods: { + logout() { + const reRouteTo = deleteCookieByValue(this.$route.params.token) + if (reRouteTo) { + this.$router.push({ name: 'publicVote', params: { token: reRouteTo } }) + } + }, + async toggleSubscription() { await this.$store.dispatch('subscription/update', !this.subscribed) }, diff --git a/src/js/helpers/cookieHelper.js b/src/js/helpers/cookieHelper.js index 1036ba65..28ebb266 100644 --- a/src/js/helpers/cookieHelper.js +++ b/src/js/helpers/cookieHelper.js @@ -26,29 +26,55 @@ * @param {string} cookieValue Cookie value * @param {number} cookieExpiration expiration from now in seconds */ -const setCookie = (cookieName, cookieValue, cookieExpiration) => { - const currentTime = new Date() - currentTime.setTime(currentTime.getTime() + cookieExpiration) - const cookieExpiry = `expires=${currentTime.toUTCString()}` - document.cookie = `${cookieName}=${cookieValue};${cookieExpiry};path=/` +const setCookie = (cookieName, cookieValue = '', cookieExpiration = 360) => { + const expirationTime = (new Date()) + expirationTime.setTime(expirationTime.getTime() + cookieExpiration) + document.cookie = `${cookieName}=${cookieValue};expires=${expirationTime.toUTCString()};path=/` } /** * @param {string} cookieName Cookie name to read + * @return {string} Cookie string ('name=value') */ -const getCookie = (cookieName) => { - const name = `${cookieName}=` - const cookiesArray = decodeURIComponent(document.cookie).split(';') - for (let i = 0; i < cookiesArray.length; i++) { - let cookie = cookiesArray[i] - while (cookie.charAt(0) === ' ') { - cookie = cookie.substring(1) - } - if (cookie.indexOf(name) === 0) { - return cookie.substring(name.length, cookie.length) - } +const findCookie = (cookieName) => document.cookie.split(';').find((cookie) => cookie.split('=')[0] === cookieName) + +/** + * @param {string} searchValue Cookie value to search for + * @return {string} Cookie string ('name=value') + */ +const findCookieByValue = (searchValue) => document.cookie.split(';').find((cookie) => cookie.split('=')[1] === searchValue) + +/** + * @param {string} cookieName Cookie name to delete + */ +const deleteCookie = (cookieName) => { + setCookie(cookieName, 'deleted', -100) +} + +/** + * Shortcut to retrieve the cookie value directly or an empty strin, if not found + * + * @param {string} cookieName Cookie name to read + * @return {string} Value of the found cookie + */ +const getCookieValue = (cookieName) => { + const cookie = findCookie(cookieName) + if (cookie) { + return cookie.split('=')[1] } return '' } -export { getCookie, setCookie } +/** + * @param {string} searchValue Cookie value to search for + */ +const deleteCookieByValue = (searchValue) => { + const [cookieName, cookieValue] = findCookieByValue(searchValue).split('=') + + if (cookieValue === searchValue) { + deleteCookie(cookieName) + return cookieName + } +} + +export { getCookieValue, findCookie, setCookie, deleteCookie, deleteCookieByValue, findCookieByValue } diff --git a/src/js/router.js b/src/js/router.js index 1ddf9473..e5aa2352 100644 --- a/src/js/router.js +++ b/src/js/router.js @@ -25,7 +25,7 @@ import Router from 'vue-router' import axios from '@nextcloud/axios' import { getCurrentUser } from '@nextcloud/auth' import { generateUrl } from '@nextcloud/router' -import { getCookie, setCookie } from './helpers/cookieHelper.js' +import { getCookieValue, setCookie } from './helpers/cookieHelper.js' // Dynamic loading const List = () => import('./views/PollList.vue') @@ -61,9 +61,9 @@ async function validateToken(to, from, next) { } else { - const privateToken = getCookie(to.params.token) + const privateToken = getCookieValue(to.params.token) - if (privateToken && to.params.token !== privateToken) { + if (privateToken && privateToken !== to.params.token) { // extend expiry time for 30 days after successful access const cookieExpiration = (30 * 24 * 60 * 1000) setCookie(to.params.token, privateToken, cookieExpiration) |