-
-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added script to add users by CLI
- Loading branch information
Showing
2 changed files
with
31 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); | ||
} |