diff --git a/src/lib/trpc/routers/apiauth.router.ts b/src/lib/trpc/routers/apiauth.router.ts index 386f228..8969521 100755 --- a/src/lib/trpc/routers/apiauth.router.ts +++ b/src/lib/trpc/routers/apiauth.router.ts @@ -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