Remove legacy component & Update validating system using localstorage.

This commit is contained in:
吳元皓 2025-05-30 15:49:59 +08:00
parent b716a0ed5c
commit c5c614c75d
7 changed files with 141 additions and 86 deletions

View File

@ -23,7 +23,6 @@ const emit = defineEmits([
"openNewsSourcePage", "openNewsSourcePage",
"windowopener", "windowopener",
]); ]);
const staticid = computed(() => props.staticid);
const openNewWindow = (itemId: string) => { const openNewWindow = (itemId: string) => {
emit("windowopener", "aboutNewsOrg"); emit("windowopener", "aboutNewsOrg");
@ -285,10 +284,7 @@ const openPublisher = (text: string) => {
<div> <div>
<div> <div>
<h3 class="text-lg">類似文章</h3> <h3 class="text-lg">類似文章</h3>
<div <div class="space-y-2">
class="space-y-2"
>
<div <div
v-for="similar in useArgFindRel(item.title, item.publisher)" v-for="similar in useArgFindRel(item.title, item.publisher)"
:key="similar.item.id" :key="similar.item.id"
@ -302,8 +298,12 @@ const openPublisher = (text: string) => {
</div> </div>
</div> </div>
</div> </div>
<div v-if="checkIfEmpty(item.title)" class="text-gray-500 text-sm">找不到類似文章</div> <div
v-if="checkIfEmpty(item.title)"
class="text-gray-500 text-sm"
>
找不到類似文章
</div>
</div> </div>
<!--<div v-for="item in findRel(item.title)"> <!--<div v-for="item in findRel(item.title)">
{{ item }} {{ item }}

View File

@ -1,13 +1,32 @@
<script setup lang="ts"> <script setup lang="ts">
const logoutAction = () => { const user = ref();
const userToken = localStorage.getItem("token");
const { t, locale } = useI18n();
onMounted(async () => {
const req = await fetch("/api/user/validateUserToken", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
token: userToken,
lang: locale,
page: "settings",
}),
});
const res = req.json();
user.value = res;
});
} const logoutAction = () => {};
</script> </script>
<template> <template>
<div> <div class="justify-center align-center text-center">
<div class="bg-gray-400 p-2 m-2 w-full"> <div class="">Greetings, {{ user }}</div>
<div class="bg-gray-200/70 p-2 m-2 w-full">
<button @click="logoutAction">Logout</button> <button @click="logoutAction">Logout</button>
</div> </div>
<hr/> <hr />
<div class="justiy-center align-center text-center">Settings v0.0.1</div>
</div> </div>
</template> </template>

View File

@ -1,6 +1,6 @@
<script setup lang="ts"> <script setup lang="ts">
// Imports // Imports
const { t } = useI18n(); const { t, locale } = useI18n();
// Values // Values
const allowed = ref(false); const allowed = ref(false);
const error = ref(false); const error = ref(false);
@ -9,7 +9,21 @@ const emit = defineEmits(["windowopener", "error", "loadValue"]);
try { try {
// await :( // await :(
const { data, error: sendError } = await useFetch("/api/user/checkcookie"); const token = localStorage.getItem("token");
const { data, error: sendError } = await useFetch(
"/api/user/validateUserToken",
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
token: token,
lang: locale,
page: "a_window_application_using_blurPageBeforeLogin_component",
}),
},
);
if (sendError.value) { if (sendError.value) {
error.value = true; error.value = true;
} }

View File

@ -1,10 +1,55 @@
<script setup lang="ts">
const apis = [
{
apiroute: "/shortforward",
name: "A Simple url forwarder.",
content:
"This is maly used for yhw.tw/news, which is a super simple redirection tool for easy redirecting.",
caching: false,
},
{
apiroute: "/api/tabs",
name: "Get LINE Today Tabs",
content: "Using LINE Today as a source for getting tabs & caching results.",
caching: true,
},
{
apiroute: "/api/home/lt",
name: "Get the news feed of LINE Today",
content:
"This endpoint requires ?query=, and you can go the the /api/tabs to find the query.",
caching: true,
},
{
apiroute: "/api/news/get/lt/[slug]",
name: "Get the news article using node-fetch & cheerio",
content:
"This endpoint requires the slug to be filled in, in order to get it to work.",
caching: true,
},
{
apiroute: "/api/ai/chat/[slug]",
name: "",
content: "",
caching: false,
},
{
apiroute: "/api/ai/summarize/[slug]",
name: "",
content: "",
caching: false,
},
];
</script>
<template> <template>
<div <div
class="justify-center align-center text-center absolute inset-0 flex flex-col" class="justify-center align-center text-center absolute inset-0 flex flex-col"
> >
<h1 class="text-4xl text-bold">APIs</h1> <h1 class="text-4xl text-bold">APIs</h1>
<div class="items flex flex-row flex-wrap"> <div class="items flex flex-row flex-wrap">
<div class="item group"></div> <div class="item group" v-for="item in apis">
{{ item.name }}
</div>
</div> </div>
</div> </div>
</template> </template>

