mirror of
https://github.com/BobbyWibowo/lolisafe.git
synced 2024-12-14 00:16:21 +00:00
refactor: authController pass errors
This commit is contained in:
parent
b3a304729f
commit
59c023588e
@ -5,7 +5,6 @@ const paths = require('./pathsController')
|
|||||||
const perms = require('./permissionController')
|
const perms = require('./permissionController')
|
||||||
const tokens = require('./tokenController')
|
const tokens = require('./tokenController')
|
||||||
const utils = require('./utilsController')
|
const utils = require('./utilsController')
|
||||||
const apiErrorsHandler = require('./handlers/apiErrorsHandler.js')
|
|
||||||
const ClientError = require('./utils/ClientError')
|
const ClientError = require('./utils/ClientError')
|
||||||
const ServerError = require('./utils/ServerError')
|
const ServerError = require('./utils/ServerError')
|
||||||
const config = require('./../config')
|
const config = require('./../config')
|
||||||
@ -31,8 +30,8 @@ const self = {
|
|||||||
// https://github.com/kelektiv/node.bcrypt.js/tree/v5.0.1#a-note-on-rounds
|
// https://github.com/kelektiv/node.bcrypt.js/tree/v5.0.1#a-note-on-rounds
|
||||||
const saltRounds = 10
|
const saltRounds = 10
|
||||||
|
|
||||||
self.verify = async (req, res, next) => {
|
self.verify = (req, res, next) => {
|
||||||
try {
|
Promise.resolve().then(async () => {
|
||||||
const username = typeof req.body.username === 'string'
|
const username = typeof req.body.username === 'string'
|
||||||
? req.body.username.trim()
|
? req.body.username.trim()
|
||||||
: ''
|
: ''
|
||||||
@ -59,13 +58,11 @@ self.verify = async (req, res, next) => {
|
|||||||
} else {
|
} else {
|
||||||
await res.json({ success: true, token: user.token })
|
await res.json({ success: true, token: user.token })
|
||||||
}
|
}
|
||||||
} catch (error) {
|
}).catch(next)
|
||||||
return apiErrorsHandler(error, req, res, next)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
self.register = async (req, res, next) => {
|
self.register = (req, res, next) => {
|
||||||
try {
|
Promise.resolve().then(async () => {
|
||||||
if (config.enableUserAccounts === false) {
|
if (config.enableUserAccounts === false) {
|
||||||
throw new ClientError('Registration is currently disabled.', { statusCode: 403 })
|
throw new ClientError('Registration is currently disabled.', { statusCode: 403 })
|
||||||
}
|
}
|
||||||
@ -110,13 +107,11 @@ self.register = async (req, res, next) => {
|
|||||||
tokens.onHold.delete(token)
|
tokens.onHold.delete(token)
|
||||||
|
|
||||||
await res.json({ success: true, token })
|
await res.json({ success: true, token })
|
||||||
} catch (error) {
|
}).catch(next)
|
||||||
return apiErrorsHandler(error, req, res, next)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
self.changePassword = async (req, res, next) => {
|
self.changePassword = (req, res, next) => {
|
||||||
try {
|
Promise.resolve().then(async () => {
|
||||||
const user = await utils.authorize(req)
|
const user = await utils.authorize(req)
|
||||||
|
|
||||||
const password = typeof req.body.password === 'string'
|
const password = typeof req.body.password === 'string'
|
||||||
@ -133,9 +128,7 @@ self.changePassword = async (req, res, next) => {
|
|||||||
.update('password', hash)
|
.update('password', hash)
|
||||||
|
|
||||||
await res.json({ success: true })
|
await res.json({ success: true })
|
||||||
} catch (error) {
|
}).catch(next)
|
||||||
return apiErrorsHandler(error, req, res, next)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
self.assertPermission = (user, target) => {
|
self.assertPermission = (user, target) => {
|
||||||
@ -148,8 +141,8 @@ self.assertPermission = (user, target) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
self.createUser = async (req, res, next) => {
|
self.createUser = (req, res, next) => {
|
||||||
try {
|
Promise.resolve().then(async () => {
|
||||||
const user = await utils.authorize(req)
|
const user = await utils.authorize(req)
|
||||||
|
|
||||||
const isadmin = perms.is(user, 'admin')
|
const isadmin = perms.is(user, 'admin')
|
||||||
@ -209,13 +202,11 @@ self.createUser = async (req, res, next) => {
|
|||||||
tokens.onHold.delete(token)
|
tokens.onHold.delete(token)
|
||||||
|
|
||||||
await res.json({ success: true, username, password, group })
|
await res.json({ success: true, username, password, group })
|
||||||
} catch (error) {
|
}).catch(next)
|
||||||
return apiErrorsHandler(error, req, res, next)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
self.editUser = async (req, res, next) => {
|
self.editUser = (req, res, next) => {
|
||||||
try {
|
Promise.resolve().then(async () => {
|
||||||
const user = await utils.authorize(req)
|
const user = await utils.authorize(req)
|
||||||
|
|
||||||
const isadmin = perms.is(user, 'admin')
|
const isadmin = perms.is(user, 'admin')
|
||||||
@ -263,18 +254,16 @@ self.editUser = async (req, res, next) => {
|
|||||||
const response = { success: true, update }
|
const response = { success: true, update }
|
||||||
if (password) response.update.password = password
|
if (password) response.update.password = password
|
||||||
await res.json(response)
|
await res.json(response)
|
||||||
} catch (error) {
|
}).catch(next)
|
||||||
return apiErrorsHandler(error, req, res, next)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
self.disableUser = async (req, res, next) => {
|
self.disableUser = (req, res, next) => {
|
||||||
req.body = { id: req.body.id, enabled: false }
|
req.body = { id: req.body.id, enabled: false }
|
||||||
return self.editUser(req, res, next)
|
return self.editUser(req, res, next)
|
||||||
}
|
}
|
||||||
|
|
||||||
self.deleteUser = async (req, res, next) => {
|
self.deleteUser = (req, res, next) => {
|
||||||
try {
|
Promise.resolve().then(async () => {
|
||||||
const user = await utils.authorize(req)
|
const user = await utils.authorize(req)
|
||||||
|
|
||||||
const isadmin = perms.is(user, 'admin')
|
const isadmin = perms.is(user, 'admin')
|
||||||
@ -336,17 +325,15 @@ self.deleteUser = async (req, res, next) => {
|
|||||||
utils.invalidateStatsCache('users')
|
utils.invalidateStatsCache('users')
|
||||||
|
|
||||||
await res.json({ success: true })
|
await res.json({ success: true })
|
||||||
} catch (error) {
|
}).catch(next)
|
||||||
return apiErrorsHandler(error, req, res, next)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
self.bulkDeleteUsers = async (req, res, next) => {
|
self.bulkDeleteUsers = (req, res, next) => {
|
||||||
// TODO
|
// TODO
|
||||||
}
|
}
|
||||||
|
|
||||||
self.listUsers = async (req, res, next) => {
|
self.listUsers = (req, res, next) => {
|
||||||
try {
|
Promise.resolve().then(async () => {
|
||||||
const user = await utils.authorize(req)
|
const user = await utils.authorize(req)
|
||||||
|
|
||||||
const isadmin = perms.is(user, 'admin')
|
const isadmin = perms.is(user, 'admin')
|
||||||
@ -385,9 +372,7 @@ self.listUsers = async (req, res, next) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
await res.json({ success: true, users, count })
|
await res.json({ success: true, users, count })
|
||||||
} catch (error) {
|
}).catch(next)
|
||||||
return apiErrorsHandler(error, req, res, next)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = self
|
module.exports = self
|
||||||
|
Loading…
Reference in New Issue
Block a user