mirror of
https://gitlab.dit.htwk-leipzig.de/htwk-software/htwkalender-pwa.git
synced 2025-07-16 09:38:51 +02:00
feat:#4 extracted token regex test
This commit is contained in:
19
frontend/src/helpers/token.ts
Normal file
19
frontend/src/helpers/token.ts
Normal file
@ -0,0 +1,19 @@
|
||||
const tokenRegex = /^[a-z0-9]{15}$/;
|
||||
const tokenUriRegex = /[?&]token=([a-z0-9]{15})(?:&|$)/;
|
||||
|
||||
export const isToken = (token: string): boolean => {
|
||||
return tokenRegex.test(token) || tokenUriRegex.test(token);
|
||||
};
|
||||
|
||||
export function extractToken(token: string): string {
|
||||
if (tokenRegex.test(token)) {
|
||||
return token;
|
||||
}
|
||||
|
||||
const match = tokenUriRegex.exec(token);
|
||||
if (match) {
|
||||
return match[1];
|
||||
}
|
||||
|
||||
throw new Error("Invalid token");
|
||||
}
|
@ -26,6 +26,7 @@ import tokenStore from "../store/tokenStore";
|
||||
import { useToast } from "primevue/usetoast";
|
||||
import { useI18n } from "vue-i18n";
|
||||
import DynamicPage from "./DynamicPage.vue";
|
||||
import { extractToken, isToken } from "@/helpers/token.ts";
|
||||
const { t } = useI18n({ useScope: "global" });
|
||||
|
||||
const toast = useToast();
|
||||
@ -33,26 +34,6 @@ const toast = useToast();
|
||||
const token: Ref<string> = ref("");
|
||||
const modules: Ref<Map<string, Module>> = ref(moduleStore().modules);
|
||||
|
||||
const tokenRegex = /^[a-z0-9]{15}$/;
|
||||
const tokenUriRegex = /[?&]token=([a-z0-9]{15})(?:&|$)/;
|
||||
|
||||
const isToken = (token: string): boolean => {
|
||||
return tokenRegex.test(token) || tokenUriRegex.test(token);
|
||||
};
|
||||
|
||||
function extractToken(token: string): string {
|
||||
if (tokenRegex.test(token)) {
|
||||
return token;
|
||||
}
|
||||
|
||||
const match = tokenUriRegex.exec(token);
|
||||
if (match) {
|
||||
return match[1];
|
||||
}
|
||||
|
||||
throw new Error("Invalid token");
|
||||
}
|
||||
|
||||
function loadCalendar(): void {
|
||||
try {
|
||||
token.value = extractToken(token.value);
|
||||
|
Reference in New Issue
Block a user