View File

@ -472,12 +472,6 @@ const openNewsSourcePage = async (slug: string, titleName: string) => {
passedValues.value = null; passedValues.value = null;
}, 1000); }, 1000);
}; };
// Not used?
const getStaticArticleId = () => {
storeStaticArticleId.value += 1;
return storeStaticArticleId.value;
};
</script> </script>
<template> <template>
<div v-if="changeLangAnimation"> <div v-if="changeLangAnimation">
@ -597,7 +591,6 @@ const getStaticArticleId = () => {
@loadValue="" @loadValue=""
@openArticles="openArticles" @openArticles="openArticles"
@openNewsSourcePage="openNewsSourcePage" @openNewsSourcePage="openNewsSourcePage"
:staticid="getStaticArticleId"
:values="passedValues" :values="passedValues"
/> />
</Suspense> </Suspense>

View File

@ -1,64 +0,0 @@
// This should be hooked up to a database soon.
import sql from "~/server/components/postgres";
// Parse Date Function
function checkDate(dateString: string) {
const now = new Date();
const parsed = new Date(dateString);
const timer = 60 * 60 * 1;
return now.getTime() - parsed.getTime() > timer;
}
export default defineEventHandler(async (event) => {
const loginCookie = getCookie(event, "session");
const lastCheckCookie = getCookie(event, "last_check");
const nowDate = new Date().toLocaleString();
console.log(nowDate);
if (!lastCheckCookie && loginCookie) {
deleteCookie(event, "session");
setCookie(event, "lastCheckCookie", nowDate, {
httpOnly: true,
secure: process.env.NODE_ENV === "production",
path: "/",
});
return {
auth: false,
user: null,
};
}
if (!lastCheckCookie) {
setCookie(event, "lastCheckCookie", nowDate, {
httpOnly: true,
secure: process.env.NODE_ENV === "production",
path: "/",
});
}
if (checkDate(String(lastCheckCookie))) {
setCookie(event, "lastCheckCookie", nowDate, {
httpOnly: true,
secure: process.env.NODE_ENV === "production",
path: "/",
});
}
if (!loginCookie) {
setCookie(event, "lastCheckCookie", nowDate, {
httpOnly: true,
secure: process.env.NODE_ENV === "production",
path: "/",
});
return {
auth: false,
user: null,
};
}
const loginCookieStore = atob(loginCookie);
/*const findUser = sql`
select * from userlogintokens
where token = ${loginCookieStore}
`;*/
return {
auth: true,
user: "testing",
loginCookie: loginCookieStore, // Debug
};
});

View File

@ -0,0 +1,48 @@
import sql from "~/server/components/postgres";
export default defineEventHandler(async (event) => {
const body = await readBody(event);
const token = body.token;
if (!token) {
return {
error: "NO_TOKEN_GIVEN",
requested_action: "SHOW_WARNING",
};
}
const checkIsUUIDRegex =
/[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}/;
if (!checkIsUUIDRegex.test(token)) {
return {
error: "NOT_A_UUID",
requested_action: "LOGOUT_USER",
};
}
const fetchViaSQL = await sql`
SELECT * FROM usertokens
where token=${token}
`;
if (!fetchViaSQL[0]) {
return {
error: "INVALID_TOKEN",
requested_action: "LOGOUT_USER",
};
}
const tokenDate = new Date(fetchViaSQL[0].created_at);
const now = new Date();
const dayInMilliseconds = 24 * 60 * 60 * 1000;
if (now.getTime() - tokenDate.getTime() > dayInMilliseconds) {
return {
error: "TOKEN_EXPIRED",
requested_action: "LOGOUT_USER",
};
}
return {
userAccount: fetchViaSQL[0].username,
requested_action: "CONTINUE",
email: fetchViaSQL[0].email,
avatarURL: fetchViaSQL[0].avatarurl,
firstName: fetchViaSQL[0].firstName,
};
});