"use strict";(()=>{var e={};e.id=3303,e.ids=[3303],e.modules={27825:e=>{e.exports=require("lodash")},75600:e=>{e.exports=require("next/dist/compiled/next-server/pages-api.runtime.prod.js")},21572:e=>{e.exports=require("nodemailer")},72583:e=>{e.exports=require("sanitize-html")},9973:e=>{e.exports=require("serverless-mysql")},79428:e=>{e.exports=require("buffer")},55511:e=>{e.exports=require("crypto")},29021:e=>{e.exports=require("fs")},81630:e=>{e.exports=require("http")},33873:e=>{e.exports=require("path")},95122:(e,r,t)=>{t.r(r),t.d(r,{config:()=>D,default:()=>A,routeModule:()=>R});var s={};t.r(s),t.d(s,{default:()=>b});var i=t(89947),a=t(2706),n=t(96762),o=t(51348),l=t.n(o),u=t(44767),d=t.n(u),_=t(48225),m=t.n(_),c=t(59474),p=t.n(c),g=t(91766),f=t.n(g),S=t(31341),E=t.n(S),h=t(14533),y=t.n(h);let L=t(29021),v=t(20503);async function b(e,r){if("POST"===e.method){let t=e.body,s=t.email,i=t.password,a=t.query;if(s.match(/ /))return r.json({user:null,msg:"Invalid Email"});try{let t=process.env.DSQL_USER_LOGIN_KEYS_PATH;if(!t)throw Error("loginUser error: process.env.DSQL_USER_LOGIN_KEYS_PATH not present");L.existsSync(t)||L.mkdirSync(t,{recursive:!0});let n=e.headers["x-real-ip"]||e.headers["x-forwarded-for"]||e.connection.remoteAddress,o=process.env.DSQL_HOST||"https://datasquirel.com",u=y()({encryptionKey:process.env.DSQL_ENCRYPTION_PASSWORD||"",password:i});if(i?.match(/ /)||s?.match(/ /))return r.json({msg:"Failed!",user:null});let _=await l()("SELECT * FROM users WHERE email=? OR username=?",[s,s]);if(!_||!_[0])return r.json({msg:"No user found",user:null});let m=!1;if(_&&_[0]&&(m=u===_[0].password),!m)return await f()(),p()({to:process.env.DSQL_CONTACT_EMAIL,subject:`Failed Login from ${s}`,html:`<h1><u>${s}</u> tried to login with a wrong password</h1><p><b>${s}</b> attempted a login on ${o} from ${n} with a wrong password.</p>`}),r.json({msg:"Wrong Password, Wrong Social Media Credentials",user:null});let c=Math.random().toString(36).substring(2)+"-"+Math.random().toString(36).substring(2),g={id:_[0].id,stripe_id:_[0].stripe_id||"",first_name:_[0].first_name,last_name:_[0].last_name,username:_[0].username,email:_[0].email,social_id:_[0].social_id,image:_[0].image,image_thumbnail:_[0].image_thumbnail,verification_status:_[0].verification_status,social_login:_[0].social_login,social_platform:_[0].social_platform,csrf_k:c,logged_in_status:!0,date:Date.now()},S=v({data:JSON.stringify(g)}),{keyCookieName:h,csrfCookieName:b}=E()();r.setHeader("Set-Cookie",[`${h}=${S};samesite=strict;path=/;HttpOnly=true;Secure=true`,`${b}=${c};samesite=strict;path=/;HttpOnly=true`]),L.appendFile("./.tmp/userLoginLogs.log",`${_[0].first_name} ${_[0].last_name} Logged in from ${_[0].social_login?_[0].social_platform:"Regular Email Login"} at ${Date()} 


`,"utf8",e=>{e&&console.log("Loging log write error")}),L.writeFileSync(`${t}/${c}`,JSON.stringify(g)),a?.invite&&d()({query:a,user:{...g,phone:_[0].phone}}),await f()(),p()({to:process.env.DSQL_CONTACT_EMAIL,subject:`${s} Logged in successfully`,html:`<h1><u>${s}</u> successfully logged in</h1><p><b>${s}</b> successfully logged in on ${o} from ${n} on ${Date().substring(0,16)}.</p>`}),r.json({msg:"User Found",user:{auth:!0,userid:_[0].id,csrf_k:c,stripe_id:_[0].stripe_id||""},userPayload:g})}catch(e){console.log("Login Error =>",e.message),m()({component:"/api/loginUser/main-catch-error",message:e.message,user:{email:s}})}}else r.json({msg:"Oops ..."})}let A=(0,n.M)(s,"default"),D=(0,n.M)(s,"config"),R=new i.PagesAPIRouteModule({definition:{kind:a.A.PAGES_API,page:"/api/loginUser",pathname:"/api/loginUser",bundlePath:"",filename:""},userland:s})},91766:e=>{e.exports=async function(e){return await new Promise((r,t)=>{setTimeout(()=>{r(!0)},e||1e3)}),!0}},44767:(e,r,t)=>{let s=t(48225),i=t(51348),a=t(47670),n=t(44900);e.exports=async function({query:e,user:r,useLocal:t}){try{let s=t?n:i,{invite:o,database_access:l,priviledge:u,email:d}=e,_=[o,d],m=await s("SELECT date_created_code FROM invitations WHERE inviting_user_id=? AND invited_user_email=?",_);if(!m||!m[0])throw Error("No Invitation Found");let c=[o],p=await s("SELECT first_name,last_name,email FROM users WHERE id=?",c);if(p?.[0]){let e=await s("SELECT email FROM user_users WHERE user_id=? AND invited_user_id=? AND user_type='admin' AND email=?",[o,r.id,d]);if(e?.[0])console.log("User already added");else{a({dbFullName:"datasquirel",tableName:"user_users",data:{user_id:o,invited_user_id:r.id,database_access:l,first_name:r.first_name,last_name:r.last_name,phone:r.phone,email:r.email,username:r.username,user_type:"admin",user_priviledge:u,image:r.image,image_thumbnail:r.image_thumbnail},useLocal:t});let e=await s("SELECT db_tables_data FROM invitations WHERE inviting_user_id=? AND invited_user_email=?",[o,d]);if(await s("DELETE FROM delegated_user_tables WHERE root_user_id=? AND delegated_user_id=?",[o,r.id]),e&&e[0]){let s=e[0].db_tables_data.split("|");for(let e=0;e<s.length;e++){let[i,n]=s[e].split("-");await a({dbFullName:"datasquirel",tableName:"delegated_user_tables",data:{delegated_user_id:r.id,root_user_id:o,database:i,table:n,priviledge:u},useLocal:t})}}}await s("UPDATE invitations SET invitation_status='Accepted' WHERE inviting_user_id=? AND invited_user_email=?",[o,d])}}catch(e){s({component:"addAdminUserOnLogin",message:e.message,user:r})}}},31341:e=>{e.exports=function(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",i=r;e?.userId&&(i+=`user_${e.userId}_`),e?.database&&(i+=`${e.database}_`),i+=t;let a=r;return e?.userId&&(a+=`user_${e.userId}_`),e?.database&&(a+=`${e.database}_`),{keyCookieName:i,csrfCookieName:a+=s}}},59474:(e,r,t)=>{let s=t(29021),i=t(21572).createTransport({host:process.env.DSQL_MAIL_HOST,port:465,secure:!0,auth:{user:process.env.DSQL_MAIL_EMAIL,pass:process.env.DSQL_MAIL_PASSWORD}});e.exports=async function({to:e,subject:r,text:t,html:a,alias:n}){let o;if(!process.env.DSQL_MAIL_HOST||!process.env.DSQL_MAIL_EMAIL||!process.env.DSQL_MAIL_PASSWORD)return null;let l=(n?.match(/support/i),process.env.DSQL_MAIL_EMAIL);if(!s.existsSync("./email/index.html"))return;let u=s.readFileSync("./email/index.html","utf8").replace(/{{email_body}}/,a||"").replace(/{{issue_date}}/,Date().substring(0,24));try{let s={};s.from=`"Datasquirel" <${l}>`,s.sender=l,n&&(s["replyTo "]=l),s.to=e,s.subject=r,s.text=t,s.html=u,o=await i.sendMail(s)}catch(e){console.log("ERROR in handleNodemailer Function =>",e.message)}return o}},48225:(e,r,t)=>{let s=t(29021),{IncomingMessage:i}=t(81630);e.exports=async function({user:e,message:r,component:t,noMail:i,req:a}){let n=new Date,o=(()=>{if(!a)return null;try{let e=a.headers["x-forwarded-for"],r=a.headers["x-real-ip"],t=a.headers["cf-connecting-ip"],s=Array.isArray(e)?e[0]:e?.split(",")[0],i=t||s||r||a.socket.remoteAddress;if(!i)return null;return String(i)}catch(e){return null}})();try{let i=`🚀 SERVER ERROR ===========================
Error Message: ${r}
Component: ${t}`;e?.id&&e?.first_name&&e?.last_name&&e?.email&&(i+=`
User Id: ${e?.id}
User Name: ${e?.first_name} ${e?.last_name}
User Email: ${e?.email}`),a?.url&&(i+=`
URL: ${a.url}`),a?.body&&(i+=`
Request Body: ${JSON.stringify(a.body,null,4)}`),o&&(i+=`
IP: ${o}`),i+=`
Date: ${n.toDateString()}
========================================`,s.existsSync("./.tmp/error.log")||s.writeFileSync("./.tmp/error.log","","utf-8");let l=s.readFileSync("./.tmp/error.log","utf-8");s.writeFileSync("./.tmp/error.log",i),s.appendFileSync("./.tmp/error.log",`




${l}`)}catch(e){console.log("Server Error Reporting Error:",e.message)}}},14533:(e,r,t)=>{let{createHmac:s}=t(55511);e.exports=function({password:e,encryptionKey:r}){let t=r||process.env.DSQL_ENCRYPTION_PASSWORD;if(!t?.match(/.{8,}/))throw Error("Encryption key is invalid");let i=s("sha512",t);return i.update(e),i.digest("base64")}}};var r=require("../../webpack-api-runtime.js");r.C(e);var t=e=>r(r.s=e),s=r.X(0,[4050],()=>t(95122));module.exports=s})();