@base44-preview/cli 0.0.50-pr.475.145bd51 → 0.0.50-pr.475.1c343ca

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cli/index.js CHANGED
@@ -253321,7 +253321,9 @@ class Validator {
253321
253321
  }
253322
253322
 
253323
253323
  // src/cli/dev/dev-server/db/database.ts
253324
+ var PRIVATE_COLLECTION_PREFIX = "$";
253324
253325
  var USER_COLLECTION = "user";
253326
+ var PRIVATE_USER_COLLECTION = PRIVATE_COLLECTION_PREFIX + USER_COLLECTION;
253325
253327
 
253326
253328
  class Database {
253327
253329
  collections = new Map;
@@ -253343,6 +253345,7 @@ class Database {
253343
253345
  this.schemas.set(USER_COLLECTION, this.buildUserSchema(userEntity));
253344
253346
  const collection = new import_nedb.default;
253345
253347
  this.collections.set(USER_COLLECTION, collection);
253348
+ this.collections.set(PRIVATE_USER_COLLECTION, new import_nedb.default);
253346
253349
  const userInfo = await readAuth();
253347
253350
  const now = getNowISOTimestamp();
253348
253351
  await collection.insertAsync({
@@ -253384,7 +253387,9 @@ class Database {
253384
253387
  return this.collections.get(this.normalizeName(name2));
253385
253388
  }
253386
253389
  getCollectionNames() {
253387
- return Array.from(this.collections.keys());
253390
+ return Array.from(this.collections.keys()).filter((name2) => {
253391
+ return !name2.startsWith(PRIVATE_COLLECTION_PREFIX);
253392
+ });
253388
253393
  }
253389
253394
  dropAll() {
253390
253395
  for (const collection of this.collections.values()) {
@@ -253461,32 +253466,37 @@ var createJwtToken = (email3) => {
253461
253466
  expiresIn: "360d"
253462
253467
  });
253463
253468
  };
253464
- var UserRegiterSchema = object({
253465
- id: string2(),
253466
- email: email2(),
253467
- otpCode: string2().length(6),
253468
- password: string2().min(8),
253469
- createdAt: number2().min(1)
253470
- });
253469
+ var LoginBody = object({ email: email2(), password: string2() });
253470
+ var VerifyOtpBody = object({ email: email2(), otp_code: string2() });
253471
253471
  function createAuthRouter(db2, logger2) {
253472
253472
  const router = import_express2.Router({ mergeParams: true });
253473
- const userRegitrPendingMap = new Map;
253474
253473
  const parseBody = import_express2.json();
253475
253474
  router.post("/login", parseBody, async (req, res) => {
253476
- const { email: email3, password: _password } = req.body;
253475
+ const { email: email3, password } = LoginBody.parse(req.body);
253477
253476
  const result = await db2.getCollection(USER_COLLECTION)?.findOneAsync({ email: email3 });
253478
253477
  if (result) {
253479
- res.json({
253480
- access_token: createJwtToken(email3),
253481
- success: true,
253482
- user: {}
253483
- });
253478
+ const privateUserData = await db2.getCollection(PRIVATE_USER_COLLECTION)?.findOneAsync({ email: email3 });
253479
+ if (result.role === "admin" || privateUserData?.password === password) {
253480
+ res.json({
253481
+ access_token: createJwtToken(email3),
253482
+ success: true,
253483
+ user: {}
253484
+ });
253485
+ } else {
253486
+ res.status(400).json({
253487
+ detail: "Invalid email or password",
253488
+ error_type: "HTTPException",
253489
+ message: "Invalid email or password",
253490
+ request_id: null,
253491
+ traceback: ""
253492
+ });
253493
+ }
253484
253494
  return;
253485
253495
  }
253486
253496
  res.status(401).json({ error: "Unauthorized" });
253487
253497
  });
253488
253498
  router.post("/register", parseBody, async (req, res) => {
253489
- const { email: email3, password } = req.body;
253499
+ const { email: email3, password } = LoginBody.parse(req.body);
253490
253500
  if ((password || "").length < 8) {
253491
253501
  res.status(400).json({
253492
253502
  detail: "Password must be at least 8 characters long",
@@ -253508,15 +253518,30 @@ function createAuthRouter(db2, logger2) {
253508
253518
  });
253509
253519
  return;
253510
253520
  }
253511
- const otpCode = generateCode();
253512
- const id2 = nanoid3();
253513
- userRegitrPendingMap.set(email3, {
253514
- id: id2,
253515
- email: email3,
253516
- otpCode,
253517
- password,
253518
- createdAt: +Date.now()
253521
+ const privateUserCollection = db2.getCollection(PRIVATE_USER_COLLECTION);
253522
+ const privateUserData = await privateUserCollection?.findOneAsync({
253523
+ email: email3
253519
253524
  });
253525
+ const otpCode = generateCode();
253526
+ const id2 = privateUserData ? privateUserData.id : nanoid3();
253527
+ if (!privateUserData) {
253528
+ await privateUserCollection?.insertAsync({
253529
+ id: id2,
253530
+ email: email3,
253531
+ otpCode,
253532
+ password,
253533
+ createdAt: Date.now()
253534
+ });
253535
+ } else {
253536
+ await privateUserCollection?.updateAsync({
253537
+ email: email3
253538
+ }, {
253539
+ $set: {
253540
+ otpCode,
253541
+ createdAt: Date.now()
253542
+ }
253543
+ });
253544
+ }
253520
253545
  logger2.log(theme.styles.info(`
253521
253546
  In order to complete registration use this verification code: ${otpCode}
253522
253547
  `));
@@ -253527,16 +253552,24 @@ In order to complete registration use this verification code: ${otpCode}
253527
253552
  });
253528
253553
  });
253529
253554
  router.post("/verify-otp", parseBody, async (req, res) => {
253530
- const { email: email3, otp_code } = req.body;
253531
- const userData = userRegitrPendingMap.get(email3);
253532
- if (userData && userData.otpCode === otp_code) {
253533
- if (+Date.now() - userData.createdAt < 10 * 60 * 1000) {
253555
+ const { email: email3, otp_code } = VerifyOtpBody.parse(req.body);
253556
+ const privateUserCollection = db2.getCollection(PRIVATE_USER_COLLECTION);
253557
+ const privateUserData = await privateUserCollection?.findOneAsync({
253558
+ email: email3
253559
+ });
253560
+ if (privateUserData && privateUserData.otpCode === otp_code) {
253561
+ if (+Date.now() - privateUserData.createdAt < 10 * 60 * 1000) {
253562
+ await privateUserCollection?.updateAsync({
253563
+ email: email3
253564
+ }, {
253565
+ $unset: { otpCode: true }
253566
+ });
253534
253567
  const collection = db2.getCollection(USER_COLLECTION);
253535
253568
  const now = getNowISOTimestamp();
253536
253569
  const nameFromEmailMatch = /^([^@]+)/.exec(email3);
253537
253570
  const fullName = nameFromEmailMatch ? nameFromEmailMatch[1] : email3;
253538
253571
  await collection?.insertAsync({
253539
- id: userData.id,
253572
+ id: privateUserData.id,
253540
253573
  email: email3,
253541
253574
  full_name: fullName,
253542
253575
  is_service: false,
@@ -253548,7 +253581,7 @@ In order to complete registration use this verification code: ${otpCode}
253548
253581
  updated_date: now
253549
253582
  });
253550
253583
  res.json({
253551
- id: userData.id,
253584
+ id: privateUserData.id,
253552
253585
  access_token: createJwtToken(email3),
253553
253586
  message: "Email verified successfully. You are now logged in.",
253554
253587
  success: true
@@ -260360,4 +260393,4 @@ export {
260360
260393
  CLIExitError
260361
260394
  };
260362
260395
 
260363
- //# debugId=7F65D5426750E8BD64756E2164756E21
260396
+ //# debugId=87279C94ABAC181364756E2164756E21