From 4fe1625f8d0741b46a81973484a83aa13bf98246 Mon Sep 17 00:00:00 2001
From: laurentlemercier <22855554+laurentlemercier@users.noreply.github.com>
Date: Sun, 11 Feb 2024 22:56:04 +0100
Subject: [PATCH 1/8] Update reset-password.ts

You can use a variable in order to set password value :
docker exec -e PASSWORD=value -it dockge npm run reset-password
and you still have the standard behavior when no PASSWORD provided :
docker exec -it dockge npm run reset-password
---
 extra/reset-password.ts | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)

diff --git a/extra/reset-password.ts b/extra/reset-password.ts
index 696fb4c..d7364a6 100644
--- a/extra/reset-password.ts
+++ b/extra/reset-password.ts
@@ -38,10 +38,22 @@ export const main = async () => {
 
             console.log("Found user: " + user.username);
 
-            while (true) {
-                let password = await question("New Password: ");
-                let confirmPassword = await question("Confirm New Password: ");
+            let password = "";
+            let confirmPassword = " ";
 
+            while (true) {
+                if (process.env.PASSWORD) {
+                   console.log("Found password : " + process.env.PASSWORD) ;
+                   password = process.env.PASSWORD ;
+                   confirmPassword = process.env.PASSWORD ;
+                } else {
+                   console.log("No found password: " ) ;
+                   password = await question("New Password: ");
+                   confirmPassword = await question("Confirm New Password: ");
+                }
+
+                // console.log("Password to be set :" + password);
+            
                 if (password === confirmPassword) {
                     await User.resetPassword(user.id, password);
 

From be72cdbffe5b5d3148186a534173cea76fa67164 Mon Sep 17 00:00:00 2001
From: laurentlemercier <22855554+laurentlemercier@users.noreply.github.com>
Date: Sun, 11 Feb 2024 22:58:08 +0100
Subject: [PATCH 2/8] Update compose.yaml

Usefull for interactive action within the container
---
 compose.yaml | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/compose.yaml b/compose.yaml
index 6c13fd0..c9dde89 100644
--- a/compose.yaml
+++ b/compose.yaml
@@ -21,3 +21,6 @@ services:
     environment:
       # Tell Dockge where is your stacks directory
       - DOCKGE_STACKS_DIR=/opt/stacks
+    # Usefull for interactive action within the container
+    stdin_open: true # docker run -i
+    tty: true        # docker run -t

From ff922232c1e52253ed42cd479fb5c90d02176bbc Mon Sep 17 00:00:00 2001
From: laurentlemercier <22855554+laurentlemercier@users.noreply.github.com>
Date: Sun, 11 Feb 2024 23:09:46 +0100
Subject: [PATCH 3/8] Update reset-password.ts

---
 extra/reset-password.ts | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/extra/reset-password.ts b/extra/reset-password.ts
index d7364a6..e5c3d55 100644
--- a/extra/reset-password.ts
+++ b/extra/reset-password.ts
@@ -43,13 +43,13 @@ export const main = async () => {
 
             while (true) {
                 if (process.env.PASSWORD) {
-                   console.log("Found password : " + process.env.PASSWORD) ;
-                   password = process.env.PASSWORD ;
-                   confirmPassword = process.env.PASSWORD ;
+                    console.log("Found password : " + process.env.PASSWORD) ;
+                    password = process.env.PASSWORD ;
+                    confirmPassword = process.env.PASSWORD ;
                 } else {
-                   console.log("No found password: " ) ;
-                   password = await question("New Password: ");
-                   confirmPassword = await question("Confirm New Password: ");
+                    console.log("No found password: " ) ;
+                    password = await question("New Password: ");
+                    confirmPassword = await question("Confirm New Password: ");
                 }
 
                 // console.log("Password to be set :" + password);

From 9615b844c3be28f952da3063a19565d01d389089 Mon Sep 17 00:00:00 2001
From: laurentlemercier <22855554+laurentlemercier@users.noreply.github.com>
Date: Sun, 11 Feb 2024 23:11:39 +0100
Subject: [PATCH 4/8] Update reset-password.ts

---
 extra/reset-password.ts | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/extra/reset-password.ts b/extra/reset-password.ts
index e5c3d55..eae0c32 100644
--- a/extra/reset-password.ts
+++ b/extra/reset-password.ts
@@ -53,7 +53,7 @@ export const main = async () => {
                 }
 
                 // console.log("Password to be set :" + password);
-            
+
                 if (password === confirmPassword) {
                     await User.resetPassword(user.id, password);
 

From c451067ac47735bf1efcd86432a2f9c4d8632500 Mon Sep 17 00:00:00 2001
From: laurentlemercier <22855554+laurentlemercier@users.noreply.github.com>
Date: Sun, 10 Mar 2024 23:18:50 +0100
Subject: [PATCH 5/8] Update reset-password.ts

Set User and Password (previous version had a bug).
docker exec --e USER=uservalue -e PASSWORD=passwordvalue -it dockge npm run reset-password
---
 extra/reset-password.ts | 92 ++++++++++++++++++++++++++---------------
 1 file changed, 58 insertions(+), 34 deletions(-)

diff --git a/extra/reset-password.ts b/extra/reset-password.ts
index eae0c32..1154474 100644
--- a/extra/reset-password.ts
+++ b/extra/reset-password.ts
@@ -6,6 +6,7 @@ import { DockgeServer } from "../backend/dockge-server";
 import { log } from "../backend/log";
 import { io } from "socket.io-client";
 import { BaseRes } from "../common/util-common";
+import { generatePasswordHash } from "../backend/password-hash";
 
 console.log("== Dockge Reset Password Tool ==");
 
@@ -29,54 +30,76 @@ export const main = async () => {
     }
 
     try {
+		let user ;
         // No need to actually reset the password for testing, just make sure no connection problem. It is ok for now.
+		
         if (!process.env.TEST_BACKEND) {
-            const user = await R.findOne("user");
-            if (! user) {
-                throw new Error("user not found, have you installed?");
-            }
+            user = await R.findOne("user");
+            				
+            if (! user )  {
+				if ( !process.env.USER ) {
+                    throw new Error("user not found or provided, have you installed? Try to set USER env variable ...");
+				} else {
+					console.log("Trying to initialise user : " + process.env.USER);
+						user = R.dispense("user");
+						user.username = process.env.USER;	
+						user.password = generatePasswordHash(process.env.PASSWORD);
+						await R.store(user);						
+						console.log("User/Password set successfully");
+						
+						// Reset all sessions by reset jwt secret
+						await server.initJWTSecret();
+						console.log("JWT reset successfully.");
 
-            console.log("Found user: " + user.username);
+						// Disconnect all other socket clients of the user
+						await disconnectAllSocketClients(user.username, user.password);
+						console.log("You may have to restart");
+						exit;
+				}
+			}
+            }
 
             let password = "";
             let confirmPassword = " ";
 
+
             while (true) {
-                if (process.env.PASSWORD) {
-                    console.log("Found password : " + process.env.PASSWORD) ;
-                    password = process.env.PASSWORD ;
-                    confirmPassword = process.env.PASSWORD ;
-                } else {
-                    console.log("No found password: " ) ;
-                    password = await question("New Password: ");
-                    confirmPassword = await question("Confirm New Password: ");
-                }
 
-                // console.log("Password to be set :" + password);
+					if (process.env.PASSWORD) {
+					   console.log("Found password : " + process.env.PASSWORD) ;
+					   password = process.env.PASSWORD ;
+					   confirmPassword = process.env.PASSWORD ;
+					}
+					else {
+					   console.log("No found password: " ) ;
+					   password = await question("New Password: ");
+					   confirmPassword = await question("Confirm New Password: ");
+					}
 
-                if (password === confirmPassword) {
-                    await User.resetPassword(user.id, password);
+					if (password === confirmPassword) {
+						await User.resetPassword(user.id, password);
+						console.log("Password reset successfully.");
 
-                    // Reset all sessions by reset jwt secret
-                    await server.initJWTSecret();
+						// Reset all sessions by reset jwt secret
+						await server.initJWTSecret();
 
-                    console.log("Password reset successfully.");
+						console.log("JWT reset successfully.");
 
-                    // Disconnect all other socket clients of the user
-                    await disconnectAllSocketClients(user.username, password);
-
-                    break;
-                } else {
-                    console.log("Passwords do not match, please try again.");
-                }
-            }
-        }
-    } catch (e) {
-        if (e instanceof Error) {
-            console.error("Error: " + e.message);
-        }
-    }
+						// Disconnect all other socket clients of the user
+						await disconnectAllSocketClients(user.username, password);
 
+					} else {
+						console.log("Passwords do not match, please try again.");
+						break;
+					}
+					break;
+				}  
+			} catch (e) {
+				if (e instanceof Error) {
+					console.error("Error: " + e.message);
+				}
+			}
+	
     await Database.close();
     rl.close();
 
@@ -139,3 +162,4 @@ function disconnectAllSocketClients(username : string, password : string) : Prom
 if (!process.env.TEST_BACKEND) {
     main();
 }
+

From d5e8f17aa190ea4a6c5ba6957744e34f2f4294b7 Mon Sep 17 00:00:00 2001
From: laurentlemercier <22855554+laurentlemercier@users.noreply.github.com>
Date: Sun, 10 Mar 2024 23:34:03 +0100
Subject: [PATCH 6/8] Update reset-password.ts

Indent changes...
---
 extra/reset-password.ts | 124 ++++++++++++++++++++--------------------
 1 file changed, 62 insertions(+), 62 deletions(-)

diff --git a/extra/reset-password.ts b/extra/reset-password.ts
index 1154474..08131e8 100644
--- a/extra/reset-password.ts
+++ b/extra/reset-password.ts
@@ -30,33 +30,33 @@ export const main = async () => {
     }
 
     try {
-		let user ;
+        let user ;
         // No need to actually reset the password for testing, just make sure no connection problem. It is ok for now.
-		
+        
         if (!process.env.TEST_BACKEND) {
             user = await R.findOne("user");
-            				
+                        
             if (! user )  {
-				if ( !process.env.USER ) {
-                    throw new Error("user not found or provided, have you installed? Try to set USER env variable ...");
-				} else {
-					console.log("Trying to initialise user : " + process.env.USER);
-						user = R.dispense("user");
-						user.username = process.env.USER;	
-						user.password = generatePasswordHash(process.env.PASSWORD);
-						await R.store(user);						
-						console.log("User/Password set successfully");
-						
-						// Reset all sessions by reset jwt secret
-						await server.initJWTSecret();
-						console.log("JWT reset successfully.");
+                if ( !process.env.USER ) {
+                    throw new Error("user not found or provided, have you installed? Try to set USER and PASSWORD variables ...");
+                } else {
+                    console.log("Trying to initialise user : " + process.env.USER);
+                    user = R.dispense("user");
+                    user.username = process.env.USER;        
+                    user.password = generatePasswordHash(process.env.PASSWORD);
+                    await R.store(user);                    
+                    console.log("User/Password set successfully");
+                    
+                    // Reset all sessions by reset jwt secret
+                    await server.initJWTSecret();
+                    console.log("JWT reset successfully.");
 
-						// Disconnect all other socket clients of the user
-						await disconnectAllSocketClients(user.username, user.password);
-						console.log("You may have to restart");
-						exit;
-				}
-			}
+                    // Disconnect all other socket clients of the user
+                    await disconnectAllSocketClients(user.username, user.password);
+                    console.log("You may have to restart");
+                    exit;
+                }
+                }
             }
 
             let password = "";
@@ -65,41 +65,41 @@ export const main = async () => {
 
             while (true) {
 
-					if (process.env.PASSWORD) {
-					   console.log("Found password : " + process.env.PASSWORD) ;
-					   password = process.env.PASSWORD ;
-					   confirmPassword = process.env.PASSWORD ;
-					}
-					else {
-					   console.log("No found password: " ) ;
-					   password = await question("New Password: ");
-					   confirmPassword = await question("Confirm New Password: ");
-					}
+                if (process.env.PASSWORD) {
+                   console.log("Found password : " + process.env.PASSWORD) ;
+                   password = process.env.PASSWORD ;
+                   confirmPassword = process.env.PASSWORD ;
+                }
+                else {
+                   console.log("No found password: " ) ;
+                   password = await question("New Password: ");
+                   confirmPassword = await question("Confirm New Password: ");
+                }
 
-					if (password === confirmPassword) {
-						await User.resetPassword(user.id, password);
-						console.log("Password reset successfully.");
+                if (password === confirmPassword) {
+                    await User.resetPassword(user.id, password);
+                    console.log("Password reset successfully.");
 
-						// Reset all sessions by reset jwt secret
-						await server.initJWTSecret();
+                    // Reset all sessions by reset jwt secret
+                    await server.initJWTSecret();
 
-						console.log("JWT reset successfully.");
+                    console.log("JWT reset successfully.");
 
-						// Disconnect all other socket clients of the user
-						await disconnectAllSocketClients(user.username, password);
+                    // Disconnect all other socket clients of the user
+                    await disconnectAllSocketClients(user.username, password);
 
-					} else {
-						console.log("Passwords do not match, please try again.");
-						break;
-					}
-					break;
-				}  
-			} catch (e) {
-				if (e instanceof Error) {
-					console.error("Error: " + e.message);
-				}
-			}
-	
+                } else {
+                    console.log("Passwords do not match, please try again.");
+                    break;
+                }
+            break;
+            }  
+        } catch (e) {
+            if (e instanceof Error) {
+                console.error("Error: " + e.message);
+            }
+        }
+        
     await Database.close();
     rl.close();
 
@@ -132,17 +132,17 @@ function disconnectAllSocketClients(username : string, password : string) : Prom
         });
         socket.on("connect", () => {
             socket.emit("login", {
-                username,
-                password,
+        username,
+        password,
             }, (res : BaseRes) => {
-                if (res.ok) {
-                    console.log("Logged in.");
-                    socket.emit("disconnectOtherSocketClients");
-                } else {
-                    console.warn("Login failed.");
-                    console.warn("Please restart the server to disconnect all sessions.");
-                }
-                socket.close();
+        if (res.ok) {
+            console.log("Logged in.");
+            socket.emit("disconnectOtherSocketClients");
+        } else {
+            console.warn("Login failed.");
+            console.warn("Please restart the server to disconnect all sessions.");
+        }
+        socket.close();
             });
         });
 

