mirror of
https://github.com/Dokploy/dokploy.git
synced 2026-10-15 13:21:11 +00:00
Merge pull request #5237 from shuv-o/feat/reset-password-by-email
feat: allow reset-password script to reset a specific user by email
This commit is contained in:
+9
-1
@@ -120,12 +120,20 @@ pnpm run docker:push
|
||||
|
||||
## Password Reset
|
||||
|
||||
In the case you lost your password, you can reset it using the following command
|
||||
In the case you lost your password, you can reset the owner's password using the following command
|
||||
|
||||
```bash
|
||||
pnpm run reset-password
|
||||
```
|
||||
|
||||
To reset the password of a specific user instead, pass their email as an argument
|
||||
|
||||
```bash
|
||||
pnpm run reset-password -- user@example.com
|
||||
```
|
||||
|
||||
Both commands print the new randomly generated password to the console.
|
||||
|
||||
If you want to test the webhooks on development mode using localtunnel, make sure to install [`localtunnel`](https://localtunnel.app/)
|
||||
|
||||
```bash
|
||||
|
||||
@@ -1,30 +1,54 @@
|
||||
import { findOwner, generateRandomPassword } from "@dokploy/server";
|
||||
import { db } from "@dokploy/server/db";
|
||||
import { account } from "@dokploy/server/db/schema";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { account, user } from "@dokploy/server/db/schema";
|
||||
import { and, eq } from "drizzle-orm";
|
||||
|
||||
(async () => {
|
||||
try {
|
||||
const email = process.argv[2]?.trim().toLowerCase();
|
||||
|
||||
const randomPassword = await generateRandomPassword();
|
||||
|
||||
const result = await findOwner();
|
||||
let userId: string;
|
||||
|
||||
if (email) {
|
||||
const foundUser = await db.query.user.findFirst({
|
||||
where: eq(user.email, email),
|
||||
});
|
||||
|
||||
if (!foundUser) {
|
||||
console.log(`User not found for email: ${email}`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
userId = foundUser.id;
|
||||
} else {
|
||||
const owner = await findOwner();
|
||||
userId = owner.userId;
|
||||
}
|
||||
|
||||
const update = await db
|
||||
.update(account)
|
||||
.set({
|
||||
password: randomPassword.hashedPassword,
|
||||
})
|
||||
.where(eq(account.userId, result.userId));
|
||||
.where(
|
||||
and(eq(account.userId, userId), eq(account.providerId, "credential")),
|
||||
);
|
||||
|
||||
if (update) {
|
||||
if (update.count > 0) {
|
||||
console.log("Password reset successful");
|
||||
console.log("New password: ", randomPassword.randomPassword);
|
||||
} else {
|
||||
console.log("Password reset failed");
|
||||
console.log(
|
||||
"Password reset failed: no credential account found for this user",
|
||||
);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
process.exit(0);
|
||||
} catch (error) {
|
||||
console.log("Error resetting password", error);
|
||||
process.exit(1);
|
||||
}
|
||||
})();
|
||||
|
||||
Reference in New Issue
Block a user