users.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. var passport = require('../security/passport'),
  2. UserController = require('../controllers/users');
  3. module.exports = function(app) {
  4. /**
  5. * @api {post} /users/login Login
  6. * @apiVersion 1.0.0
  7. * @apiName Login
  8. * @apiGroup Users
  9. *
  10. * @apiHeader {String} Content-Type application/json
  11. *
  12. * @apiParam {String} username User's username
  13. * @apiParam {String} password User's password
  14. * @apiParamExample {json} Request-Example:
  15. * {
  16. * "username": "John",
  17. * "password": "s3cr3t"
  18. * }
  19. *
  20. * @apiSuccess {String} username Username of the User.
  21. * @apiSuccess {String} token The JWT valid token.
  22. *
  23. * @apiSuccessExample Success-Response:
  24. * HTTP/1.1 200 OK
  25. * {
  26. * "username": "John",
  27. * "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiNTVlNmU0ZTAwNTIzMGY0OTI3MWM3MDc4IiwiaWF0IjoxNDQxMTk1MjMyfQ.eWh9nuXVVSVDKKCmTMDoc9FBU55-KgkiOJH1hrdQRTQ"
  28. * }
  29. *
  30. * @apiError (401) {json} AuthenticationFailed The user can't be found.
  31. * @apiErrorExample AuthenticationFailed:
  32. * HTTP/1.1 401 Not Found
  33. * {
  34. * "message": "Authentication failed"
  35. * }
  36. *
  37. */
  38. app.post('/api/users/login', passport.local, UserController.login);
  39. /**
  40. * @api {delete} /users/login Logout
  41. * @apiVersion 1.0.0
  42. * @apiName Logout
  43. * @apiGroup Users
  44. *
  45. * @apiHeader {String} Authentication The valid JWT token provided by the {post} /users/login resource
  46. * @apiHeaderExample {string} Authentication header example:
  47. * {
  48. * "Authentication": "JWT eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiNTVlNmU0ZTAwNTIzMGY0OTI3MWM3MDc4IiwiaWF0IjoxNDQxMTk1MjMyfQ.eWh9nuXVVSVDKKCmTMDoc9FBU55-KgkiOJH1hrdQRTQ"
  49. * }
  50. *
  51. * @apiSuccessExample Success-Response:
  52. * HTTP/1.1 200 OK
  53. *
  54. */
  55. app.delete('/api/users/login', UserController.logout);
  56. /**
  57. * @api {post} /users Registration
  58. * @apiVersion 1.0.0
  59. * @apiName Registration
  60. * @apiGroup Users
  61. *
  62. * @apiHeader {String} Content-Type application/json
  63. *
  64. * @apiParam {String} username User's username
  65. * @apiParam {String} password User's password
  66. * @apiParam {String} [language='en'] User's default language
  67. * @apiParamExample {json} Request-Example:
  68. * {
  69. * "username": "John",
  70. * "password": "s3cr3t",
  71. * "language": "en"
  72. * }
  73. *
  74. * @apiSuccess (201) {String} username Username of the User.
  75. * @apiSuccess (201) {String} token The JWT valid token.
  76. * @apiSuccessExample Success-Response:
  77. * HTTP/1.1 201 OK
  78. * {
  79. * "username": "John",
  80. * "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiNTVlNmU0ZTAwNTIzMGY0OTI3MWM3MDc4IiwiaWF0IjoxNDQxMTk1MjMyfQ.eWh9nuXVVSVDKKCmTMDoc9FBU55-KgkiOJH1hrdQRTQ"
  81. * }
  82. *
  83. * @apiError (409) {json} UserAlreadyExist The user can't be found.
  84. * @apiErrorExample {json} UserAlreadyExist:
  85. * HTTP/1.1 409 Not Found
  86. * {
  87. * "message": "Account already exists"
  88. * }
  89. *
  90. * @apiError (400) {json} BadRequest Validation errors.
  91. * @apiErrorExample {json} BadRequest:
  92. * HTTP/1.1 400 Bad Request
  93. * [
  94. * {
  95. * "field": "password",
  96. * "rule": "required",
  97. * "message": "Path `password` is required."
  98. * },
  99. * {
  100. * "field": "username",
  101. * "rule": "required",
  102. * "message": "Path `username` is required."
  103. * }
  104. * ]
  105. *
  106. *
  107. */
  108. app.post('/api/users', UserController.subscribe);
  109. /**
  110. * @api {delete} /users Unregistration
  111. * @apiVersion 1.0.0
  112. * @apiName Unregistration
  113. * @apiGroup Users
  114. *
  115. * @apiHeader {String} Content-Type application/json
  116. * @apiHeader {String} Authentication The valid JWT token provided by the {post} /users/login resource
  117. * @apiHeaderExample {string} Authentication header example:
  118. * {
  119. * "Authentication": "JWT eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiNTVlNmU0ZTAwNTIzMGY0OTI3MWM3MDc4IiwiaWF0IjoxNDQxMTk1MjMyfQ.eWh9nuXVVSVDKKCmTMDoc9FBU55-KgkiOJH1hrdQRTQ"
  120. * }
  121. *
  122. * @apiSuccess (204) -
  123. * @apiSuccessExample Success-Response:
  124. * HTTP/1.1 204 No Content
  125. *
  126. * @apiError (401) {json} AuthenticationFailed The user can't be found.
  127. * @apiErrorExample AuthenticationFailed:
  128. * HTTP/1.1 401 Not Found
  129. * {
  130. * "message": "Authentication failed"
  131. * }
  132. *
  133. */
  134. app.delete('/api/users', passport.jwt, UserController.unsubscribe);
  135. };