2017-01-17 20:54:25 +01:00
|
|
|
const config = require('../config.js')
|
|
|
|
const db = require('knex')(config.database)
|
|
|
|
|
|
|
|
let tokenController = {}
|
|
|
|
|
|
|
|
tokenController.verify = function(req, res, next){
|
|
|
|
|
2017-01-29 08:18:31 +01:00
|
|
|
if(req.body.token === undefined) return res.json({ success: false, description: 'No token provided' })
|
|
|
|
let token = req.body.token
|
2017-01-17 20:54:25 +01:00
|
|
|
|
2017-01-29 08:18:31 +01:00
|
|
|
db.table('users').where('token', token).then((user) => {
|
|
|
|
if(user.length === 0) return res.json({ success: false, description: 'Token mismatch' })
|
2017-01-17 20:54:25 +01:00
|
|
|
return res.json({ success: true })
|
2017-01-29 08:18:31 +01:00
|
|
|
}).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })
|
2017-01-17 20:54:25 +01:00
|
|
|
}
|
|
|
|
|
2017-01-18 08:51:42 +01:00
|
|
|
tokenController.list = function(req, res, next){
|
|
|
|
|
2017-01-29 08:18:31 +01:00
|
|
|
if(req.headers.auth === undefined) return res.json({ success: false, description: 'No token provided' })
|
|
|
|
let token = req.headers.auth
|
|
|
|
|
|
|
|
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'}) })
|
|
|
|
|
2017-01-18 08:51:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
tokenController.change = function(req, res, next){
|
|
|
|
if(req.headers.auth !== config.adminToken)
|
2017-01-19 06:37:35 +01:00
|
|
|
return res.status(401).json({ success: false, description: 'not-authorized'})
|
2017-01-18 08:51:42 +01:00
|
|
|
|
2017-01-19 06:37:35 +01:00
|
|
|
let type = req.body.type
|
|
|
|
let token = req.body.token
|
2017-01-18 08:51:42 +01:00
|
|
|
|
|
|
|
if(type === undefined) return res.json({ success: false, description: 'No type provided.' })
|
|
|
|
if(token === undefined) return res.json({ success: false, description: 'No token provided.' })
|
|
|
|
if(type !== 'client' && type !== 'admin') return res.json({ success: false, description: 'Wrong type provided.' })
|
|
|
|
|
|
|
|
db.table('tokens').where('name', type).update({ value: token, timestamp: Math.floor(Date.now() / 1000) })
|
|
|
|
.then(() => {
|
|
|
|
|
|
|
|
if(type === 'client')
|
|
|
|
config.clientToken = token
|
|
|
|
else if(type === 'admin')
|
|
|
|
config.adminToken = token
|
|
|
|
|
|
|
|
res.json({ success: true })
|
2017-01-20 07:28:26 +01:00
|
|
|
}).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })
|
2017-01-18 08:51:42 +01:00
|
|
|
}
|
|
|
|
|
2017-01-17 20:54:25 +01:00
|
|
|
module.exports = tokenController
|