Skip to content

Commit

Permalink
fix broken login
Browse files Browse the repository at this point in the history
  • Loading branch information
probablyjassin committed Dec 17, 2024
1 parent 817c0c7 commit 7466ecd
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 91 deletions.
78 changes: 38 additions & 40 deletions composables/useCampus.ts
Original file line number Diff line number Diff line change
@@ -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
};
}
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,
};
}
26 changes: 11 additions & 15 deletions middleware/isLoggedIn.global.ts
Original file line number Diff line number Diff line change
@@ -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");
});
89 changes: 53 additions & 36 deletions pages/login.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,27 @@
</div>
</div>
<div class="flex flex-col space-y-5 max-w-md p-4">
<input type="text" name="username" id="username" placeholder="Username" v-model="username"
:class="{ 'border-red-500': !usernameValid }" class="p-2 border rounded" />
<input
type="text"
name="username"
id="username"
placeholder="Username"
v-model="username"
:class="{ 'border-red-500': !usernameValid }"
class="p-2 border rounded" />
<p v-if="!usernameValid" class="text-red-500 text-sm">Bitte geben Sie einen Benutzernamen ein.</p>

<input type="password" name="Hash" id="hash" placeholder="Passwort (Hash)" v-model="password"
:class="{ 'border-red-500': !passwordValid }" class="p-2 border rounded" />
<input
type="password"
name="Hash"
id="hash"
placeholder="Passwort (Hash)"
v-model="password"
:class="{ 'border-red-500': !passwordValid }"
class="p-2 border rounded" />
<p v-if="!passwordValid" class="text-red-500 text-sm">Bitte geben Sie ein Passwort ein.</p>

<UButton @click="login" :loading="isLoading && !error" class="p-3 bg-primary text-text rounded block">Login
</UButton>
<UButton @click="login" :loading="isLoading && !error" class="p-3 bg-primary text-text rounded block">Login </UButton>

<p v-if="error" class="text-red-500 text-sm">
Der Login ist fehlgeschlagen. Überprüfe ob du tatsächlich deinen aktuellen Hash von der CampusDual API hast.
Expand All @@ -26,48 +37,54 @@
</template>

<script setup>
const router = useRouter();
const { getCampusData } = useCampus();
const router = useRouter();
const route = useRoute();
const username = ref("");
const password = ref("");
const usernameValid = ref(true);
const passwordValid = ref(true);
const username = ref("");
const password = ref("");
const usernameValid = ref(true);
const passwordValid = ref(true);
const isLoading = ref(false);
const error = ref(false);
const isLoading = ref(false);
const error = ref(false);
const usernameCookie = useCookie("username", { expires: new Date(Date.now() + 90 * 24 * 60 * 60 * 1000) });
const passwordCookie = useCookie("password", { expires: new Date(Date.now() + 90 * 24 * 60 * 60 * 1000) });
const usernameCookie = useCookie("username", { expires: new Date(Date.now() + 90 * 24 * 60 * 60 * 1000) });
const passwordCookie = useCookie("password", { expires: new Date(Date.now() + 90 * 24 * 60 * 60 * 1000) });
async function login() {
isLoading.value = true;
async function login() {
isLoading.value = true;
usernameValid.value = username.value !== "";
passwordValid.value = password.value !== "";
usernameValid.value = username.value !== "";
passwordValid.value = password.value !== "";
if (!usernameValid.value || !passwordValid.value) {
return;
}
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);
if (!usernameValid.value || !passwordValid.value) {
return;
}
if (typeof response === 'number') {
usernameCookie.value = username.value;
passwordCookie.value = password.value;
return router.push("/dash/stundenplan");
const response = await getCampusData("timeline");
console.log(response);
if (typeof response != "string") {
router.push({ path: "/dash/stundenplan" });
return;
}
error.value = true;
isLoading.value = false;
usernameCookie.value = null;
passwordCookie.value = null;
}
error.value = true;
isLoading.value = false;
}
</script>

<style scoped>
.border-red-500 {
border-color: #f56565;
}
.border-red-500 {
border-color: #f56565;
}
.text-red-500 {
color: #f56565;
}
.text-red-500 {
color: #f56565;
}
</style>

0 comments on commit 7466ecd

Please sign in to comment.