mirror of
https://github.com/Permissionless-Software-Foundation/bch-dex.git
synced 2026-09-21 16:52:00 -07:00
fix(util): Adding scripts for creating new users
This commit is contained in:
@@ -0,0 +1,55 @@
|
|||||||
|
/*
|
||||||
|
Use the system-user-dev.json file to log in as the admin user
|
||||||
|
and create a new user.
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Global npm libraries
|
||||||
|
import axios from 'axios'
|
||||||
|
import fs from 'fs'
|
||||||
|
|
||||||
|
// Local libraries
|
||||||
|
// import config from '../../../config/index.js'
|
||||||
|
// import User from '../../../src/adapters/localdb/models/users.js'
|
||||||
|
|
||||||
|
// Customize these variables
|
||||||
|
const EMAIL = 'someone@somewhere.com'
|
||||||
|
const NAME = 'Someone'
|
||||||
|
const PASSWORD = 'test'
|
||||||
|
|
||||||
|
async function createUser () {
|
||||||
|
try {
|
||||||
|
// Open the system-user-dev.json file
|
||||||
|
const systemUser = JSON.parse(fs.readFileSync('../../../config/system-user-dev.json', 'utf8'))
|
||||||
|
|
||||||
|
// Log in as the admin user
|
||||||
|
const adminLogin = {
|
||||||
|
email: systemUser.email,
|
||||||
|
password: systemUser.password
|
||||||
|
}
|
||||||
|
const response = await axios.post('http://localhost:5700/auth', adminLogin)
|
||||||
|
console.log(`response: ${JSON.stringify(response.data, null, 2)}`)
|
||||||
|
|
||||||
|
const jwt = response.data.token
|
||||||
|
|
||||||
|
// Create a new user, using the admin credentials
|
||||||
|
const options = {
|
||||||
|
method: 'post',
|
||||||
|
url: 'http://localhost:5700/users/',
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${jwt}`
|
||||||
|
},
|
||||||
|
data: {
|
||||||
|
user: {
|
||||||
|
email: EMAIL,
|
||||||
|
name: NAME,
|
||||||
|
password: PASSWORD
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const result = await axios(options)
|
||||||
|
console.log(`result: ${JSON.stringify(result.data, null, 2)}`)
|
||||||
|
} catch (err) {
|
||||||
|
console.error(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
createUser()
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
import mongoose from 'mongoose'
|
import mongoose from 'mongoose'
|
||||||
import config from '../../config/index.js'
|
import config from '../../../config/index.js'
|
||||||
import User from '../../src/adapters/localdb/models/users.js'
|
import User from '../../../src/adapters/localdb/models/users.js'
|
||||||
|
|
||||||
async function getUsers () {
|
async function getUsers () {
|
||||||
// Connect to the Mongo Database.
|
// Connect to the Mongo Database.
|
||||||
|
|||||||
@@ -0,0 +1,57 @@
|
|||||||
|
/*
|
||||||
|
Log in as an admin user to create a new user.
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Global npm libraries
|
||||||
|
import axios from 'axios'
|
||||||
|
// import fs from 'fs'
|
||||||
|
|
||||||
|
// Local libraries
|
||||||
|
// import config from '../../../config/index.js'
|
||||||
|
// import User from '../../../src/adapters/localdb/models/users.js'
|
||||||
|
|
||||||
|
// Customize these variables
|
||||||
|
const EMAIL = 'someone@somewhere.com'
|
||||||
|
const NAME = 'Someone'
|
||||||
|
const PASSWORD = 'test'
|
||||||
|
|
||||||
|
const ADMIN_EMAIL = 'system@system.com'
|
||||||
|
const ADMIN_PASSWORD = 'admin'
|
||||||
|
|
||||||
|
async function createUser () {
|
||||||
|
try {
|
||||||
|
// Open the system-user-prod.json file
|
||||||
|
// const systemUser = JSON.parse(fs.readFileSync('../../../config/system-user-prod.json', 'utf8'))
|
||||||
|
|
||||||
|
// Log in as the admin user
|
||||||
|
const adminLogin = {
|
||||||
|
email: ADMIN_EMAIL,
|
||||||
|
password: ADMIN_PASSWORD
|
||||||
|
}
|
||||||
|
const response = await axios.post('http://localhost:5700/auth', adminLogin)
|
||||||
|
console.log(`response: ${JSON.stringify(response.data, null, 2)}`)
|
||||||
|
|
||||||
|
const jwt = response.data.token
|
||||||
|
|
||||||
|
// Create a new user, using the admin credentials
|
||||||
|
const options = {
|
||||||
|
method: 'post',
|
||||||
|
url: 'http://localhost:5700/users/',
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${jwt}`
|
||||||
|
},
|
||||||
|
data: {
|
||||||
|
user: {
|
||||||
|
email: EMAIL,
|
||||||
|
name: NAME,
|
||||||
|
password: PASSWORD
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const result = await axios(options)
|
||||||
|
console.log(`result: ${JSON.stringify(result.data, null, 2)}`)
|
||||||
|
} catch (err) {
|
||||||
|
console.error(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
createUser()
|
||||||
Reference in New Issue
Block a user