filesafe/controllers/tokenController.js

47 lines
1.7 KiB
JavaScript
Raw Normal View History

2017-01-17 19:54:25 +00:00
const config = require('../config.js')
const db = require('knex')(config.database)
2017-01-30 07:42:15 +00:00
const randomstring = require('randomstring')
2017-01-17 19:54:25 +00:00
let tokenController = {}
2017-03-17 04:14:24 +00:00
tokenController.verify = function(req, res, next) {
2017-01-17 19:54:25 +00:00
2017-03-17 04:14:24 +00:00
if (req.body.token === undefined) return res.json({ success: false, description: 'No token provided' })
2017-01-29 07:18:31 +00:00
let token = req.body.token
2017-01-17 19:54:25 +00:00
2017-01-29 07:18:31 +00:00
db.table('users').where('token', token).then((user) => {
2017-03-17 04:14:24 +00:00
if (user.length === 0) return res.json({ success: false, description: 'Token mismatch' })
return res.json({ success: true, username: user[0].username })
}).catch(function(error) { console.log(error); res.json({ success: false, description: 'error' }) })
2017-01-30 01:06:52 +00:00
2017-01-17 19:54:25 +00:00
}
2017-03-17 04:14:24 +00:00
tokenController.list = function(req, res, next) {
2017-01-30 01:06:52 +00:00
let token = req.headers.token
2017-03-17 04:14:24 +00:00
if (token === undefined) return res.status(401).json({ success: false, description: 'No token provided' })
2017-01-29 07:18:31 +00:00
db.table('users').where('token', token).then((user) => {
2017-03-17 04:14:24 +00:00
if (user.length === 0) return res.json({ success: false, description: 'Token mismatch' })
2017-01-29 07:18:31 +00:00
return res.json({ success: true, token: token })
2017-03-17 04:14:24 +00:00
}).catch(function(error) { console.log(error); res.json({ success: false, description: 'error' }) })
2017-01-29 07:18:31 +00:00
}
2017-03-17 04:14:24 +00:00
tokenController.change = function(req, res, next) {
2017-01-30 01:06:52 +00:00
let token = req.headers.token
2017-03-17 04:14:24 +00:00
if (token === undefined) return res.status(401).json({ success: false, description: 'No token provided' })
2017-01-30 01:06:52 +00:00
let newtoken = randomstring.generate(64)
2017-03-17 04:14:24 +00:00
2017-01-30 01:06:52 +00:00
db.table('users').where('token', token).update({
token: newtoken,
timestamp: Math.floor(Date.now() / 1000)
2017-01-30 07:42:15 +00:00
}).then(() => {
2017-01-30 01:06:52 +00:00
res.json({ success: true, token: newtoken })
2017-03-17 04:14:24 +00:00
}).catch(function(error) { console.log(error); res.json({ success: false, description: 'error' }) })
}
2017-03-17 04:14:24 +00:00
module.exports = tokenController