diff --git a/webapp/src/router/index.ts b/webapp/src/router/index.ts index ca49938..eb81a01 100644 --- a/webapp/src/router/index.ts +++ b/webapp/src/router/index.ts @@ -103,20 +103,4 @@ const router = createRouter({ ] }); -router.beforeEach((to, from, next) => { - // redirect to login page if not logged in and trying to access a restricted page - const publicPages = ['/', '/login', '/about', '/info/network', '/info/system', '/info/ntp', '/info/mqtt', ]; - const authRequired = !publicPages.includes(to.path); - const loggedIn = localStorage.getItem('user'); - - if (authRequired && !loggedIn) { - return next({ - path: '/login', - query: { returnUrl: to.path } - }); - } - - next(); -}); - export default router; \ No newline at end of file diff --git a/webapp/src/utils/authentication.ts b/webapp/src/utils/authentication.ts index 6d8276c..0bcb178 100644 --- a/webapp/src/utils/authentication.ts +++ b/webapp/src/utils/authentication.ts @@ -1,17 +1,19 @@ import type { Emitter, EventType } from "mitt"; +import type { Router } from "vue-router"; export function authHeader(): Headers { // return authorization header with basic auth credentials - let user = JSON.parse(localStorage.getItem('user') || ""); + let user = null; + try { + user = JSON.parse(localStorage.getItem('user') || ""); + } catch { } + const headers = new Headers(); + headers.append('X-Requested-With', 'XMLHttpRequest'); if (user && user.authdata) { - const headers = new Headers(); headers.append('Authorization', 'Basic ' + user.authdata); - headers.append('X-Requested-With', 'XMLHttpRequest') - return new Headers(headers); - } else { - return new Headers(); } + return new Headers(headers); } export function logout() { @@ -47,7 +49,7 @@ export function login(username: String, password: String) { }); } -export function handleResponse(response: Response, emitter: Emitter>) { +export function handleResponse(response: Response, emitter: Emitter>, router: Router) { return response.text().then(text => { const data = text && JSON.parse(text); if (!response.ok) { @@ -55,7 +57,7 @@ export function handleResponse(response: Response, emitter: Emitter handleResponse(response, this.$emitter)) + .then((response) => handleResponse(response, this.$emitter, this.$router)) .then( (response) => { this.alertMessage = response.message; diff --git a/webapp/src/views/DtuAdminView.vue b/webapp/src/views/DtuAdminView.vue index e23ba40..802def3 100644 --- a/webapp/src/views/DtuAdminView.vue +++ b/webapp/src/views/DtuAdminView.vue @@ -79,7 +79,7 @@ export default defineComponent({ getDtuConfig() { this.dataLoading = true; fetch("/api/dtu/config", { headers: authHeader() }) - .then((response) => handleResponse(response, this.$emitter)) + .then((response) => handleResponse(response, this.$emitter, this.$router)) .then( (data) => { this.dtuConfigList = data; @@ -98,7 +98,7 @@ export default defineComponent({ headers: authHeader(), body: formData, }) - .then((response) => handleResponse(response, this.$emitter)) + .then((response) => handleResponse(response, this.$emitter, this.$router)) .then( (response) => { this.alertMessage = response.message; diff --git a/webapp/src/views/HomeView.vue b/webapp/src/views/HomeView.vue index 0d5c65f..1256f16 100644 --- a/webapp/src/views/HomeView.vue +++ b/webapp/src/views/HomeView.vue @@ -463,8 +463,8 @@ export default defineComponent({ isLoggedIn, getInitialData() { this.dataLoading = true; - fetch("/api/livedata/status") - .then((response) => response.json()) + fetch("/api/livedata/status", { headers: authHeader() }) + .then((response) => handleResponse(response, this.$emitter, this.$router)) .then((data) => { this.liveData = data; this.dataLoading = false; @@ -526,8 +526,8 @@ export default defineComponent({ }, onShowEventlog(serial: number) { this.eventLogLoading = true; - fetch("/api/eventlog/status?inv=" + serial) - .then((response) => response.json()) + fetch("/api/eventlog/status?inv=" + serial, { headers: authHeader() }) + .then((response) => handleResponse(response, this.$emitter, this.$router)) .then((data) => { this.eventLogList = data[serial]; this.eventLogLoading = false; @@ -540,8 +540,8 @@ export default defineComponent({ }, onShowDevInfo(serial: number) { this.devInfoLoading = true; - fetch("/api/devinfo/status") - .then((response) => response.json()) + fetch("/api/devinfo/status", { headers: authHeader() }) + .then((response) => handleResponse(response, this.$emitter, this.$router)) .then((data) => { this.devInfoList = data[serial][0]; this.devInfoLoading = false; @@ -559,8 +559,8 @@ export default defineComponent({ this.targetLimitTypeText = "Relative (%)"; this.limitSettingLoading = true; - fetch("/api/limit/status") - .then((response) => response.json()) + fetch("/api/limit/status", { headers: authHeader() }) + .then((response) => handleResponse(response, this.$emitter, this.$router)) .then((data) => { this.currentLimitList = data[serial]; this.targetLimitList.serial = serial; @@ -583,7 +583,7 @@ export default defineComponent({ headers: authHeader(), body: formData, }) - .then((response) => handleResponse(response, this.$emitter)) + .then((response) => handleResponse(response, this.$emitter, this.$router)) .then( (response) => { if (response.type == "success") { @@ -614,8 +614,8 @@ export default defineComponent({ onShowPowerSettings(serial: number) { this.powerSettingLoading = true; - fetch("/api/power/status") - .then((response) => response.json()) + fetch("/api/power/status", { headers: authHeader() }) + .then((response) => handleResponse(response, this.$emitter, this.$router)) .then((data) => { this.successCommandPower = data[serial].power_set_status; this.powerSettingSerial = serial; @@ -653,7 +653,7 @@ export default defineComponent({ headers: authHeader(), body: formData, }) - .then((response) => handleResponse(response, this.$emitter)) + .then((response) => handleResponse(response, this.$emitter, this.$router)) .then( (response) => { if (response.type == "success") { diff --git a/webapp/src/views/InverterAdminView.vue b/webapp/src/views/InverterAdminView.vue index 981642e..47f0585 100644 --- a/webapp/src/views/InverterAdminView.vue +++ b/webapp/src/views/InverterAdminView.vue @@ -207,7 +207,7 @@ export default defineComponent({ getInverters() { this.dataLoading = true; fetch("/api/inverter/list", { headers: authHeader() }) - .then((response) => handleResponse(response, this.$emitter)) + .then((response) => handleResponse(response, this.$emitter, this.$router)) .then((data) => { this.inverters = data.inverter; this.dataLoading = false; @@ -222,7 +222,7 @@ export default defineComponent({ headers: authHeader(), body: formData, }) - .then((response) => handleResponse(response, this.$emitter)) + .then((response) => handleResponse(response, this.$emitter, this.$router)) .then((data) => { this.getInverters(); this.alert = data; diff --git a/webapp/src/views/MaintenanceRebootView.vue b/webapp/src/views/MaintenanceRebootView.vue index 0806ebd..5d8d415 100644 --- a/webapp/src/views/MaintenanceRebootView.vue +++ b/webapp/src/views/MaintenanceRebootView.vue @@ -78,7 +78,7 @@ export default defineComponent({ headers: authHeader(), body: formData, }) - .then((response) => handleResponse(response, this.$emitter)) + .then((response) => handleResponse(response, this.$emitter, this.$router)) .then((data) => { this.alertMessage = data.message; this.alertType = data.type; diff --git a/webapp/src/views/MqttAdminView.vue b/webapp/src/views/MqttAdminView.vue index 7694129..b905aaf 100644 --- a/webapp/src/views/MqttAdminView.vue +++ b/webapp/src/views/MqttAdminView.vue @@ -242,7 +242,7 @@ export default defineComponent({ getMqttConfig() { this.dataLoading = true; fetch("/api/mqtt/config", { headers: authHeader() }) - .then((response) => handleResponse(response, this.$emitter)) + .then((response) => handleResponse(response, this.$emitter, this.$router)) .then((data) => { this.mqttConfigList = data; this.dataLoading = false; @@ -259,7 +259,7 @@ export default defineComponent({ headers: authHeader(), body: formData, }) - .then((response) => handleResponse(response, this.$emitter)) + .then((response) => handleResponse(response, this.$emitter, this.$router)) .then( (response) => { this.alertMessage = response.message; diff --git a/webapp/src/views/MqttInfoView.vue b/webapp/src/views/MqttInfoView.vue index 2f20f46..43cc233 100644 --- a/webapp/src/views/MqttInfoView.vue +++ b/webapp/src/views/MqttInfoView.vue @@ -149,6 +149,7 @@