overleaf/services/web/frontend/js/main/oauth/controllers/UserOauthController.js
Alasdair Smith e035d526d6 Merge pull request #2287 from overleaf/as-frontend-rearch
Restructure frontend directories

GitOrigin-RevId: 5a1224ca186f4809df45680a80d374c1c318d7bf
2019-11-05 14:22:52 +00:00

48 lines
1.4 KiB
JavaScript

define(['base'], App =>
App.controller('UserOauthController', function($http, $scope, $q) {
const _reset = function() {
$scope.ui = {
hasError: false,
errorMessage: ''
}
$scope.providers = window.oauthProviders
$scope.thirdPartyIds = window.thirdPartyIds
}
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 = {
_csrf: window.csrfToken,
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
} else {
_unlinkError(providerId, response)
}
})
}
}
_reset()
}))