filesafe/controllers/tokenController.js
Bobby Wibowo 2dd724f88f
Updates
* Switched standard to eslint with eslint-config-standard (and 4 more eslint plugins needed by standard).

* Added "curly" eslint rule with "all" option. I like it.

* Refactored all JS files to apply the new "curly" eslint rule.

* Renewed axios.min.js, dropzone.min.js and sweetalert.min.js. Re-minified and added a small comment stating their version and copyright statement.

* Some buttons in dashboard will now show loading icon whenever they're waiting for response from the server.

* Updated README.md and .gitignore.
2018-03-29 00:40:50 +07:00

37 lines
1.2 KiB
JavaScript

const config = require('../config.js')
const db = require('knex')(config.database)
const randomstring = require('randomstring')
const utils = require('./utilsController.js')
const tokenController = {}
tokenController.verify = async (req, res, next) => {
const token = req.body.token
if (token === undefined) { return res.status(401).json({ success: false, description: 'No token provided.' }) }
const user = await db.table('users').where('token', token).first()
if (!user) { return res.status(401).json({ success: false, description: 'Invalid token.' }) }
return res.json({ success: true, username: user.username })
}
tokenController.list = async (req, res, next) => {
const user = await utils.authorize(req, res)
if (!user) { return }
return res.json({ success: true, token: user.token })
}
tokenController.change = async (req, res, next) => {
const user = await utils.authorize(req, res)
if (!user) { return }
const newtoken = randomstring.generate(64)
await db.table('users').where('token', user.token).update({
token: newtoken,
timestamp: Math.floor(Date.now() / 1000)
})
res.json({ success: true, token: newtoken })
}
module.exports = tokenController