yuh kuh moar build time fixes #2!!!!! -_-
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
|
import { env } from "$env/dynamic/private";
|
||||||
import { dbApiUser } from "$lib/server/db/apiuser.db";
|
import { dbApiUser } from "$lib/server/db/apiuser.db";
|
||||||
import { getSessionToken } from "$lib/server/external/api.scraping.helpers";
|
import { getSessionToken } from "$lib/server/external/api.scraping.helpers";
|
||||||
|
import { logger } from "$lib/server/logger";
|
||||||
import {
|
import {
|
||||||
isSessionValidInStore,
|
isSessionValidInStore,
|
||||||
removeSessionFromStore,
|
removeSessionFromStore,
|
||||||
@@ -9,23 +11,20 @@ import { getUUID } from "$lib/utils";
|
|||||||
import { constants } from "$lib/utils/constants";
|
import { constants } from "$lib/utils/constants";
|
||||||
import type { ServerError } from "$lib/utils/data.types";
|
import type { ServerError } from "$lib/utils/data.types";
|
||||||
import { TRPCError } from "@trpc/server";
|
import { TRPCError } from "@trpc/server";
|
||||||
|
import fetch from "node-fetch";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
import { createTRPCRouter, protectedProcedure } from "../t";
|
import { createTRPCRouter, protectedProcedure } from "../t";
|
||||||
import { env } from "$env/dynamic/private";
|
|
||||||
import { logger } from "$lib/server/logger";
|
|
||||||
import fetch from "node-fetch";
|
|
||||||
|
|
||||||
|
|
||||||
export const apiAuthRouter = createTRPCRouter({
|
export const apiAuthRouter = createTRPCRouter({
|
||||||
getCaptcha: protectedProcedure.mutation(async () => {
|
getCaptcha: protectedProcedure.mutation(async () => {
|
||||||
const scrapingbeeApiKey = env.SCRAPINGBEE_API_KEY;
|
const scrapingbeeApiKey = env.SCRAPINGBEE_API_KEY ?? "";
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const uuid = getUUID();
|
const uuid = getUUID();
|
||||||
const targetUrl = `${constants.SCRAP_API_URL}/verify/image?uuid=${uuid}`;
|
const targetUrl = `${constants.SCRAP_API_URL}/verify/image?uuid=${uuid}`;
|
||||||
|
|
||||||
logger.info(`[getCaptcha] Fetching captcha image for uuid: ${uuid}`);
|
logger.info(`[getCaptcha] Fetching captcha image for uuid: ${uuid}`);
|
||||||
|
|
||||||
// Build ScrapingBee API URL with params
|
// Build ScrapingBee API URL with params
|
||||||
const scrapingbeeUrl = new URL("https://app.scrapingbee.com/api/v1");
|
const scrapingbeeUrl = new URL("https://app.scrapingbee.com/api/v1");
|
||||||
scrapingbeeUrl.searchParams.set("api_key", scrapingbeeApiKey);
|
scrapingbeeUrl.searchParams.set("api_key", scrapingbeeApiKey);
|
||||||
@@ -34,12 +33,14 @@ export const apiAuthRouter = createTRPCRouter({
|
|||||||
scrapingbeeUrl.searchParams.set("block_resources", "false");
|
scrapingbeeUrl.searchParams.set("block_resources", "false");
|
||||||
|
|
||||||
const res = await fetch(scrapingbeeUrl.toString());
|
const res = await fetch(scrapingbeeUrl.toString());
|
||||||
|
|
||||||
if (!res.ok || res.status !== 200) {
|
if (!res.ok || res.status !== 200) {
|
||||||
// Clone response before reading to avoid consuming body
|
// Clone response before reading to avoid consuming body
|
||||||
const clonedRes = res.clone();
|
const clonedRes = res.clone();
|
||||||
const errorText = await clonedRes.text().catch(() => "Unknown error");
|
const errorText = await clonedRes.text().catch(() => "Unknown error");
|
||||||
logger.error(`[getCaptcha] ScrapingBee error ${res.status}: ${errorText.substring(0, 200)}`);
|
logger.error(
|
||||||
|
`[getCaptcha] ScrapingBee error ${res.status}: ${errorText.substring(0, 200)}`,
|
||||||
|
);
|
||||||
throw new TRPCError({
|
throw new TRPCError({
|
||||||
code: "INTERNAL_SERVER_ERROR",
|
code: "INTERNAL_SERVER_ERROR",
|
||||||
message: `Failed to fetch captcha image: ${res.status}`,
|
message: `Failed to fetch captcha image: ${res.status}`,
|
||||||
@@ -50,8 +51,10 @@ export const apiAuthRouter = createTRPCRouter({
|
|||||||
const arrayBuffer = await res.arrayBuffer();
|
const arrayBuffer = await res.arrayBuffer();
|
||||||
const imageBuffer = Buffer.from(arrayBuffer);
|
const imageBuffer = Buffer.from(arrayBuffer);
|
||||||
const base64String = imageBuffer.toString("base64");
|
const base64String = imageBuffer.toString("base64");
|
||||||
|
|
||||||
logger.info(`[getCaptcha] Successfully fetched captcha image for uuid: ${uuid}, size: ${imageBuffer.length} bytes`);
|
logger.info(
|
||||||
|
`[getCaptcha] Successfully fetched captcha image for uuid: ${uuid}, size: ${imageBuffer.length} bytes`,
|
||||||
|
);
|
||||||
return { id: uuid, image: base64String };
|
return { id: uuid, image: base64String };
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
logger.error("[getCaptcha] Error getting captcha image", err);
|
logger.error("[getCaptcha] Error getting captcha image", err);
|
||||||
@@ -90,7 +93,7 @@ export const apiAuthRouter = createTRPCRouter({
|
|||||||
password = _user.password;
|
password = _user.password;
|
||||||
logger.info(`[getNewSession] Using specific user: ${userId}`);
|
logger.info(`[getNewSession] Using specific user: ${userId}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info(`[getNewSession] Getting session token for user ${userId}`);
|
logger.info(`[getNewSession] Getting session token for user ${userId}`);
|
||||||
const token = await getSessionToken({
|
const token = await getSessionToken({
|
||||||
code: captchaAnswer,
|
code: captchaAnswer,
|
||||||
@@ -99,7 +102,7 @@ export const apiAuthRouter = createTRPCRouter({
|
|||||||
userType: userType,
|
userType: userType,
|
||||||
password: password,
|
password: password,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!token.ok) {
|
if (!token.ok) {
|
||||||
logger.warn(`[getNewSession] Failed to get session token: ${token.message}`);
|
logger.warn(`[getNewSession] Failed to get session token: ${token.message}`);
|
||||||
return {
|
return {
|
||||||
@@ -107,7 +110,7 @@ export const apiAuthRouter = createTRPCRouter({
|
|||||||
errors: [{ message: token.message }],
|
errors: [{ message: token.message }],
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
await setSessionToRedis(token.message, input.userId ?? "");
|
await setSessionToRedis(token.message, input.userId ?? "");
|
||||||
logger.info(`[getNewSession] Successfully created session for user ${userId}`);
|
logger.info(`[getNewSession] Successfully created session for user ${userId}`);
|
||||||
return { success: true, errors: [] as ServerError };
|
return { success: true, errors: [] as ServerError };
|
||||||
|
|||||||
Reference in New Issue
Block a user