Skip to content

Commit dada304

Browse files
committed
feat: allow reset-password script to target a specific user by email
Previously reset-password only reset the owner account's password. Passing an email as an argument now resets that specific user's password instead, while omitting it keeps the existing owner-reset behavior. Also scopes the update to the credential (password-based) account row via providerId, and fixes the success check to verify a row was actually updated instead of always reporting success.
1 parent 0971e19 commit dada304

2 files changed

Lines changed: 35 additions & 7 deletions

File tree

‎CONTRIBUTING.md‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,20 @@ pnpm run docker:push
120120

121121
## Password Reset
122122

123-
In the case you lost your password, you can reset it using the following command
123+
In the case you lost your password, you can reset the owner's password using the following command
124124

125125
```bash
126126
pnpm run reset-password
127127
```
128128

129+
To reset the password of a specific user instead, pass their email as an argument
130+
131+
```bash
132+
pnpm run reset-password -- [email protected]
133+
```
134+
135+
Both commands print the new randomly generated password to the console.
136+
129137
If you want to test the webhooks on development mode using localtunnel, make sure to install [`localtunnel`](https://localtunnel.app/)
130138

131139
```bash

‎apps/dokploy/reset-password.ts‎

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,50 @@
11
import { findOwner, generateRandomPassword } from "@dokploy/server";
22
import { db } from "@dokploy/server/db";
3-
import { account } from "@dokploy/server/db/schema";
4-
import { eq } from "drizzle-orm";
3+
import { account, user } from "@dokploy/server/db/schema";
4+
import { and, eq } from "drizzle-orm";
55

66
(async () => {
77
try {
8+
const email = process.argv[2];
9+
810
const randomPassword = await generateRandomPassword();
911

10-
const result = await findOwner();
12+
let userId: string;
13+
14+
if (email) {
15+
const foundUser = await db.query.user.findFirst({
16+
where: eq(user.email, email),
17+
});
18+
19+
if (!foundUser) {
20+
console.log(`User not found for email: ${email}`);
21+
process.exit(1);
22+
}
23+
24+
userId = foundUser.id;
25+
} else {
26+
const owner = await findOwner();
27+
userId = owner.userId;
28+
}
1129

1230
const update = await db
1331
.update(account)
1432
.set({
1533
password: randomPassword.hashedPassword,
1634
})
17-
.where(eq(account.userId, result.userId));
35+
.where(and(eq(account.userId, userId), eq(account.providerId, "credential")));
1836

19-
if (update) {
37+
if (update.count > 0) {
2038
console.log("Password reset successful");
2139
console.log("New password: ", randomPassword.randomPassword);
2240
} else {
23-
console.log("Password reset failed");
41+
console.log("Password reset failed: no credential account found for this user");
42+
process.exit(1);
2443
}
2544

2645
process.exit(0);
2746
} catch (error) {
2847
console.log("Error resetting password", error);
48+
process.exit(1);
2949
}
3050
})();

0 commit comments

Comments
 (0)