"use strict";(()=>{var e={};e.id=3040,e.ids=[3040],e.modules={27825:e=>{e.exports=require("lodash")},75600:e=>{e.exports=require("next/dist/compiled/next-server/pages-api.runtime.prod.js")},79428:e=>{e.exports=require("buffer")},79646:e=>{e.exports=require("child_process")},55511:e=>{e.exports=require("crypto")},29021:e=>{e.exports=require("fs")},33873:e=>{e.exports=require("path")},96762:(e,r)=>{Object.defineProperty(r,"M",{enumerable:!0,get:function(){return function e(r,t){return t in r?r[t]:"then"in r&&"function"==typeof r.then?r.then(r=>e(r,t)):"function"==typeof r&&"default"===t?r:void 0}}})},61980:(e,r,t)=>{t.r(r),t.d(r,{config:()=>S,default:()=>E,routeModule:()=>g});var s={};t.r(s),t.d(s,{default:()=>p});var o=t(89947),n=t(2706),a=t(96762),i=t(4820),u=t(67619),l=t(69842),d=t(27825),c=t.n(d),f=t(48786);t(29021),t(33873);let{execSync:_}=t(79646);async function p(e,r){if("GET"!==e.method)return r.json({msg:"Failed!"});let t=await (0,l.A)(e,r);if(!t)return r.json({success:!1,msg:"Unauthorized"});let s=e.query.dbUserId;e.query.delegated;try{if(!s)throw Error("No Root user Id Found!");let e=await (0,f.A)("SELECT * FROM datasquirel.delegated_user_tables WHERE delegated_user_id=? AND root_user_id=?",[String(t.id),String(s)]),o=(0,i.A)({userId:String(s)});if(!o)throw Error("Root user schema not found!");let n=[];e.forEach(e=>{let r=n.findIndex(r=>r.dbSlug===e.database),t=o.find(r=>r.dbSlug===e.database);if(t){let s=n[r]?n[r]:{...t,tables:[]},o=c().cloneDeep(t.tables).find(r=>r.tableName===e.table);o&&n[r]?n[r]={...s,tables:[...n[r].tables,o]}:o&&!n[r]&&n.push({...t,tables:[o]})}}),r.json({success:!0,payload:n})}catch(e){console.log("ERROR:",e.message),(0,u.default)({component:"/api/get-authorized-databases",message:e.message,user:t}),r.json({success:!1,msg:e.message})}}let E=(0,a.M)(s,"default"),S=(0,a.M)(s,"config"),g=new o.PagesAPIRouteModule({definition:{kind:n.A.PAGES_API,page:"/api/get-authorized-databases",pathname:"/api/get-authorized-databases",bundlePath:"",filename:""},userland:s})},69842:(e,r,t)=>{t.d(r,{A:()=>u});var s=t(48786),o=t(95969),n=t(29021),a=t.n(n),i=t(89159);async function u(e,r,t){try{let{keyCookieName:n,csrfCookieName:u}=(0,i.A)();if(!e.cookies?.[n]?.match(/./))throw Error("Request Cookies not Found!");let l=(0,o.A)({encryptedString:e.cookies[n]});if(!l)throw Error("Couldn't decrypt auth cookie!");let d=JSON.parse(l);if(!d.csrf_k)throw Error("No `csrf_k` present in user Object!");if(t&&!Object.keys(e.headers).find(e=>e.includes(d.csrf_k)))throw Error("Deep Auth Failed. `csrf` key not found in request headers!");let c=process.env.DSQL_USER_LOGIN_KEYS_PATH;if(!c)throw Error("DSQL_USER_LOGIN_KEYS_PATH env variable not found. Please set this variable.");if(t&&!a().existsSync(`${c}/${d.csrf_k}`))throw Error("Key not found in Auth Keys Folder!");if(0==d.verification_status&&!t){let e=await (0,s.A)(`SELECT verification_status FROM users WHERE id='${d.id}'`);e&&e[0]&&1==e[0].verification_status&&r.setHeader("Set-Cookie",["user_refresh=1"])}if(d?.date&&Date.now()-d.date>6048e5)throw Error("Key expired!");return d}catch(e){return console.log(`userAuth Error: ${e.message}`),null}}},89159:(e,r,t)=>{t.d(r,{A:()=>s});function s(e){let r=process.env.DSQL_COOKIES_PREFIX||"dsql_",t=process.env.DSQL_COOKIES_KEY_NAME||"key",s=process.env.DSQL_COOKIES_CSRF_NAME||"csrf",o=process.env.DSQL_COOKIES_ONE_TIME_CODE_NAME||"one-time-code",n=e?.database?.replace(/^datasquirel_user_\d+_/,"")||process.env.DSQL_DB_NAME?.replace(/^datasquirel_user_\d+_/,""),a=r;e?.userId&&(a+=`user_${e.userId}_`),n&&(a+=`${n}_`),a+=t;let i=r;e?.userId&&(i+=`user_${e.userId}_`),n&&(i+=`${n}_`),i+=s;let u=r;return e?.userId&&(u+=`user_${e.userId}_`),n&&(u+=`${n}_`),{keyCookieName:a,csrfCookieName:i,oneTimeCodeName:u+=o}}},4820:(e,r,t)=>{t.d(r,{A:()=>u});var s=t(67619),o=t(29021),n=t.n(o),a=t(33873),i=t.n(a);function u({userId:e}){try{let r=i().resolve(process.cwd(),`${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${e}/main.json`);return JSON.parse(n().readFileSync(r,"utf-8"))}catch(e){return(0,s.default)({component:"grabUserSchemaData",message:e.message}),null}}},67619:(e,r,t)=>{t.r(r),t.d(r,{default:()=>n});var s=t(29021),o=t.n(s);async function n({user:e,message:r,component:t,noMail:s,req:n}){let a=new Date,i=(()=>{if(!n)return null;try{let e=n.headers["x-forwarded-for"],r=n.headers["x-real-ip"],t=n.headers["cf-connecting-ip"],s=Array.isArray(e)?e[0]:e?.split(",")[0],o=t||s||r||n.socket.remoteAddress;if(!o)return null;return String(o)}catch(e){return null}})();try{let s=`🚀 SERVER ERROR =========================== Error Message: ${r} Component: ${t}`;e?.id&&e?.first_name&&e?.last_name&&e?.email&&(s+=` User Id: ${e?.id} User Name: ${e?.first_name} ${e?.last_name} User Email: ${e?.email}`),n?.url&&(s+=` URL: ${n.url}`),n?.body&&(s+=` Request Body: ${JSON.stringify(n.body,null,4)}`),i&&(s+=` IP: ${i}`),s+=` Date: ${a.toDateString()} ========================================`,o().existsSync("./.tmp/error.log")||o().writeFileSync("./.tmp/error.log","","utf-8");let u=o().readFileSync("./.tmp/error.log","utf-8");o().writeFileSync("./.tmp/error.log",s),o().appendFileSync("./.tmp/error.log",` ${u}`)}catch(e){console.log("Server Error Reporting Error:",e.message)}}},95969:(e,r,t)=>{t.d(r,{A:()=>n});var s=t(55511),o=t(79428);function n({encryptedString:e,encryptionKey:r,encryptionSalt:t}){if(!e?.match(/./))return console.log("Encrypted string is invalid"),e;let n=r||process.env.DSQL_ENCRYPTION_PASSWORD,a=t||process.env.DSQL_ENCRYPTION_SALT,i=process.env.DSQL_ENCRYPTION_KEY_LENGTH?Number(process.env.DSQL_ENCRYPTION_KEY_LENGTH):24;if(!n?.match(/.{8,}/))return console.log("Decrption key is invalid"),e;if(!a?.match(/.{8,}/))return console.log("Decrption salt is invalid"),e;let u=(0,s.scryptSync)(n,a,i),l=o.Buffer.alloc(16,0),d=(0,s.createDecipheriv)("aes-192-cbc",u,l);try{let r=d.update(e,"hex","utf8");return r+=d.final("utf8")}catch(r){return console.log("Error in decrypting =>",r.message),e}}},48786:(e,r,t)=>{t.d(r,{A:()=>s});async function s(...e){let r=global.DSQL_DB_CONN;try{if(!r)throw Error("No Connection provided to DB_HANDLER function!");let t=await r.query(...e);return JSON.parse(JSON.stringify(t))}catch(e){return console.log("DB Error =>",e),{success:!1,error:e.message}}finally{await r?.end()}}},2706:(e,r)=>{Object.defineProperty(r,"A",{enumerable:!0,get:function(){return t}});var t=function(e){return e.PAGES="PAGES",e.PAGES_API="PAGES_API",e.APP_PAGE="APP_PAGE",e.APP_ROUTE="APP_ROUTE",e.IMAGE="IMAGE",e}({})},89947:(e,r,t)=>{e.exports=t(75600)}};var r=require("../../webpack-api-runtime.js");r.C(e);var t=r(r.s=61980);module.exports=t})();