Skip to content

Commit

Permalink
Adding two user scripts:
Browse files Browse the repository at this point in the history
- 'delete-user' to remove a user definitely
- 'list-balances' to show the balances of all the users
  • Loading branch information
ineiti committed Dec 22, 2023
1 parent 7915513 commit 14eca7f
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 1 deletion.
48 changes: 48 additions & 0 deletions config/delete-user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const path = require('path');
require('module-alias')({ base: path.resolve(__dirname, '..', 'api') });
const { connectWithTimeout, askQuestion, silentExit } = require('./helpers');
const User = require('~/models/User');

(async () => {
await connectWithTimeout();

/**
* Show the welcome / help menu
*/
console.purple('---------------');
console.purple('Deleting a user');
console.purple('---------------');

let email = '';
if (process.argv.length >= 3) {
email = process.argv[2];
} else {
email = await askQuestion('Email:');
}
let user = await User.findOne({ email: email });
if (user !== null) {
if ((await askQuestion(`Delete user ${user}?`)) === 'y') {
user = await User.findOneAndDelete({ _id: user._id });
if (user !== null) {
console.yellow(`Deleted user ${user}`);
} else {
console.yellow(`Couldn't delete user with email ${email}`);
}
}
} else {
console.yellow(`Didn't find user with email ${email}`);
}

silentExit(0);
})();

process.on('uncaughtException', (err) => {
if (!err.message.includes('fetch failed')) {
console.error('There was an uncaught error:');
console.error(err);
}

if (!err.message.includes('fetch failed')) {
process.exit(1);
}
});
39 changes: 39 additions & 0 deletions config/list-balances.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const path = require('path');
require('module-alias')({ base: path.resolve(__dirname, '..', 'api') });
const { connectWithTimeout, silentExit } = require('./helpers');
const Balance = require('~/models/Balance');
const User = require('~/models/User');

(async () => {
await connectWithTimeout();

/**
* Show the welcome / help menu
*/
console.purple('-----------------------------');
console.purple('Show the balance of all users');
console.purple('-----------------------------');

let users = await User.find({});
for (const user of users) {
let balance = await Balance.findOne({ user: user._id });
if (balance !== null) {
console.green(`User ${user.name} has a balance of ${balance.tokenCredits}`);
} else {
console.yellow(`User ${user.name} has no balance`);
}
}

silentExit(0);
})();

process.on('uncaughtException', (err) => {
if (!err.message.includes('fetch failed')) {
console.error('There was an uncaught error:');
console.error(err);
}

if (!err.message.includes('fetch failed')) {
process.exit(1);
}
});
8 changes: 8 additions & 0 deletions docs/features/token_usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ npm run add-balance danny@librechat.ai 1000

This works well to track your own usage for personal use; 1000 credits = $0.001 (1 mill USD)

## Listing of balances

To see the balances of your users, you can run:

```bash
npm run list-balances
```

## Notes

- With summarization enabled, you will be blocked from making an API request if the cost of the content that you need to summarize + your messages payload exceeds the current balance
Expand Down
2 changes: 2 additions & 0 deletions docs/install/configuration/dotenv.md
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,7 @@ see: [Token Usage](../../features/token_usage.md)

- To manually add balances, run the following command:`npm run add-balance`
- You can also specify the email and token credit amount to add, e.g.:`npm run add-balance example@example.com 1000`
- To list the balance of every user: `npm run list-balances`

> **Note:** 1000 credits = $0.001 (1 mill USD)
Expand All @@ -600,6 +601,7 @@ see: [User/Auth System](../configuration/user_auth_system.md)
- `ALLOW_SOCIAL_REGISTRATION`: Enable or disable registration of new user using various social network. Set to `true` or `false` to enable or disable.

> **Quick Tip:** Even with registration disabled, add users directly to the database using `npm run create-user`.
> **Quick Tip:** With registration disabled, you can delete a user with `npm run delete-user email@domain.com`.
```bash
ALLOW_EMAIL_LOGIN=true
Expand Down
1 change: 1 addition & 0 deletions docs/install/configuration/user_auth_system.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Here's an overview of the general configuration, located in the `.env` file at t
> **Note:** OpenID does not support the ability to disable only registration.

>> **Quick Tip:** Even with registration disabled, add users directly to the database using `npm run create-user`. If you can't get npm to work, try `sudo docker exec -ti LibreChat sh` first to "ssh" into the container.
>> **Quick Tip:** To delete a user, you can run `docker-compose exec api npm run delete-user email@domain.com`

![image](https://github.com/danny-avila/LibreChat/assets/81851188/52a37d1d-7392-4a9a-a79f-90ed2da7f841)

Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"scripts": {
"update": "node config/update.js",
"add-balance": "node config/add-balance.js",
"list-balances": "node config/list-balances.js",
"rebuild:package-lock": "node config/packages",
"reinstall": "node config/update.js -l -g",
"b:reinstall": "bun config/update.js -b -l -g",
Expand All @@ -25,6 +26,7 @@
"upgrade": "node config/upgrade.js",
"create-user": "node config/create-user.js",
"ban-user": "node config/ban-user.js",
"delete-user": "node config/delete-user.js",
"backend": "cross-env NODE_ENV=production node api/server/index.js",
"backend:dev": "cross-env NODE_ENV=development npx nodemon api/server/index.js",
"backend:stop": "node config/stop-backend.js",
Expand Down Expand Up @@ -53,7 +55,8 @@
"b:client:dev": "cd client && bun run b:dev",
"b:test:client": "cd client && bun run b:test",
"b:test:api": "cd api && bun run b:test",
"b:balance": "bun config/add-balance.js"
"b:balance": "bun config/add-balance.js",
"b:list-balances": "bun config/list-balances.js"
},
"repository": {
"type": "git",
Expand Down

0 comments on commit 14eca7f

Please sign in to comment.