diff --git a/src/controllers/auth.js b/src/controllers/auth.js deleted file mode 100644 index bfef202..0000000 --- a/src/controllers/auth.js +++ /dev/null @@ -1,78 +0,0 @@ -import Router from 'koa-router' -import passport from 'koa-passport' -import jwt from 'jsonwebtoken' -import config from '../../config' - -const router = new Router({ prefix: '/auth' }) - -/** - * @apiDefine TokenError - * @apiError Unauthorized Invalid JWT token - * - * @apiErrorExample {json} Unauthorized-Error: - * HTTP/1.1 401 Unauthorized - * { - * "status": 401, - * "error": "Unauthorized" - * } - */ - -/** - * @api {post} /auth Authenticate user - * @apiVersion 1.0.0 - * @apiName AuthUser - * @apiGroup Auth - * - * @apiParam {String} username User username. - * @apiParam {String} password User password. - * - * @apiExample Example usage: - * curl -H "Content-Type: application/json" -X POST -d '{ "username": "johndoe@gmail.com", "password": "foo" }' localhost:5000/auth - * - * @apiSuccess {Object} user User object - * @apiSuccess {ObjectId} user._id User id - * @apiSuccess {String} user.name User name - * @apiSuccess {String} user.username User username - * @apiSuccess {String} token Encoded JWT - * - * @apiSuccessExample {json} Success-Response: - * HTTP/1.1 200 OK - * { - * "user": { - * "_id": "56bd1da600a526986cf65c80" - * "username": "foo" - * "username": "johndoe" - * }, - * "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ" - * } - * - * @apiError Unauthorized Incorrect credentials - * - * @apiErrorExample {json} Error-Response: - * HTTP/1.1 401 Unauthorized - * { - * "status": 401, - * "error": "Unauthorized" - * } - */ -router.post('/', async (ctx, next) => - passport.authenticate('local', (user) => { - if (!user) { - ctx.throw(401) - } - - const token = jwt.sign({ id: user.id }, config.token) - - const response = user.toJSON() - - delete response.password - delete response.salt - - ctx.body = { - token, - user: response - } - })(ctx, next) -) - -export default router diff --git a/src/controllers/index.js b/src/controllers/index.js deleted file mode 100644 index a49672d..0000000 --- a/src/controllers/index.js +++ /dev/null @@ -1,14 +0,0 @@ -import glob from 'glob' - -exports = module.exports = function Controllers(app) { - glob(`${__dirname}/*.js`, { ignore: '**/index.js' }, (err, matches) => { - if (err) { throw err } - - matches.forEach((file) => { - const controller = require(file).default - app - .use(controller.routes()) - .use(controller.allowedMethods()) - }) - }) -} diff --git a/src/controllers/users.js b/src/controllers/users.js deleted file mode 100644 index 70739e3..0000000 --- a/src/controllers/users.js +++ /dev/null @@ -1,258 +0,0 @@ -import Router from 'koa-router' -import User from '../models/users' -import config from '../../config' -import jwt from 'jsonwebtoken' -import { ensureUser } from '../middleware/validators' - -const router = new Router({ prefix: '/users' }) - -/** - * @api {get} /users Get all users - * @apiPermission user - * @apiVersion 1.0.0 - * @apiName GetUsers - * @apiGroup Users - * - * @apiExample Example usage: - * curl -H "Content-Type: application/json" -X GET localhost:5000/users - * - * @apiSuccess {Object[]} users Array of user objects - * @apiSuccess {ObjectId} users._id User id - * @apiSuccess {String} users.name User name - * @apiSuccess {String} users.username User username - * - * @apiSuccessExample {json} Success-Response: - * HTTP/1.1 200 OK - * { - * "users": [{ - * "_id": "56bd1da600a526986cf65c80" - * "name": "John Doe" - * "username": "johndoe" - * }] - * } - * - * @apiUse TokenError - */ -router.get('/', - ensureUser, - async (ctx) => { - const users = await User.find({}, '-password -salt') - ctx.body = users - } -) - -/** - * @api {get} /users/:id Get user by id - * @apiPermission user - * @apiVersion 1.0.0 - * @apiName GetUser - * @apiGroup Users - * - * @apiExample Example usage: - * curl -H "Content-Type: application/json" -X GET localhost:5000/users/56bd1da600a526986cf65c80 - * - * @apiSuccess {Object} users User object - * @apiSuccess {ObjectId} users._id User id - * @apiSuccess {String} users.name User name - * @apiSuccess {String} users.username User username - * - * @apiSuccessExample {json} Success-Response: - * HTTP/1.1 200 OK - * { - * "user": { - * "_id": "56bd1da600a526986cf65c80" - * "name": "John Doe" - * "username": "johndoe" - * } - * } - * - * @apiUse TokenError - */ -router.get('/:id', - ensureUser, - async (ctx) => { - try { - const user = await User.findById(ctx.params.id, '-password -salt') - if (!user) { - ctx.throw(404) - } - - ctx.body = user - } catch (err) { - if (err === 404 || err.name === 'CastError') { - ctx.throw(404) - } - - ctx.throw(500) - } - } -) - -/** - * @api {post} /users Create a new user - * @apiPermission - * @apiVersion 1.0.0 - * @apiName CreateUser - * @apiGroup Users - * - * @apiExample Example usage: - * curl -H "Content-Type: application/json" -X POST -d '{ "user": { "username": "johndoe", "password": "secretpasas" } }' localhost:5000/users - * - * @apiParam {Object} user User object (required) - * @apiParam {String} user.username Username. - * @apiParam {String} user.password Password. - * - * @apiSuccess {Object} users User object - * @apiSuccess {ObjectId} users._id User id - * @apiSuccess {String} users.name User name - * @apiSuccess {String} users.username User username - * - * @apiSuccessExample {json} Success-Response: - * HTTP/1.1 200 OK - * { - * "user": { - * "_id": "56bd1da600a526986cf65c80" - * "name": "John Doe" - * "username": "johndoe" - * } - * } - * - * @apiError UnprocessableEntity Missing required parameters - * - * @apiErrorExample {json} Error-Response: - * HTTP/1.1 422 Unprocessable Entity - * { - * "status": 422, - * "error": "Unprocessable Entity" - * } - */ -router.post('/', - async (ctx) => { - const user = new User(ctx.request.body.user) - try { - await user.save() - const token = jwt.sign({ id: user.id }, config.token) - - const response = user.toJSON() - - delete response.password - delete response.salt - - ctx.body = { - user: response, - token - } - } catch (err) { - ctx.throw(422, err) - } - } -) - -/** - * @api {put} /users/:id Update a user - * @apiPermission - * @apiVersion 1.0.0 - * @apiName UpdateUser - * @apiGroup Users - * - * @apiExample Example usage: - * curl -H "Content-Type: application/json" -X PUT -d '{ "user": { "name": "Cool new Name" } }' localhost:5000/users/56bd1da600a526986cf65c80 - * - * @apiParam {Object} user User object (required) - * @apiParam {String} user.name Name. - * @apiParam {String} user.username Username. - * - * @apiSuccess {Object} users User object - * @apiSuccess {ObjectId} users._id User id - * @apiSuccess {String} users.name Updated name - * @apiSuccess {String} users.username Updated username - * - * @apiSuccessExample {json} Success-Response: - * HTTP/1.1 200 OK - * { - * "user": { - * "_id": "56bd1da600a526986cf65c80" - * "name": "Cool new name" - * "username": "johndoe" - * } - * } - * - * @apiError UnprocessableEntity Missing required parameters - * - * @apiErrorExample {json} Error-Response: - * HTTP/1.1 422 Unprocessable Entity - * { - * "status": 422, - * "error": "Unprocessable Entity" - * } - * - * @apiUse TokenError - */ -router.put('/:id', - ensureUser, - async (ctx) => { - try { - const user = await User.findById(ctx.params.id, '-password -salt') - if (!user) { - ctx.throw(404) - } - - Object.assign(user, ctx.request.body.user) - - await user.save() - ctx.body = { - user - } - } catch (err) { - if (err === 404 || err.name === 'CastError') { - ctx.throw(404) - } - - ctx.throw(500) - } - } -) - -/** - * @api {delete} /users/:id Delete a user - * @apiPermission - * @apiVersion 1.0.0 - * @apiName DeleteUser - * @apiGroup Users - * - * @apiExample Example usage: - * curl -H "Content-Type: application/json" -X DELETE localhost:5000/users/56bd1da600a526986cf65c80 - * - * @apiSuccess {StatusCode} 200 - * - * @apiSuccessExample {json} Success-Response: - * HTTP/1.1 200 OK - * { - * "status": 200 - * } - * - * @apiUse TokenError - */ -router.delete('/:id', - ensureUser, - async (ctx) => { - try { - const user = await User.findById(ctx.params.id) - if (!user) { - ctx.throw(404) - } - - await user.remove() - - ctx.body = 200 - } catch (err) { - if (err === 404 || err.name === 'CastError') { - ctx.throw(404) - } - - ctx.throw(500) - } - } -) - -export default router