UserOauthController.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. define(['base'], App =>
  2. App.controller('UserOauthController', function(
  3. $http,
  4. $scope,
  5. $q,
  6. _,
  7. UserOauthDataService
  8. ) {
  9. const _monitorRequest = function(promise) {
  10. $scope.ui.hasError = false
  11. $scope.ui.isLoadingV1Ids = true
  12. promise
  13. .catch(response => {
  14. $scope.ui.hasError = true
  15. $scope.ui.errorMessage =
  16. response && response.data && response.data.message
  17. ? response.data.message
  18. : 'error'
  19. })
  20. .finally(() => {
  21. $scope.ui.isLoadingV1Ids = false
  22. })
  23. return promise
  24. }
  25. const _reset = function() {
  26. $scope.ui = {
  27. hasError: false,
  28. errorMessage: '',
  29. isLoadingV1Ids: false
  30. }
  31. $scope.providers = window.oauthProviders
  32. $scope.thirdPartyIds = window.thirdPartyIds
  33. // until oauthUseV2=true, we will use OAuth data via v1 DB,
  34. // except for Collabratec, which is only writing to the v2 DB.
  35. // $scope.v2ThirdPartyIds is required for Collabratec,
  36. // and only until v2 is authoritative. Though, we should leave this
  37. // until we stop double writes, in case we need to flip.
  38. // Double writes for OAuth will stop when oauthFallback=false
  39. $scope.v2ThirdPartyIds = window.thirdPartyIds
  40. }
  41. const _getUserV1OauthProviders = () => {
  42. $scope.ui.isLoadingV1Ids = true
  43. return _monitorRequest(UserOauthDataService.getUserOauthV1()).then(
  44. thirdPartyIds => {
  45. $scope.thirdPartyIds = thirdPartyIds
  46. }
  47. )
  48. }
  49. const _unlinkError = (providerId, err) => {
  50. $scope.providers[providerId].ui.hasError = true
  51. $scope.providers[providerId].ui.errorMessage =
  52. err && err.data && err.data.message ? err.data.message : 'error'
  53. }
  54. $scope.unlink = providerId => {
  55. if (window.ExposedSettings.isOverleaf) {
  56. // UI
  57. $scope.providers[providerId].ui = {
  58. hasError: false,
  59. isProcessing: true
  60. }
  61. // Data for update
  62. const data = {
  63. _csrf: window.csrfToken,
  64. link: false,
  65. providerId
  66. }
  67. $http
  68. .post('/user/oauth-unlink', data)
  69. .catch(error => {
  70. $scope.providers[providerId].ui.isProcessing = false
  71. _unlinkError(providerId, error)
  72. })
  73. .then(response => {
  74. $scope.providers[providerId].ui.isProcessing = false
  75. if (response.status === 200) {
  76. $scope.thirdPartyIds[providerId] = null
  77. // v2thirdPartyIds below can be removed post user c11n
  78. $scope.v2ThirdPartyIds[providerId] = null
  79. } else {
  80. _unlinkError(providerId, response)
  81. }
  82. })
  83. }
  84. }
  85. _reset()
  86. if (!window.oauthUseV2) {
  87. _getUserV1OauthProviders()
  88. }
  89. }))