Edit: /tmp/tsx-0/17887-ddc7fc466db761ea2bb0436015e273989e541c3c (6649B)
{"code":"__filename=\"/opt/canhelp/node_modules/better-auth/dist/plugins/two-factor/verify-two-factor.mjs\";(()=>{\nvar __defProp=Object.defineProperty;var __getOwnPropDesc=Object.getOwnPropertyDescriptor;var __getOwnPropNames=Object.getOwnPropertyNames;var __hasOwnProp=Object.prototype.hasOwnProperty;var __name=(target,value)=>__defProp(target,\"name\",{value,configurable:true});var __export=(target,all)=>{for(var name in all)__defProp(target,name,{get:all[name],enumerable:true})};var __copyProps=(to,from,except,desc)=>{if(from&&typeof from===\"object\"||typeof from===\"function\"){for(let key of __getOwnPropNames(from))if(!__hasOwnProp.call(to,key)&&key!==except)__defProp(to,key,{get:()=>from[key],enumerable:!(desc=__getOwnPropDesc(from,key))||desc.enumerable})}return to};var __toCommonJS=mod=>__copyProps(__defProp({},\"__esModule\",{value:true}),mod);var verify_two_factor_exports={};__export(verify_two_factor_exports,{verifyTwoFactor:()=>verifyTwoFactor});module.exports=__toCommonJS(verify_two_factor_exports);var import_schema=require(\"../../db/schema.mjs\");var import_random=require(\"../../crypto/random.mjs\");var import_cookies=require(\"../../cookies/index.mjs\");var import_session=require(\"../../api/routes/session.mjs\");var import_api=require(\"../../api/index.mjs\");var import_error_code=require(\"./error-code.mjs\");var import_constant=require(\"./constant.mjs\");var import_error=require(\"@better-auth/core/error\");var import_hmac=require(\"@better-auth/utils/hmac\");async function verifyTwoFactor(ctx){const invalid=__name(errorKey=>{throw import_error.APIError.from(\"UNAUTHORIZED\",import_error_code.TWO_FACTOR_ERROR_CODES[errorKey])},\"invalid\");const session=await(0,import_session.getSessionFromCtx)(ctx);if(!session){const twoFactorCookie=ctx.context.createAuthCookie(import_constant.TWO_FACTOR_COOKIE_NAME);const signedTwoFactorCookie=await ctx.getSignedCookie(twoFactorCookie.name,ctx.context.secret);if(!signedTwoFactorCookie)throw import_error.APIError.from(\"UNAUTHORIZED\",import_error_code.TWO_FACTOR_ERROR_CODES.INVALID_TWO_FACTOR_COOKIE);const verificationToken=await ctx.context.internalAdapter.findVerificationValue(signedTwoFactorCookie);if(!verificationToken)throw import_error.APIError.from(\"UNAUTHORIZED\",import_error_code.TWO_FACTOR_ERROR_CODES.INVALID_TWO_FACTOR_COOKIE);const user=await ctx.context.internalAdapter.findUserById(verificationToken.value);if(!user)throw import_error.APIError.from(\"UNAUTHORIZED\",import_error_code.TWO_FACTOR_ERROR_CODES.INVALID_TWO_FACTOR_COOKIE);const dontRememberMe=await ctx.getSignedCookie(ctx.context.authCookies.dontRememberToken.name,ctx.context.secret);return{valid:__name(async ctx2=>{const session2=await ctx2.context.internalAdapter.createSession(verificationToken.value,!!dontRememberMe);if(!session2)throw import_error.APIError.from(\"INTERNAL_SERVER_ERROR\",{message:\"failed to create session\",code:\"FAILED_TO_CREATE_SESSION\"});await ctx2.context.internalAdapter.deleteVerificationByIdentifier(signedTwoFactorCookie);await(0,import_cookies.setSessionCookie)(ctx2,{session:session2,user});(0,import_cookies.expireCookie)(ctx2,twoFactorCookie);if(ctx2.body.trustDevice){const maxAge=ctx2.context.getPlugin(\"two-factor\").options?.trustDeviceMaxAge??import_constant.TRUST_DEVICE_COOKIE_MAX_AGE;const trustDeviceCookie=ctx2.context.createAuthCookie(import_constant.TRUST_DEVICE_COOKIE_NAME,{maxAge});const trustIdentifier=`trust-device-${(0,import_random.generateRandomString)(32)}`;const token=await(0,import_hmac.createHMAC)(\"SHA-256\",\"base64urlnopad\").sign(ctx2.context.secret,`${user.id}!${trustIdentifier}`);await ctx2.context.internalAdapter.createVerificationValue({value:user.id,identifier:trustIdentifier,expiresAt:new Date(Date.now()+maxAge*1e3)});await ctx2.setSignedCookie(trustDeviceCookie.name,`${token}!${trustIdentifier}`,ctx2.context.secret,trustDeviceCookie.attributes);(0,import_cookies.expireCookie)(ctx2,ctx2.context.authCookies.dontRememberToken)}return ctx2.json({token:session2.token,user:(0,import_schema.parseUserOutput)(ctx2.context.options,user)})},\"valid\"),invalid,session:{session:null,user},key:signedTwoFactorCookie}}return{valid:__name(async ctx2=>{return ctx2.json({token:session.session.token,user:(0,import_schema.parseUserOutput)(ctx2.context.options,session.user)})},\"valid\"),invalid,session,key:`${session.user.id}!${session.session.id}`}}__name(verifyTwoFactor,\"verifyTwoFactor\");0&&(module.exports={verifyTwoFactor});\n})()\n","warnings":[],"map":{"version":3,"mappings":";suBAAA,oLAAgC,+BAChC,kBAAqC,mCACrC,mBAA+C,mCAC/C,mBAAkC,wCAClC,eAAO,+BACP,sBAAuC,4BACvC,oBAA8F,0BAC9F,iBAAyB,mCACzB,gBAA2B,mCAG3B,eAAe,gBAAgB,IAAK,CACnC,MAAM,QAAU,OAAC,UAAa,CAC7B,MAAM,sBAAS,KAAK,eAAgB,yCAAuB,QAAQ,CAAC,CACrE,EAFgB,WAGhB,MAAM,QAAU,QAAM,kCAAkB,GAAG,EAC3C,GAAI,CAAC,QAAS,CACb,MAAM,gBAAkB,IAAI,QAAQ,iBAAiB,sCAAsB,EAC3E,MAAM,sBAAwB,MAAM,IAAI,gBAAgB,gBAAgB,KAAM,IAAI,QAAQ,MAAM,EAChG,GAAI,CAAC,sBAAuB,MAAM,sBAAS,KAAK,eAAgB,yCAAuB,yBAAyB,EAChH,MAAM,kBAAoB,MAAM,IAAI,QAAQ,gBAAgB,sBAAsB,qBAAqB,EACvG,GAAI,CAAC,kBAAmB,MAAM,sBAAS,KAAK,eAAgB,yCAAuB,yBAAyB,EAC5G,MAAM,KAAO,MAAM,IAAI,QAAQ,gBAAgB,aAAa,kBAAkB,KAAK,EACnF,GAAI,CAAC,KAAM,MAAM,sBAAS,KAAK,eAAgB,yCAAuB,yBAAyB,EAC/F,MAAM,eAAiB,MAAM,IAAI,gBAAgB,IAAI,QAAQ,YAAY,kBAAkB,KAAM,IAAI,QAAQ,MAAM,EACnH,MAAO,CACN,MAAO,aAAOA,MAAQ,CACrB,MAAMC,SAAU,MAAMD,KAAI,QAAQ,gBAAgB,cAAc,kBAAkB,MAA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