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] 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(); } +