Skip to content

Commit 5345c00

Browse files
authored
Merge pull request Dokploy#5237 from shuv-o/feat/reset-password-by-email
feat: allow reset-password script to reset a specific user by email
2 parents d181384 + 8db39a8 commit 5345c00

2 files changed

Lines changed: 39 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: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,54 @@
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]?.trim().toLowerCase();
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(
36+
and(eq(account.userId, userId), eq(account.providerId, "credential")),
37+
);
1838

19-
if (update) {
39+
if (update.count > 0) {
2040
console.log("Password reset successful");
2141
console.log("New password: ", randomPassword.randomPassword);
2242
} else {
23-
console.log("Password reset failed");
43+
console.log(
44+
"Password reset failed: no credential account found for this user",
45+
);
46+
process.exit(1);
2447
}
2548

2649
process.exit(0);
2750
} catch (error) {
2851
console.log("Error resetting password", error);
52+
process.exit(1);
2953
}
3054
})();

0 commit comments

Comments
 (0)