filesafe/controllers/tokenController.js
Bobby Wibowo 52d336cc45
Updated ESLint rule: curly
No more enforced curly for if/else/for/while/do blocks w/ one statement.

With that said, auto-fixed all JS files to follow the rule.

I'd also like to apologize for the inconveniences this commit cause,
after all it was me who intentionally enforced curly rule back then.

Why the change of heart?
After doing some more non-JS codes recently, I realized it was
pretty stupid of me to enforce that.
2018-12-19 00:01:28 +07:00

57 lines
1.3 KiB
JavaScript

const config = require('./../config')
const db = require('knex')(config.database)
const perms = require('./permissionController')
const randomstring = require('randomstring')
const utils = require('./utilsController')
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,
permissions: perms.mapPermissions(user)
})
}
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