43 lines
1.2 KiB
TypeScript
43 lines
1.2 KiB
TypeScript
export default defineNuxtRouteMiddleware(async (to, _from) => {
|
|
const runtimeConfig = useRuntimeConfig()
|
|
|
|
const code = to.query.code;
|
|
if (to.path === "/poll") {
|
|
console.log(`${runtimeConfig.public.apiBase}/check/${code}`)
|
|
const { error } = await useFetch(`${runtimeConfig.public.apiBase}/check/${code}`);
|
|
if (error.value && error.value.statusCode !== 200) {
|
|
if(error.value.statusCode == 403){
|
|
return navigateTo({path:"/finished", query:to.query});
|
|
}
|
|
throw createError({
|
|
statusCode: error.value.statusCode,
|
|
statusMessage: error.value.statusMessage,
|
|
message: error.value.data.message
|
|
});
|
|
}
|
|
return;
|
|
}
|
|
|
|
if(to.path === "/finished"){
|
|
return
|
|
}
|
|
|
|
if(to.path === "/results"){
|
|
if(code != "fc6d9f50-68af-4df5-95d4-36745c2938c9"){
|
|
throw createError({
|
|
statusCode: 403,
|
|
statusMessage: "Invalid code",
|
|
message: "Vous ne pouvez pas accéder aux résultats avec ce code !"
|
|
});
|
|
}
|
|
return
|
|
}
|
|
|
|
// In a real app you would probably not redirect every route to `/`
|
|
// however it is important to check `to.path` before redirecting or you
|
|
// might get an infinite redirect loop
|
|
if (to.path !== "/") {
|
|
return navigateTo("/");
|
|
}
|
|
});
|