106 lines
3.3 KiB
TypeScript
Executable File
106 lines
3.3 KiB
TypeScript
Executable File
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 }) => {
|
|
console.log("[=] Getting new session... ", input);
|
|
const { captchaId, captchaAnswer } = input;
|
|
let { userId, userType, password } =
|
|
await dbApiUser.getRandomDistributor();
|
|
if (input.userId) {
|
|
let _user = await dbApiUser.getUserById(input.userId);
|
|
console.log("[=] User :: ", _user?.userId);
|
|
if (!_user) {
|
|
return {
|
|
success: false,
|
|
errors: [{ message: "User not found." }],
|
|
};
|
|
}
|
|
userId = _user.userId;
|
|
userType = _user.userType;
|
|
password = _user.password;
|
|
}
|
|
console.log(`[=] Getting session token for user ${userId}...`);
|
|
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 };
|
|
}),
|
|
});
|