users.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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} Authorization The valid JWT token provided by the {post} /users/login resource
  46. * @apiHeaderExample {string} Authorization header example:
  47. * "Authorization": "JWT eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiNTVlNmU0ZTAwNTIzMGY0OTI3MWM3MDc4IiwiaWF0IjoxNDQxMTk1MjMyfQ.eWh9nuXVVSVDKKCmTMDoc9FBU55-KgkiOJH1hrdQRTQ"
  48. *
  49. * @apiSuccessExample Success-Response:
  50. * HTTP/1.1 200 OK
  51. *
  52. */
  53. app.delete('/api/users/login', UserController.logout);
  54. /**
  55. * @api {post} /users Registration
  56. * @apiVersion 1.0.0
  57. * @apiName Registration
  58. * @apiGroup Users
  59. *
  60. * @apiHeader {String} Content-Type application/json
  61. *
  62. * @apiParam {String} username User's username
  63. * @apiParam {String} password User's password
  64. * @apiParam {String} [language='en'] User's default language
  65. * @apiParamExample {json} Request-Example:
  66. * {
  67. * "username": "John",
  68. * "password": "s3cr3t",
  69. * "language": "en"
  70. * }
  71. *
  72. * @apiSuccess (201) {String} username Username of the User.
  73. * @apiSuccess (201) {String} token The JWT valid token.
  74. * @apiSuccessExample Success-Response:
  75. * HTTP/1.1 201 OK
  76. * {
  77. * "username": "John",
  78. * "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiNTVlNmU0ZTAwNTIzMGY0OTI3MWM3MDc4IiwiaWF0IjoxNDQxMTk1MjMyfQ.eWh9nuXVVSVDKKCmTMDoc9FBU55-KgkiOJH1hrdQRTQ"
  79. * }
  80. *
  81. * @apiError (409) {json} UserAlreadyExist The user can't be found.
  82. * @apiErrorExample {json} UserAlreadyExist:
  83. * HTTP/1.1 409 Not Found
  84. * {
  85. * "message": "Account already exists"
  86. * }
  87. *
  88. * @apiError (400) {json} BadRequest Validation errors.
  89. * @apiErrorExample {json} BadRequest:
  90. * HTTP/1.1 400 Bad Request
  91. * [
  92. * {
  93. * "field": "password",
  94. * "rule": "required",
  95. * "message": "Path `password` is required."
  96. * },
  97. * {
  98. * "field": "username",
  99. * "rule": "required",
  100. * "message": "Path `username` is required."
  101. * }
  102. * ]
  103. *
  104. *
  105. */
  106. app.post('/api/users', UserController.subscribe);
  107. /**
  108. * @api {delete} /users Unregistration
  109. * @apiVersion 1.0.0
  110. * @apiName Unregistration
  111. * @apiGroup Users
  112. *
  113. * @apiHeader {String} Content-Type application/json
  114. * @apiHeader {String} Authorization The valid JWT token provided by the {post} /users/login resource
  115. * @apiHeaderExample {string} Authorization header example:
  116. * "Authorization": "JWT eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiNTVlNmU0ZTAwNTIzMGY0OTI3MWM3MDc4IiwiaWF0IjoxNDQxMTk1MjMyfQ.eWh9nuXVVSVDKKCmTMDoc9FBU55-KgkiOJH1hrdQRTQ"
  117. *
  118. * @apiSuccess (204) -
  119. * @apiSuccessExample Success-Response:
  120. * HTTP/1.1 204 No Content
  121. *
  122. * @apiError (401) {json} AuthenticationFailed The user can't be found.
  123. * @apiErrorExample AuthenticationFailed:
  124. * HTTP/1.1 401 Not Found
  125. * {
  126. * "message": "Authentication failed"
  127. * }
  128. *
  129. */
  130. app.delete('/api/users', passport.jwt, UserController.unsubscribe);
  131. };