diff --git a/composables/useCampus.ts b/composables/useCampus.ts index 58e3af1..1270c55 100644 --- a/composables/useCampus.ts +++ b/composables/useCampus.ts @@ -1,43 +1,41 @@ -import { ref } from 'vue'; +import { ref } from "vue"; -const baseCampusURL = "https://selfservice.campus-dual.de" -const baseCorsUrl = "https://corsproxy.io/?" +const baseCampusURL = "https://selfservice.campus-dual.de"; +const baseCorsUrl = "https://corsproxy.io/?"; export function useCampus() { - const username = useCookie("username"); - const password = useCookie("password"); - - async function getCampusData(type: 'room' | 'timeline' | 'credits' | 'semester') { - - const UrlParams = `?userid=${username.value}&hash=${password.value}&t=${Math.floor(Date.now() / 1000)}&_=${Date.now()}` - - switch (type) { - - case 'room': - var urlPath = "/room/json" - break; - case 'timeline': - var urlPath = "/dash/gettimeline" - break; - case 'credits': - var urlPath = "/dash/getcp" - break; - case 'semester': - var urlPath = "/dash/getfs" - break; - - default: - throw new Error('Invalid type'); - } - - const url = baseCorsUrl + encodeURIComponent(baseCampusURL + urlPath + UrlParams) - - return await $fetch(url); - } - - return { - username, - password, - getCampusData - }; -} \ No newline at end of file + const username = useCookie("username"); + const password = useCookie("password"); + + async function getCampusData(type: "room" | "timeline" | "credits" | "semester") { + const UrlParams = `?userid=${username.value}&hash=${password.value}&t=${Math.floor(Date.now() / 1000)}&_=${Date.now()}`; + + switch (type) { + case "room": + var urlPath = "/room/json"; + break; + case "timeline": + var urlPath = "/dash/gettimeline"; + break; + case "credits": + var urlPath = "/dash/getcp"; + break; + case "semester": + var urlPath = "/dash/getfs"; + break; + + default: + throw new Error("Invalid type"); + } + + const url = baseCorsUrl + encodeURIComponent(baseCampusURL + urlPath + UrlParams); + + return await $fetch(url); + } + + return { + username, + password, + getCampusData, + }; +} diff --git a/middleware/isLoggedIn.global.ts b/middleware/isLoggedIn.global.ts index d1cabf8..42fa44a 100644 --- a/middleware/isLoggedIn.global.ts +++ b/middleware/isLoggedIn.global.ts @@ -1,29 +1,25 @@ export default defineNuxtRouteMiddleware(async (to, from) => { if (to.fullPath == "/stundenplan") { - return navigateTo("/dash/stundenplan") + return navigateTo("/dash/stundenplan"); } - if (import.meta.server) return true + if (import.meta.server) return true; - - const isLoggedIn = useState("isLoggedIn", (() => false) as () => boolean) + const { getCampusData } = useCampus(); + const isLoggedIn = useState("isLoggedIn", (() => false) as () => boolean); if (!to.fullPath.includes("dash")) { - return true + return true; } - const username = useCookie("username") - const password = useCookie("password") if (!isLoggedIn.value) { try { - const test_url = `https://corsproxy.io/?https%3A%2F%2Fselfservice.campus-dual.de%2Fdash%2Fgetcp%3Fuser%3D${username.value}%26hash%3D${password.value}` - const response = await $fetch(test_url) - isLoggedIn.value = !!(typeof response === 'number') + const response = await getCampusData("timeline"); + isLoggedIn.value = !!(typeof response != "string"); } catch (error) { - isLoggedIn.value = false + isLoggedIn.value = false; } } - - if (isLoggedIn.value) return true - return navigateTo("/login") -}) + if (isLoggedIn.value) return true; + return navigateTo("/login"); +}); diff --git a/pages/login.vue b/pages/login.vue index 50d844c..3e547db 100644 --- a/pages/login.vue +++ b/pages/login.vue @@ -8,16 +8,27 @@
- +

Bitte geben Sie einen Benutzernamen ein.

- +

Bitte geben Sie ein Passwort ein.

- Login - + Login

Der Login ist fehlgeschlagen. Überprüfe ob du tatsächlich deinen aktuellen Hash von der CampusDual API hast. @@ -26,48 +37,54 @@