diff --git a/frontend/src/helpers/token.ts b/frontend/src/helpers/token.ts new file mode 100644 index 0000000..ba6c8a7 --- /dev/null +++ b/frontend/src/helpers/token.ts @@ -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"); +} \ No newline at end of file diff --git a/frontend/src/view/EditCalendarView.vue b/frontend/src/view/EditCalendarView.vue index 8bf44c9..58d3193 100644 --- a/frontend/src/view/EditCalendarView.vue +++ b/frontend/src/view/EditCalendarView.vue @@ -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 = ref(""); const modules: Ref> = 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);