v3.0.0/controllers/tokenController.js

47 lines
1.7 KiB
JavaScript
Raw Normal View History

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