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
This commit is contained in:
laurentlemercier 2024-03-10 23:18:50 +01:00 committed by GitHub
parent 9615b844c3
commit c451067ac4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

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