From 8533d18723ca32ff6423d043c69d6a3ed52affed Mon Sep 17 00:00:00 2001
From: laurentlemercier <22855554+laurentlemercier@users.noreply.github.com>
Date: Sun, 10 Mar 2024 23:41:19 +0100
Subject: [PATCH 7/8] Update reset-password.ts

Spaces...
---
 extra/reset-password.ts | 93 ++++++++++++++++++++---------------------
 1 file changed, 46 insertions(+), 47 deletions(-)

diff --git a/extra/reset-password.ts b/extra/reset-password.ts
index 08131e8..d63434f 100644
--- a/extra/reset-password.ts
+++ b/extra/reset-password.ts
@@ -32,21 +32,21 @@ export const main = async () => {
     try {
         let user ;
         // No need to actually reset the password for testing, just make sure no connection problem. It is ok for now.
-        
+
         if (!process.env.TEST_BACKEND) {
             user = await R.findOne("user");
-                        
-            if (! user )  {
+
+            if (! user ) {
                 if ( !process.env.USER ) {
                     throw new Error("user not found or provided, have you installed? Try to set USER and PASSWORD variables ...");
                 } else {
                     console.log("Trying to initialise user : " + process.env.USER);
                     user = R.dispense("user");
-                    user.username = process.env.USER;        
+                    user.username = process.env.USER;
                     user.password = generatePasswordHash(process.env.PASSWORD);
-                    await R.store(user);                    
+                    await R.store(user);
                     console.log("User/Password set successfully");
-                    
+
                     // Reset all sessions by reset jwt secret
                     await server.initJWTSecret();
                     console.log("JWT reset successfully.");
@@ -56,49 +56,48 @@ export const main = async () => {
                     console.log("You may have to restart");
                     exit;
                 }
-                }
-            }
-
-            let password = "";
-            let confirmPassword = " ";
-
-
-            while (true) {
-
-                if (process.env.PASSWORD) {
-                   console.log("Found password : " + process.env.PASSWORD) ;
-                   password = process.env.PASSWORD ;
-                   confirmPassword = process.env.PASSWORD ;
-                }
-                else {
-                   console.log("No found password: " ) ;
-                   password = await question("New Password: ");
-                   confirmPassword = await question("Confirm New Password: ");
-                }
-
-                if (password === confirmPassword) {
-                    await User.resetPassword(user.id, password);
-                    console.log("Password reset successfully.");
-
-                    // Reset all sessions by reset jwt secret
-                    await server.initJWTSecret();
-
-                    console.log("JWT reset successfully.");
-
-                    // Disconnect all other socket clients of the user
-                    await disconnectAllSocketClients(user.username, password);
-
-                } else {
-                    console.log("Passwords do not match, please try again.");
-                    break;
-                }
-            break;
-            }  
-        } catch (e) {
-            if (e instanceof Error) {
-                console.error("Error: " + e.message);
             }
         }
+
+        let password = "";
+        let confirmPassword = " ";
+
+        while (true) {
+
+            if (process.env.PASSWORD) {
+                console.log("Found password : " + process.env.PASSWORD) ;
+                password = process.env.PASSWORD ;
+                confirmPassword = process.env.PASSWORD ;
+            }
+            else {
+                console.log("No found password: " ) ;
+                password = await question("New Password: ");
+                confirmPassword = await question("Confirm New Password: ");
+            }
+
+            if (password === confirmPassword) {
+                await User.resetPassword(user.id, password);
+                console.log("Password reset successfully.");
+
+                // Reset all sessions by reset jwt secret
+                await server.initJWTSecret();
+
+                console.log("JWT reset successfully.");
+
+                // Disconnect all other socket clients of the user
+                await disconnectAllSocketClients(user.username, password);
+
+            } else {
+                console.log("Passwords do not match, please try again.");
+                break;
+            }
+            break;
+        }  
+    } catch (e) {
+        if (e instanceof Error) {
+            console.error("Error: " + e.message);
+        }
+    }
         
     await Database.close();
     rl.close();

From 165a944822f880a464e2264076f1eebc1ee1afe4 Mon Sep 17 00:00:00 2001
From: laurentlemercier <22855554+laurentlemercier@users.noreply.github.com>
Date: Sun, 10 Mar 2024 23:49:19 +0100
Subject: [PATCH 8/8] Update reset-password.ts

Spaces...
---
 extra/reset-password.ts | 27 +++++++++++++--------------
 1 file changed, 13 insertions(+), 14 deletions(-)

diff --git a/extra/reset-password.ts b/extra/reset-password.ts
index d63434f..2c2eeeb 100644
--- a/extra/reset-password.ts
+++ b/extra/reset-password.ts
@@ -68,8 +68,7 @@ export const main = async () => {
                 console.log("Found password : " + process.env.PASSWORD) ;
                 password = process.env.PASSWORD ;
                 confirmPassword = process.env.PASSWORD ;
-            }
-            else {
+            } else {
                 console.log("No found password: " ) ;
                 password = await question("New Password: ");
                 confirmPassword = await question("Confirm New Password: ");
@@ -92,13 +91,13 @@ export const main = async () => {
                 break;
             }
             break;
-        }  
+        }
     } catch (e) {
         if (e instanceof Error) {
             console.error("Error: " + e.message);
         }
     }
-        
+
     await Database.close();
     rl.close();
 
@@ -131,17 +130,17 @@ function disconnectAllSocketClients(username : string, password : string) : Prom
         });
         socket.on("connect", () => {
             socket.emit("login", {
-        username,
-        password,
+                username,
+                password,
             }, (res : BaseRes) => {
-        if (res.ok) {
-            console.log("Logged in.");
-            socket.emit("disconnectOtherSocketClients");
-        } else {
-            console.warn("Login failed.");
-            console.warn("Please restart the server to disconnect all sessions.");
-        }
-        socket.close();
+                if (res.ok) {
+                    console.log("Logged in.");
+                    socket.emit("disconnectOtherSocketClients");
+                } else {
+                    console.warn("Login failed.");
+                    console.warn("Please restart the server to disconnect all sessions.");
+                }
+                socket.close();
             });
         });