From 1e4904c7d8c2e7607e42e05157a281c9d534be87 Mon Sep 17 00:00:00 2001 From: LucHeart Date: Fri, 6 Oct 2023 06:52:37 +0200 Subject: [PATCH] Fix for auth change --- src/js/ApiCall.js | 12 ++++++++---- src/js/SlWs.js | 11 +++++++++-- src/js/utils.js | 23 +++++++++++++++-------- src/views/Login/Login.vue | 5 +++-- src/views/Root.vue | 2 +- src/views/dashboard/DashboardRoot.vue | 5 ++++- 6 files changed, 40 insertions(+), 18 deletions(-) diff --git a/src/js/ApiCall.js b/src/js/ApiCall.js index 036ebae..8e5ecd6 100644 --- a/src/js/ApiCall.js +++ b/src/js/ApiCall.js @@ -12,13 +12,16 @@ class ApiCall { return await axios({ method: method, url: config.apiUrl + path, - data: data + data: data, + headers: { + OpenShockSession: localStorage.getItem("token") + } }); } catch (err) { toastr.error(utils.getError(err), "API interaction failed"); if(err.response !== undefined && err.response.status === 401) { + localStorage.removeItem("token"); router.push('/account/login'); - utils.setLogin(""); return undefined; } @@ -26,7 +29,8 @@ class ApiCall { } } - async makeCall(method, path, data, headers) { + async makeCallHeaders(method, path, data, headers) { + headers.OpenShockSession = localStorage.getItem("token"); try { return await axios({ method: method, @@ -37,8 +41,8 @@ class ApiCall { } catch (err) { toastr.error(utils.getError(err), "API interaction failed"); if(err.response !== undefined && err.response.status === 401) { + localStorage.removeItem("token"); router.push('/account/login'); - utils.setLogin(""); return undefined; } diff --git a/src/js/SlWs.js b/src/js/SlWs.js index 1859286..fe37a0b 100644 --- a/src/js/SlWs.js +++ b/src/js/SlWs.js @@ -1,8 +1,10 @@ import storeF from '@/store' +import router from '@/router' import * as signalR from '@microsoft/signalr' + const connection = new signalR.HubConnectionBuilder() - .withUrl(config.apiUrl + "1/hubs/user") + .withUrl(config.apiUrl + "1/hubs/user?session=" + localStorage.getItem("token")) .configureLogging(signalR.LogLevel.Information) .withAutomaticReconnect([0, 1000, 2000, 5000, 10000, 10000, 15000, 30000, 60000]) .build(); @@ -45,7 +47,12 @@ setInterval(() => { } }, 200); -connection.start().catch((err) => toastr.error(err, "User Hub")); +connection.start().catch((err) => { + if(err.message && err.message.includes(`Status code '401'`)) { + localStorage.removeItem("token"); + router.push('/account/login'); + } else toastr.error(err, "User Hub"); +}); global.ws = ws; global.userHubConnection = connection; \ No newline at end of file diff --git a/src/js/utils.js b/src/js/utils.js index 932141a..a95a2c0 100644 --- a/src/js/utils.js +++ b/src/js/utils.js @@ -15,12 +15,16 @@ let utils = { } return ""; }, - setLogin() { - this.setCookie("loggedIn", "true", 7); - }, - authExists() { - let user = this.getCookie("loggedIn"); - return user === "true"; + isLoggedIn() { + const expiresOnRaw = localStorage.getItem("token_validUntil"); + if(expiresOnRaw === null) return false; + const tokenExists = localStorage.getItem("token") !== null; + if(!tokenExists) return false; + + const asDate = new Date(expiresOnRaw); + if(isNaN(asDate)) return false; + + return asDate > Date.now(); }, isDarkMode() { const cook = this.getCookie("settings_dark"); @@ -30,7 +34,7 @@ let utils = { return true; }, setDarkMode(dark) { - this.setCookie("settings_dark", dark, 3652); + localStorage.setItem("dark", dark); }, setCookie(cname, cvalue, exdays) { const d = new Date(); @@ -56,7 +60,10 @@ let utils = { try { const res = await axios({ method: "GET", - url: config.apiUrl + "1/users/self" + url: config.apiUrl + "1/users/self", + headers: { + OpenShockSession: localStorage.getItem("token") + } }); return res.status === 200; diff --git a/src/views/Login/Login.vue b/src/views/Login/Login.vue index e993d03..18da958 100644 --- a/src/views/Login/Login.vue +++ b/src/views/Login/Login.vue @@ -83,13 +83,14 @@ export default { method: 'POST', url: config.apiUrl + '1/account/login', data: { - username: this.username, email: this.username, password: this.password } }); + + localStorage.setItem("token", res.data.data.sessionToken); + localStorage.setItem("token_validUntil", res.data.data.validUntil); this.successful = true; - utils.setLogin(); setTimeout(() => { const returnUrl = this.$store.state.returnUrl; if(returnUrl !== undefined) { diff --git a/src/views/Root.vue b/src/views/Root.vue index cb16d10..2be83f9 100644 --- a/src/views/Root.vue +++ b/src/views/Root.vue @@ -10,7 +10,7 @@ export default { methods: { redirect() { console.log("Redirecting..."); - if(utils.authExists()) { + if(utils.isLoggedIn()) { this.$router.push('/dashboard/'); } else { this.$router.push('/account/'); diff --git a/src/views/dashboard/DashboardRoot.vue b/src/views/dashboard/DashboardRoot.vue index 9f6262e..b55388e 100644 --- a/src/views/dashboard/DashboardRoot.vue +++ b/src/views/dashboard/DashboardRoot.vue @@ -79,11 +79,14 @@ export default { }, methods: { async getSelf() { + this.loading = true; try { await this.$store.dispatch('getSelf'); this.success = true; - } catch (_) { } + } catch (err) { + console.error(err); + } this.loading = false;