Skip to content

Commit

Permalink
feat: added script to add users by CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
tycrek committed Dec 25, 2022
1 parent d3181cb commit 69bf9aa
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"docker-upfull": "npm run docker-update && npm run docker-resetup",
"docker-resetup": "docker-compose exec ass npm run setup && docker-compose restart",
"cli-setpassword": "node dist/tools/script.setpassword.js",
"cli-testpassword": "node dist/tools/script.testpassword.js"
"cli-testpassword": "node dist/tools/script.testpassword.js",
"cli-adduser": "node dist/tools/script.adduser.js"
},
"repository": "github:tycrek/ass",
"keywords": [
Expand Down
29 changes: 29 additions & 0 deletions src/tools/script.adduser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import path from 'path';
import fs from 'fs-extra';
import axios from 'axios';
import logger from '../logger';
import { User } from '../types/auth';

// Port from config.json
const { port } = fs.readJsonSync(path.join(process.cwd(), 'config.json'));

// CLI key from auth.json
const { cliKey } = fs.readJsonSync(path.join(process.cwd(), 'auth.json'));

if (process.argv.length < 4) {
logger.error('Missing username or password');
logger.error('Usage: node script.adduser.js <username> <password> [admin] [meta]');
process.exit(1);
} else {
const username = process.argv[2];
const password = process.argv[3];
const admin = process.argv[4] ? process.argv[4].toLowerCase() === 'true' : false;
const meta = process.argv[5] ? JSON.parse(process.argv[5]) : {};

axios.post(`http://localhost:${port}/api/user/new`, { username, password, admin, meta }, { headers: { 'Authorization': cliKey } })
.then((response) => {
const user = response.data as User;
logger.info('User created', username, user.unid).callback(() => process.exit(0))
})
.catch((err) => logger.error(err).callback(() => process.exit(1)));
}

0 comments on commit 69bf9aa

Please sign in to comment.