V1Handler.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* eslint-disable
  2. camelcase,
  3. max-len,
  4. no-unused-vars,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * DS103: Rewrite code to no longer use __guard__
  12. * DS207: Consider shorter variations of null checks
  13. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  14. */
  15. let V1Handler
  16. const OError = require('@overleaf/o-error')
  17. const V1Api = require('./V1Api')
  18. const Settings = require('@overleaf/settings')
  19. const logger = require('@overleaf/logger')
  20. module.exports = V1Handler = {
  21. authWithV1(email, password, callback) {
  22. return V1Api.request(
  23. {
  24. method: 'POST',
  25. url: '/api/v1/sharelatex/login',
  26. json: { email, password },
  27. expectedStatusCodes: [403],
  28. },
  29. function (err, response, body) {
  30. if (err != null) {
  31. OError.tag(err, '[V1Handler] error while talking to v1 login api', {
  32. email,
  33. })
  34. return callback(err)
  35. }
  36. if ([200, 403].includes(response.statusCode)) {
  37. const isValid = body.valid
  38. const userProfile = body.user_profile
  39. logger.log(
  40. {
  41. email,
  42. isValid,
  43. v1UserId: __guard__(
  44. body != null ? body.user_profile : undefined,
  45. x => x.id
  46. ),
  47. },
  48. '[V1Handler] got response from v1 login api'
  49. )
  50. return callback(null, isValid, userProfile)
  51. } else {
  52. err = new Error(
  53. `Unexpected status from v1 login api: ${response.statusCode}`
  54. )
  55. return callback(err)
  56. }
  57. }
  58. )
  59. },
  60. doPasswordReset(v1_user_id, password, callback) {
  61. return V1Api.request(
  62. {
  63. method: 'POST',
  64. url: '/api/v1/sharelatex/reset_password',
  65. json: {
  66. user_id: v1_user_id,
  67. password,
  68. },
  69. expectedStatusCodes: [200],
  70. },
  71. function (err, response, body) {
  72. if (err != null) {
  73. OError.tag(err, 'error while talking to v1 password reset api', {
  74. v1_user_id,
  75. })
  76. return callback(err, false)
  77. }
  78. if ([200].includes(response.statusCode)) {
  79. logger.log(
  80. { v1_user_id, changed: true },
  81. 'got success response from v1 password reset api'
  82. )
  83. return callback(null, true)
  84. } else {
  85. err = new Error(
  86. `Unexpected status from v1 password reset api: ${response.statusCode}`
  87. )
  88. return callback(err, false)
  89. }
  90. }
  91. )
  92. },
  93. }
  94. function __guard__(value, transform) {
  95. return typeof value !== 'undefined' && value !== null
  96. ? transform(value)
  97. : undefined
  98. }