This commit is contained in:
bootunloader
2024-09-19 13:39:22 +03:00
parent 0fb8ba7ff4
commit 1466a45105

View File

@@ -49,39 +49,47 @@ export const apiAuthRouter = createTRPCRouter({
)
.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) {
try {
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: "User not found." }],
errors: [{ message: token.message }],
};
}
userId = _user.userId;
userType = _user.userType;
password = _user.password;
await setSessionToRedis(token.message, input.userId ?? "");
return { success: true, errors: [] as ServerError };
} catch (err) {
console.log(err);
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: "Error getting new session.",
});
}
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