2019-04-23 10:19:52 -04:00
|
|
|
define(['base'], App =>
|
|
|
|
App.controller('UserOauthController', [
|
2019-05-20 11:57:58 -04:00
|
|
|
'$http',
|
2019-04-23 10:19:52 -04:00
|
|
|
'$scope',
|
|
|
|
'$q',
|
|
|
|
'_',
|
|
|
|
'UserOauthDataService',
|
2019-05-20 11:57:58 -04:00
|
|
|
function($http, $scope, $q, _, UserOauthDataService) {
|
2019-04-23 10:19:52 -04:00
|
|
|
const _monitorRequest = function(promise) {
|
|
|
|
$scope.ui.hasError = false
|
2019-05-20 11:57:58 -04:00
|
|
|
$scope.ui.isLoadingV1Ids = true
|
2019-04-23 10:19:52 -04:00
|
|
|
promise
|
|
|
|
.catch(response => {
|
|
|
|
$scope.ui.hasError = true
|
|
|
|
$scope.ui.errorMessage =
|
|
|
|
response && response.data && response.data.message
|
|
|
|
? response.data.message
|
|
|
|
: 'error'
|
|
|
|
})
|
|
|
|
.finally(() => {
|
2019-05-20 11:57:58 -04:00
|
|
|
$scope.ui.isLoadingV1Ids = false
|
2019-04-23 10:19:52 -04:00
|
|
|
})
|
|
|
|
return promise
|
|
|
|
}
|
|
|
|
const _reset = function() {
|
|
|
|
$scope.ui = {
|
|
|
|
hasError: false,
|
|
|
|
errorMessage: '',
|
2019-05-20 11:57:58 -04:00
|
|
|
isLoadingV1Ids: false
|
2019-04-23 10:19:52 -04:00
|
|
|
}
|
2019-05-20 11:57:58 -04:00
|
|
|
$scope.providers = window.oauthProviders
|
|
|
|
$scope.thirdPartyIds = window.thirdPartyIds
|
2019-06-25 07:08:24 -04:00
|
|
|
// until oauthUseV2=true, we will use OAuth data via v1 DB,
|
|
|
|
// except for Collabratec, which is only writing to the v2 DB.
|
|
|
|
// $scope.v2ThirdPartyIds is required for Collabratec,
|
|
|
|
// and only until v2 is authoritative. Though, we should leave this
|
|
|
|
// until we stop double writes, in case we need to flip.
|
|
|
|
// Double writes for OAuth will stop when oauthFallback=false
|
2019-05-20 11:57:58 -04:00
|
|
|
$scope.v2ThirdPartyIds = window.thirdPartyIds
|
2019-04-23 10:19:52 -04:00
|
|
|
}
|
|
|
|
const _getUserV1OauthProviders = () => {
|
2019-05-20 11:57:58 -04:00
|
|
|
$scope.ui.isLoadingV1Ids = true
|
2019-04-23 10:19:52 -04:00
|
|
|
return _monitorRequest(UserOauthDataService.getUserOauthV1()).then(
|
2019-05-20 11:57:58 -04:00
|
|
|
thirdPartyIds => {
|
|
|
|
$scope.thirdPartyIds = thirdPartyIds
|
2019-04-23 10:19:52 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
2019-05-20 11:57:58 -04:00
|
|
|
const _unlinkError = (providerId, err) => {
|
|
|
|
$scope.providers[providerId].ui.hasError = true
|
|
|
|
$scope.providers[providerId].ui.errorMessage =
|
|
|
|
err && err.data && err.data.message ? err.data.message : 'error'
|
|
|
|
}
|
|
|
|
|
|
|
|
$scope.unlink = providerId => {
|
|
|
|
if (window.ExposedSettings.isOverleaf) {
|
|
|
|
// UI
|
|
|
|
$scope.providers[providerId].ui = {
|
|
|
|
hasError: false,
|
|
|
|
isProcessing: true
|
|
|
|
}
|
|
|
|
// Data for update
|
|
|
|
const data = {
|
2019-05-28 05:17:00 -04:00
|
|
|
_csrf: window.csrfToken,
|
2019-05-20 11:57:58 -04:00
|
|
|
link: false,
|
|
|
|
providerId
|
|
|
|
}
|
|
|
|
$http
|
|
|
|
.post('/user/oauth-unlink', data)
|
|
|
|
.catch(error => {
|
|
|
|
$scope.providers[providerId].ui.isProcessing = false
|
|
|
|
_unlinkError(providerId, error)
|
|
|
|
})
|
|
|
|
.then(response => {
|
|
|
|
$scope.providers[providerId].ui.isProcessing = false
|
|
|
|
if (response.status === 200) {
|
|
|
|
$scope.thirdPartyIds[providerId] = null
|
|
|
|
// v2thirdPartyIds below can be removed post user c11n
|
|
|
|
$scope.v2ThirdPartyIds[providerId] = null
|
|
|
|
} else {
|
|
|
|
_unlinkError(providerId, response)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-23 10:19:52 -04:00
|
|
|
_reset()
|
2019-06-25 07:08:24 -04:00
|
|
|
if (!window.oauthUseV2) {
|
2019-05-20 11:57:58 -04:00
|
|
|
_getUserV1OauthProviders()
|
|
|
|
}
|
2019-04-23 10:19:52 -04:00
|
|
|
}
|
|
|
|
]))
|