initial commit ??

This commit is contained in:
bootunloader
2024-09-11 02:57:43 +03:00
commit 33cbeaa9a3
186 changed files with 17269 additions and 0 deletions

View File

@@ -0,0 +1,102 @@
import { getSessionToken } from "$lib/server/external/api.scraping.helpers";
import { TRPCError } from "@trpc/server";
import { createTRPCRouter, protectedProcedure } from "../t";
import { constants } from "$lib/utils/constants";
import { getUUID } from "$lib/utils";
import { z } from "zod";
import { dbApiUser } from "$lib/server/db/apiuser.db";
import type { ServerError } from "$lib/utils/data.types";
import {
isSessionValidInStore,
removeSessionFromStore,
setSessionToRedis,
} from "$lib/server/utils/session.service";
export const apiAuthRouter = createTRPCRouter({
getCaptcha: protectedProcedure.mutation(async () => {
try {
const uuid = getUUID();
const res = await fetch(
`${constants.SCRAP_API_URL}/verify/image?uuid=${uuid}`,
{
headers: {
...constants.SCRAP_API_BASE_HEADERS,
Accept:
"image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8",
},
},
);
const bloob = await res.blob();
const imageBuffer = Buffer.from(await bloob.arrayBuffer());
const base64String = imageBuffer.toString("base64");
return { id: uuid, image: base64String };
} catch (err) {
console.log(err);
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: "Error getting captcha image.",
});
}
}),
getNewSession: protectedProcedure
.input(
z.object({
captchaId: z.string().min(1),
captchaAnswer: z.string().min(1),
userId: z.string().optional(),
}),
)
.mutation(async ({ input }) => {
const { captchaId, captchaAnswer } = input;
let { userId, userType, password } =
await dbApiUser.getRandomDistributor();
if (input.userId) {
let _user = await dbApiUser.getUserById(input.userId);
if (!_user) {
return {
success: false,
errors: [{ message: "User not found." }],
};
}
userId = _user.userId;
userType = _user.userType;
password = _user.password;
}
const token = await getSessionToken({
code: captchaAnswer,
verifyToken: captchaId,
userId: userId,
userType: userType,
password: password,
});
console.log("[=] Token Response :: ", JSON.stringify(token, null, 2));
if (!token.ok) {
return {
success: false,
errors: [{ message: token.message }],
};
}
await setSessionToRedis(token.message, input.userId ?? "");
return { success: true, errors: [] as ServerError };
}),
isApiSessionValid: protectedProcedure
.input(
z.object({
checkingUserSession: z.boolean(),
userId: z.string().optional(),
}),
)
.query(async ({ input }) => {
return { valid: await isSessionValidInStore(input.userId) };
}),
logoutUser: protectedProcedure
.input(z.object({ userId: z.string().optional() }))
.mutation(async ({ input }) => {
const { userId } = input;
await removeSessionFromStore(userId);
return { success: true, errors: [] as ServerError };
}),
});