Working login on mobile

This commit is contained in:
John Lyon-Smith
2018-03-12 19:11:13 -07:00
parent eeb3eb4947
commit e7aaa014ff
19 changed files with 10606 additions and 131 deletions

6
server/.gitignore vendored
View File

@@ -1,7 +1,3 @@
node_modules/
coverage/
dist/
.DS_Store
npm-debug.log*
yarn-debug.log*
yarn-error.log*
scratch/

View File

@@ -8,30 +8,56 @@ import crypto from 'crypto'
import urlSafeBase64 from 'urlsafe-base64'
import util from 'util'
const mongoUri = config.get('uri.mongo')
import autoBind from 'auto-bind2'
new DB().connect(mongoUri).then((db) => {
console.log(`Connected to MongoDB at ${mongoUri}`)
class SendMessageTool {
constructor(toolName, log) {
autoBind(this)
this.toolName = toolName
this.log = log
}
const User = db.User
let user = new User({
administrator: true,
})
user.firstName = readlineSync.question('First name? ')
user.lastName = readlineSync.question('Last name? ')
user.email = readlineSync.question('Email? ')
let password = readlineSync.question('Password? ', {hideEchoBack: true})
let cr = credential()
async run() {
const mongoUri = config.get('uri.mongo')
util.promisify(cr.hash)(password).then((json) => {
user.passwordHash = JSON.parse(json)
new DB().connect(mongoUri).then((db) => {
console.log(`Connected to MongoDB at ${mongoUri}`)
return user.save()
}).then((savedUser) => {
console.log(`User is ${user}`)
process.exit(0)
}).catch((error) => {
console.log(`error: ${error.message}`)
process.exit(-1)
})
const User = db.User
let user = new User({
administrator: true,
})
user.firstName = readlineSync.question('First name? ')
user.lastName = readlineSync.question('Last name? ')
user.email = readlineSync.question('Email? ')
let password = readlineSync.question('Password? ', {hideEchoBack: true})
let cr = credential()
util.promisify(cr.hash)(password).then((json) => {
user.passwordHash = JSON.parse(json)
return user.save()
}).then((savedUser) => {
console.log(`User is ${user}`)
process.exit(0)
}).catch((error) => {
console.log(`error: ${error.message}`)
process.exit(-1)
})
})
}
}
const log = {
info: console.info,
error: function() { console.error(chalk.red('error:', [...arguments].join(' ')))},
warning: function() { console.error(chalk.yellow('warning:', [...arguments].join(' ')))}
}
const tool = new AddUserTool('add-user', log)
tool.run(process.argv.slice(2)).then((exitCode) => {
process.exit(exitCode)
}).catch((err) => {
console.error(err)
})

View File

@@ -0,0 +1,119 @@
import parseArgs from 'minimist'
import amqp from 'amqplib'
import JSON5 from 'json5'
import fs from 'fs'
import uuidv4 from 'uuid/v4'
import chalk from 'chalk'
import autoBind from 'auto-bind2'
class SendMessageTool {
constructor(toolName, log) {
autoBind(this)
this.toolName = toolName
this.log = log
}
async run(argv) {
const options = {
string: [ 'exchange', 'type' ],
boolean: [ 'help', 'version' ],
alias: {
'x': 'exchange',
't': 'type'
}
}
let args = parseArgs(argv, options)
if (args.help) {
this.log.info(`
usage: tmr-message [options] <file>
options:
-x --exchange <exchange> Exchange to send the message too, e.g. tmr-image
-t --type <message-type> The type of the message content
`)
return 0
}
if (!args.exchange) {
this.log.error("Must specify a message exchange")
return -1
}
if (!args.type) {
this.log.error("Must specify message type")
return -1
}
const exchangeName = args.exchange
const filename = args._[0]
if (!filename) {
this.log.error("Must specify a JSON5 message to send")
return -1
}
let msg = null
try {
msg = JSON5.parse(fs.readFileSync(filename))
} catch (err) {
this.log.error(`Could not read '${filename}'`)
return -1
}
const correlationId = uuidv4()
const replyQueueName = `reply-${uuidv4()}`
const withChannel = async (ch) => {
return new Promise(async (resolve, reject) => {
const q = await ch.assertQueue(replyQueueName, {exclusive: true})
if (!q) {
return reject(new Error(`Could not create reply queue ${replyQueueName}`))
}
ch.consume(q.queue, async (resMsg) => {
this.log.info(` Response ${resMsg.content.toString()}`)
await ch.close()
resolve(0)
}, {noAck: true})
const ok = await ch.checkExchange(exchangeName)
if (!ok) {
reject(new Error(`Could not create exchange ${exchangeName}`))
}
const s = JSON.stringify(msg)
this.log.info(` Type '${args.type}', Correlation id '${correlationId}'`)
this.log.info(` Sent '${s}'`)
ch.publish(exchangeName, '', new Buffer(s), {
type: args.type,
contentType: 'application/json',
timestamp: Date.now(),
correlationId,
appId: 'tmr-cli',
replyTo: replyQueueName
})
})
}
const conn = await amqp.connect('amqp://localhost')
const ch = await conn.createChannel()
await withChannel(ch)
}
}
const log = {
info: console.info,
error: function() { console.error(chalk.red('error:', [...arguments].join(' ')))},
warning: function() { console.error(chalk.yellow('warning:', [...arguments].join(' ')))}
}
const tool = new SendMessageTool('send-message', log)
tool.run(process.argv.slice(2)).then((exitCode) => {
process.exit(exitCode)
}).catch((err) => {
console.error(err)
})