diff --git a/util/users/dev/createUser.js b/util/users/dev/createUser.js new file mode 100644 index 0000000..fd1fc66 --- /dev/null +++ b/util/users/dev/createUser.js @@ -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() diff --git a/util/users/dev/getUsers.js b/util/users/dev/getUsers.js index 80b8681..fe67b16 100644 --- a/util/users/dev/getUsers.js +++ b/util/users/dev/getUsers.js @@ -1,6 +1,6 @@ import mongoose from 'mongoose' -import config from '../../config/index.js' -import User from '../../src/adapters/localdb/models/users.js' +import config from '../../../config/index.js' +import User from '../../../src/adapters/localdb/models/users.js' async function getUsers () { // Connect to the Mongo Database. diff --git a/util/users/production/createUser.js b/util/users/production/createUser.js new file mode 100644 index 0000000..781bf1a --- /dev/null +++ b/util/users/production/createUser.js @@ -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()