feat:#4 extracted token regex test

This commit is contained in:
Elmar Kresse
2024-05-20 13:41:16 +02:00
parent eb21c22d17
commit 1f1d5300e2
2 changed files with 20 additions and 20 deletions

View 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");
}

View File

@ -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);