Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/wip/composable'
Browse files Browse the repository at this point in the history
  • Loading branch information
probablyjassin committed Dec 5, 2024
2 parents dfff421 + 25a9011 commit 09ae1f0
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 75 deletions.
42 changes: 21 additions & 21 deletions app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,27 @@
</template>

<script setup>
useHead({
title: "Stundenplan",
meta: [
{
name: "description",
content: "Einfache, mobile-first WebApp für den CampusDual-Stundenplan. Support für Firefox und Chromium.",
},
],
});
useHead({
title: "Stundenplan",
meta: [
{
name: "description",
content: "Einfache, mobile-first WebApp für den CampusDual-Stundenplan. Support für Firefox und Chromium.",
},
],
});
const username = useCookie("username");
const password = useCookie("password");
const isLoggedIn = useState("isLoggedIn", () => false);
const username = useCookie("username");
const password = useCookie("password");
const isLoggedIn = useState("isLoggedIn", () => false);
onMounted(async () => {
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 = !!(response == 0);
} catch (error) {
isLoggedIn.value = false;
}
});
onMounted(async () => {
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');
} catch (error) {
isLoggedIn.value = false;
}
});
</script>
43 changes: 43 additions & 0 deletions composables/useCampus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { ref } from 'vue';

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
};
}
2 changes: 1 addition & 1 deletion middleware/isLoggedIn.global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default defineNuxtRouteMiddleware(async (to, from) => {
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 = !!(response == 0)
isLoggedIn.value = !!(typeof response === 'number')
} catch (error) {
isLoggedIn.value = false
}
Expand Down
21 changes: 16 additions & 5 deletions pages/dash/stundenplan.vue
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ definePageMeta({
key: route => route.fullPath,
});
const { getCampusData } = useCampus()
const router = useRouter();
const localData = ref([]);
Expand Down Expand Up @@ -117,14 +119,23 @@ onMounted(async () => {
localStorage.setItem('stundenplan', JSON.stringify(newValue));
}, { deep: true });
const response = await getCampusData('room');
function arraysEqual(a, b) {
if (a === b) return true;
if (a == null || b == null) return false;
if (a.length !== b.length) return false;
const response = await $fetch(
`https://corsproxy.io/?https%3A%2F%2Fselfservice.campus-dual.de%2Froom%2Fjson%3Fuserid%3D${username.value}%26hash%3D${password.value}%26t%3D${Math.floor(Date.now() / 1000)}%26_%3D${Date.now()}`
);
for (var i = 0; i < a.length; ++i) {
if (JSON.stringify(a[i]) !== JSON.stringify(b[i])) return false;
}
return true;
}
data.value = response;
localData.value = data.value;
if (!arraysEqual(JSON.parse(JSON.stringify(localData.value)), response)) {
data.value = response;
localData.value = data.value;
}
hasLoaded.value = true;
});
Expand Down
85 changes: 37 additions & 48 deletions pages/login.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,16 @@
</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 @@ -37,48 +26,48 @@
</template>

<script setup>
const router = useRouter();
const router = useRouter();
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;
}
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);
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 (response == 0) {
usernameCookie.value = username.value;
passwordCookie.value = password.value;
return router.push("/dash/stundenplan");
}
error.value = true;
isLoading.value = false;
if (typeof response === 'number') {
usernameCookie.value = username.value;
passwordCookie.value = password.value;
return router.push("/dash/stundenplan");
}
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 09ae1f0

Please sign in to comment.