Got admin user created at startup

This commit is contained in:
Chris Troutner
2019-05-16 20:18:09 -07:00
parent 5994e58fcd
commit 1ddf247e45
8 changed files with 31 additions and 20 deletions
+1
View File
@@ -60,3 +60,4 @@ docs
.nyc_output
coverage
database/
system-user-*.json
+5 -4
View File
@@ -8,7 +8,7 @@ const passport = require('koa-passport')
const mount = require('koa-mount')
const serve = require('koa-static')
const cors = require('kcors')
const adminLib = require('../src/lib/admin')
const config = require('../config')
const errorMiddleware = require('../src/middleware')
@@ -47,12 +47,13 @@ async function startServer () {
// MIDDLEWARE END
// app.listen(config.port, () => {
// console.log(`Server started on ${config.port}`)
// })
await app.listen(config.port)
console.log(`Server started on ${config.port}`)
// Create the system admin user.
const success = await adminLib.createSystemUser()
if (success) console.log(`System admin user created.`)
return app
}
// startServer()
+2 -1
View File
@@ -7,5 +7,6 @@
module.exports = {
session: 'secret-boilerplate-token',
token: 'secret-jwt-token',
database: 'mongodb://localhost:27017/koa-server-dev'
database: 'mongodb://localhost:27017/koa-server-dev',
env: 'dev'
}
+2 -1
View File
@@ -10,5 +10,6 @@
module.exports = {
session: 'secret-boilerplate-token',
token: 'secret-jwt-token',
database: 'mongodb://172.17.0.1:5555/koa-server-prod'
database: 'mongodb://172.17.0.1:5555/koa-server-prod',
env: 'prod'
}
+2 -1
View File
@@ -7,5 +7,6 @@
module.exports = {
session: 'secret-boilerplate-token',
token: 'secret-jwt-token',
database: 'mongodb://localhost:27017/koa-server-test'
database: 'mongodb://localhost:27017/koa-server-test',
env: 'test'
}
+11 -13
View File
@@ -16,10 +16,8 @@ const User = require('../models/users')
const jsonFiles = require('./utils/json-files')
const config = require('../../config')
// Ensure the environment variable is set
process.env.P2PVPS_ENV = process.env.P2PVPS_ENV || 'dev'
const env = process.env.P2PVPS_ENV
const JSON_FILE = `system-user-${env}.json`
const JSON_FILE = `system-user-${config.env}.json`
const JSON_PATH = `${__dirname}/../../config/${JSON_FILE}`
const LOCALHOST = `http://localhost:${config.port}`
const context = {}
@@ -34,7 +32,7 @@ async function createSystemUser () {
const options = {
method: 'POST',
uri: `${LOCALHOST}/api/users`,
uri: `${LOCALHOST}/users`,
resolveWithFullResponse: true,
json: true,
body: {
@@ -62,7 +60,7 @@ async function createSystemUser () {
// Write out the system user information to a JSON file that external
// applications like the Task Manager and the test scripts can access.
await jsonFiles.writeJSON(context, `${__dirname}/../persist/${JSON_FILE}`)
await jsonFiles.writeJSON(context, JSON_PATH)
return context
} catch (err) {
@@ -75,12 +73,12 @@ async function createSystemUser () {
// Call this function again.
return createSystemUser()
} catch (err2) {
console.error(`Error in util.js/createSystemUser() while trying generate new system user.`)
console.error(`Error in admin.js/createSystemUser() while trying generate new system user.`)
// process.end(1)
throw err2
}
} else {
console.log('Error in util.js/createSystemUser: ' + JSON.stringify(err, null, 2))
console.log('Error in admin.js/createSystemUser: ' + JSON.stringify(err, null, 2))
// process.end(1)
throw err
}
@@ -97,7 +95,7 @@ async function deleteExistingSystemUser () {
// Delete the user.
const options = {
method: 'DELETE',
uri: `${LOCALHOST}/api/users/${id}`,
uri: `${LOCALHOST}/users/${id}`,
resolveWithFullResponse: true,
json: true,
headers: {
@@ -109,7 +107,7 @@ async function deleteExistingSystemUser () {
return result.body.success
} catch (err) {
console.log(`Error in util.js/deleteExistingSystemUser()`)
console.log(`Error in admin.js/deleteExistingSystemUser()`)
throw err
}
}
@@ -120,13 +118,13 @@ async function loginAdmin () {
let existingUser
// Read the exising file
existingUser = await jsonFiles.readJSON(`${__dirname}/../persist/${JSON_FILE}`)
existingUser = await jsonFiles.readJSON(JSON_PATH)
// console.log(`existingUser: ${JSON.stringify(existingUser, null, 2)}`)
// Log in as the user.
let options = {
method: 'POST',
uri: `${LOCALHOST}/api/auth`,
uri: `${LOCALHOST}/auth`,
resolveWithFullResponse: true,
json: true,
body: {
@@ -139,7 +137,7 @@ async function loginAdmin () {
return result
} catch (err) {
console.error(`Error in bin/util.js/loginAdmin().`)
console.error(`Error in admin.js/loginAdmin().`)
throw err
}
}
+4
View File
@@ -1,3 +1,7 @@
/*
Retrieves the JWT token from the header of the API request.
*/
module.exports = function getToken (ctx) {
const header = ctx.request.header.authorization
if (!header) {
+4
View File
@@ -41,6 +41,10 @@ const User = require('../../models/users')
*/
async function createUser (ctx) {
const user = new User(ctx.request.body.user)
// Enforce default value of 'user'
user.type = 'user'
try {
await user.save()
} catch (err) {