2019-07-19 13:51:57 -07:00
|
|
|
var express = require('express')
|
|
|
|
var router = express.Router()
|
|
|
|
|
2019-07-30 12:21:12 -07:00
|
|
|
let User = require('@models/User');
|
2020-05-06 00:10:27 -07:00
|
|
|
const cs = require('@helpers/CryptoString')
|
2019-07-19 13:51:57 -07:00
|
|
|
|
|
|
|
// middleware that is specific to this router
|
|
|
|
router.use(function timeLog (req, res, next) {
|
|
|
|
// console.log('Time: ', Date.now())
|
|
|
|
next()
|
|
|
|
})
|
|
|
|
// define the home page route
|
|
|
|
router.get('/', function (req, res) {
|
|
|
|
res.send('User Home Page ' + User.getUsername())
|
|
|
|
})
|
|
|
|
// define the about route
|
|
|
|
router.get('/about', function (req, res) {
|
|
|
|
User.getUsername(req.headers.userId)
|
|
|
|
.then( data => res.send(data) )
|
|
|
|
})
|
2019-07-24 11:06:50 -07:00
|
|
|
// define the login route
|
2019-07-19 13:51:57 -07:00
|
|
|
router.post('/login', function (req, res) {
|
|
|
|
|
|
|
|
//Pull out variables we want
|
|
|
|
const username = req.body.username
|
|
|
|
const password = req.body.password
|
|
|
|
|
|
|
|
let returnData = {
|
|
|
|
success: false,
|
2019-07-24 11:06:50 -07:00
|
|
|
token: '',
|
|
|
|
username: ''
|
2019-07-19 13:51:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
User.login(username, password)
|
2020-05-06 00:10:27 -07:00
|
|
|
.then( ({token, userId}) => {
|
|
|
|
|
|
|
|
returnData['username'] = username
|
|
|
|
returnData['token'] = token
|
|
|
|
returnData['success'] = true
|
|
|
|
|
|
|
|
res.send(returnData)
|
|
|
|
return
|
|
|
|
})
|
|
|
|
.catch(e => {
|
|
|
|
console.log(e)
|
|
|
|
res.send(returnData)
|
|
|
|
})
|
2019-07-19 13:51:57 -07:00
|
|
|
})
|
|
|
|
|
2020-02-11 13:11:14 -08:00
|
|
|
// fetch counts of users notes
|
|
|
|
router.post('/totals', function (req, res) {
|
|
|
|
User.getCounts(req.headers.userId)
|
|
|
|
.then( countsObject => res.send( countsObject ))
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2019-07-19 13:51:57 -07:00
|
|
|
module.exports = router
|