Browse Source

fix updating user and domain

Pouria Ezzati 1 year ago
parent
commit
eed2b62fe3
3 changed files with 33 additions and 21 deletions
  1. 13 11
      server/handlers/auth.handler.js
  2. 13 7
      server/handlers/domains.handler.js
  3. 7 3
      server/queries/user.queries.js

+ 13 - 11
server/handlers/auth.handler.js

@@ -139,12 +139,16 @@ function login(req, res) {
 
 async function verify(req, res, next) {
   if (!req.params.verificationToken) return next();
+
+  const user = await query.user.find({
+    verification_token: req.params.verificationToken,
+    verification_expires: [">", new Date().toISOString()]
+  });
+
+  if (!user) return next();
   
-  const [user] = await query.user.update(
-    {
-      verification_token: req.params.verificationToken,
-      verification_expires: [">", new Date().toISOString()]
-    },
+  const [updatedUser] = await query.user.update(
+    { id: user.id },
     {
       verified: true,
       verification_token: null,
@@ -152,7 +156,7 @@ async function verify(req, res, next) {
     }
   );
   
-  if (user) {
+  if (updatedUser) {
     const token = utils.signToken(user);
     utils.deleteCurrentToken(res);
     utils.setToken(res, token);
@@ -317,16 +321,14 @@ async function changeEmail(req, res, next) {
   
   if (changeEmailToken) {
     const foundUser = await query.user.find({
-      change_email_token: changeEmailToken
+      change_email_token: changeEmailToken,
+      change_email_expires: [">", new Date().toISOString()]
     });
   
     if (!foundUser) return next();
   
     const [user] = await query.user.update(
-      {
-        change_email_token: changeEmailToken,
-        change_email_expires: [">", new Date().toISOString()]
-      },
+      { id: foundUser.id },
       {
         change_email_token: null,
         change_email_expires: null,

+ 13 - 7
server/handlers/domains.handler.js

@@ -26,19 +26,25 @@ async function add(req, res) {
 };
 
 async function remove(req, res) {
-  const [domain] = await query.domain.update(
-    {
-      uuid: req.params.id,
-      user_id: req.user.id
-    },
+  const domain = await query.domain.find({
+    uuid: req.params.id,
+    user_id: req.user.id
+  });
+
+  if (!domain) {
+    throw new CustomError("Could not delete the domain.", 500);
+  }
+  
+  const [updatedDomain] = await query.domain.update(
+    { id: domain.id },
     { user_id: null }
   );
 
-  if (!domain) {
+  if (!updatedDomain) {
     throw new CustomError("Could not delete the domain.", 500);
   }
 
-  redis.remove.domain(domain);
+  redis.remove.domain(updatedDomain);
 
   if (req.isHTML) {
     const domains = (await query.domain.get({ user_id: req.user.id })).map(sanitize.domain);

+ 7 - 3
server/queries/user.queries.js

@@ -10,8 +10,13 @@ async function find(match) {
     const cachedUser = await redis.client.get(key);
     if (cachedUser) return JSON.parse(cachedUser);
   }
-  
-  const user = await knex("users").where(match).first();
+
+  const query = knex("users");
+  Object.entries(match).forEach(([key, value]) => {
+    query.andWhere(key, ...(Array.isArray(value) ? value : [value]));
+  });
+
+  const user = await query.first();
   
   if (user) {
     const emailKey = redis.key.user(user.email);
@@ -24,7 +29,6 @@ async function find(match) {
   }
   
   return user;
-
 }
 
 async function add(params, user) {