fix auth check if database is uninitialised

This commit is contained in:
Wroclaw 2023-06-14 12:37:57 +02:00
parent 75f809051c
commit cbfc4e9317

View file

@ -8,6 +8,7 @@ const endpointsWithoutAuth: string[] = [
"/hi", "/hi",
"/login", "/login",
"/logout", "/logout",
"/firstRun",
]; ];
export default defineEventHandler(async (e) => { export default defineEventHandler(async (e) => {
@ -31,10 +32,14 @@ export default defineEventHandler(async (e) => {
*/ */
export async function isAuthorised(token: string | undefined): Promise<boolean> { export async function isAuthorised(token: string | undefined): Promise<boolean> {
if (!token) return false; if (!token) return false;
const [[session]] = await database.query( try {
"SELECT EXISTS(SELECT `id` FROM `sessions` WHERE `id` = ? AND `expiry_date` >= NOW()) as `logged_in`", const [[session]] = await database.query(
[token], "SELECT EXISTS(SELECT `id` FROM `sessions` WHERE `id` = ? AND `expiry_date` >= NOW()) as `logged_in`",
) as unknown as data<{logged_in: number}>; [token],
) as unknown as data<{logged_in: number}>;
return session.logged_in === 1; return session.logged_in === 1;
} catch {
return false;
}
} }