From 3f3d8b4eb3ff205b6619cb84b72dea75cb5b12ec Mon Sep 17 00:00:00 2001 From: Andreas Brett Date: Tue, 26 Oct 2021 12:58:04 +0200 Subject: [PATCH] fixes --- src/pages/Settings.vue | 10 ++++------ src/util-frontend.js | 19 +++++++++++++++++-- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/pages/Settings.vue b/src/pages/Settings.vue index f564fa2d..ba1e82cd 100644 --- a/src/pages/Settings.vue +++ b/src/pages/Settings.vue @@ -417,7 +417,7 @@ import TwoFADialog from "../components/TwoFADialog.vue"; dayjs.extend(utc); dayjs.extend(timezone); -import { timezoneList, setPageLocale } from "../util-frontend"; +import { timezoneList, setPageLocale, jwtDecrypt } from "../util-frontend"; import { useToast } from "vue-toastification"; const toast = useToast(); @@ -487,11 +487,9 @@ export default { }, loadUsername() { - const jwtToken = localStorage.getItem("token"); - const [, payloadBase64] = jwtToken.match(/^\S+\.(\S+)\.\S+$/i); - const payload = atob(payloadBase64); - const oPayload = JSON.parse(payload); - this.username = oPayload.username; + const jwtToken = this.$root.storage().token; + const jwtPayload = jwtDecrypt(jwtToken); + this.username = jwtPayload.username; }, loadSettings() { diff --git a/src/util-frontend.js b/src/util-frontend.js index 0b33bfd3..2412ced1 100644 --- a/src/util-frontend.js +++ b/src/util-frontend.js @@ -51,7 +51,22 @@ export function timezoneList() { } export function setPageLocale() { - const html = document.documentElement + const html = document.documentElement html.setAttribute('lang', currentLocale() ) html.setAttribute('dir', localeDirection() ) - } \ No newline at end of file +} + +export function jwtDecrypt(token) { + const base64Url = token.split(".")[1]; + const base64 = base64Url.replace(/-/g, "+").replace(/_/g, "/"); + const jsonPayload = decodeURIComponent( + atob(base64) + .split("") + .map(function(c) { + return "%" + ("00" + c.charCodeAt(0).toString(16)).slice(-2); + }) + .join("") + ); + + return JSON.parse(jsonPayload); +}