river-of-ebooks/api/controllers/UserController.js

51 lines
1.2 KiB
JavaScript
Raw Permalink Normal View History

2018-11-02 00:21:36 +00:00
/**
* UserController
*
* @description :: Server-side logic for managing Users
* @help :: See http://links.sailsjs.org/docs/controllers
*/
2019-03-05 23:55:31 +00:00
const { generateToken } = require('../util')
const HttpError = require('../errors/HttpError')
2018-11-02 00:21:36 +00:00
module.exports = {
/**
* @override
*/
create: async function (req, res, next) {
2018-11-07 20:06:36 +00:00
const passportHelper = await sails.helpers.passport()
2018-11-12 22:12:16 +00:00
passportHelper.protocols.local.register(req.body, function (err, user) {
if (err) {
return res.status(500).json({
error: err.toString()
})
}
2018-11-07 20:06:36 +00:00
res.json(user)
})
2018-11-02 00:21:36 +00:00
},
2019-02-04 21:08:45 +00:00
edit: async function (req, res, next) {
2018-11-07 20:06:36 +00:00
const passportHelper = await sails.helpers.passport()
2018-11-12 22:12:16 +00:00
passportHelper.protocols.local.update(req.body, function (err, user) {
if (err) {
return res.status(500).json({
error: err.toString()
})
}
2018-11-07 20:06:36 +00:00
res.json(user)
})
2018-11-02 00:21:36 +00:00
},
me: function (req, res) {
2018-11-07 20:06:36 +00:00
res.json(req.user)
2019-03-05 23:55:31 +00:00
},
regenerateSigningSecret: async function (req, res) {
try {
2019-03-06 00:56:55 +00:00
const user = await User.updateOne({ id: req.user.id }, { signing_secret: await generateToken({ bytes: 24 }) })
2019-03-05 23:55:31 +00:00
return res.json(user)
} catch (e) {
return (new HttpError(500, e.message)).send(res)
}
2018-11-02 00:21:36 +00:00
}
2018-11-07 20:06:36 +00:00
}