dsql-admin/dsql-app/.local_dist/server/pages/api/user-schema/replace-database-schema.js
Benjamin Toby 885c11f674 Updates
2025-01-14 07:42:13 +01:00

1 line
4.5 KiB
JavaScript

"use strict";(()=>{var e={};e.id=4105,e.ids=[4105],e.modules={75600:e=>{e.exports=require("next/dist/compiled/next-server/pages-api.runtime.prod.js")},79428:e=>{e.exports=require("buffer")},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}}})},55096:(e,r,t)=>{t.r(r),t.d(r,{config:()=>A,default:()=>h,routeModule:()=>S});var s={};t.r(s),t.d(s,{default:()=>p});var a=t(89947),n=t(2706),o=t(96762),i=t(29021),u=t.n(i),c=t(69842),d=t(67619),l=t(48786),_=t(33873),f=t.n(_),E=t(76856);async function p(e,r){if("POST"!==e.method)return r.json({user:null,msg:"Registration Failed!"});let t=await (0,c.A)(e,r,!0);if(!t)return r.json({success:!1,msg:"Unauthorized"});let s=e.body.schema,a=e.body.dbId;try{let e=await (0,l.A)("SELECT * FROM user_databases WHERE user_id = ? AND id = ?",[t.id,a]);if(!e?.[0])throw Error("Invalid Database");let n=e[0],o=f().resolve(process.cwd(),`${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${t.id}/main.json`),i=JSON.parse(u().readFileSync(o,"utf-8")),c=i.find(e=>e.dbFullName==n.db_full_name);if(!c)throw Error("This Database doesn't exist");if(c.tables=s.tables,c.childrenDatabases)for(let e=0;e<c.childrenDatabases.length;e++){let{dbFullName:r}=c.childrenDatabases[e],t=i.find(e=>e.dbFullName===r);if(!t||!t.tables){c.childrenDatabases?.splice(e,1);continue}t.tables=s.tables}u().writeFileSync(`${o}`,JSON.stringify(i),"utf8"),await (0,E.A)({userId:t.id,targetDatabase:n.db_full_name}),r.json({success:!0})}catch(s){(0,d.A)({component:"/api/acceptUserInvitation/catch-error",message:s.message,user:t,req:e}),r.json({success:!1})}}let h=(0,o.M)(s,"default"),A=(0,o.M)(s,"config"),S=new a.PagesAPIRouteModule({definition:{kind:n.A.PAGES_API,page:"/api/user-schema/replace-database-schema",pathname:"/api/user-schema/replace-database-schema",bundlePath:"",filename:""},userland:s})},69842:(e,r,t)=>{t.d(r,{A:()=>u});var s=t(48786),a=t(95969),n=t(29021),o=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 c=(0,a.A)({encryptedString:e.cookies[n]});if(!c)throw Error("Couldn't decrypt auth cookie!");let d=JSON.parse(c);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 console.log("req.headers",e.headers),console.log("userObject",d),Error("Deep Auth Failed. `csrf` key not found in request headers!");let l=process.env.DSQL_USER_LOGIN_KEYS_PATH;if(!l)throw Error("DSQL_USER_LOGIN_KEYS_PATH env variable not found. Please set this variable.");if(t&&!o().existsSync(`${l}/${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",a=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+_/,""),o=r;e?.userId&&(o+=`user_${e.userId}_`),n&&(o+=`${n}_`),o+=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:o,csrfCookieName:i,oneTimeCodeName:u+=a}}},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=e=>r(r.s=e),s=r.X(0,[2282],()=>t(55096));module.exports=s})();