Update reset-password.ts

Indent changes...
This commit is contained in:
laurentlemercier 2024-03-10 23:34:03 +01:00 committed by GitHub
parent c451067ac4
commit d5e8f17aa1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

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