filesafe/controllers/tokenController.js

47 lines
1.6 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 = {}
tokenController.verify = function(req, res, next){
2017-01-29 07:18:31 +00:00
if(req.body.token === undefined) return res.json({ success: false, description: 'No token provided' })
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) => {
if(user.length === 0) return res.json({ success: false, description: 'Token mismatch' })
2017-01-17 19:54:25 +00:00
return res.json({ success: true })
2017-01-29 07:18:31 +00:00
}).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
}
tokenController.list = function(req, res, next){
2017-01-30 01:06:52 +00:00
let token = req.headers.token
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) => {
if(user.length === 0) return res.json({ success: false, description: 'Token mismatch' })
return res.json({ success: true, token: token })
}).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })
}
tokenController.change = function(req, res, next){
2017-01-30 01:06:52 +00:00
let token = req.headers.token
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)
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 })
}).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
module.exports = tokenController