overleaf/services/web/frontend/js/main/token-access.js
Timothée Alby 7f722a006c Merge pull request #8571 from overleaf/ta-token-access-page
Require User Interaction on Token Access Page

GitOrigin-RevId: 2f4c00ba75ebd6bd87d3e770ec8223d736344f5b
2022-07-29 08:03:39 +00:00

89 lines
2.5 KiB
JavaScript

import App from '../base'
App.controller(
'TokenAccessPageController',
($scope, $http, $location, localStorage) => {
window.S = $scope
$scope.mode = 'accessAttempt' // 'accessAttempt' | 'v1Import' | 'requireAccept'
$scope.v1ImportData = null
$scope.requireAccept = null
$scope.accessInFlight = false
$scope.accessSuccess = false
$scope.accessError = false
$scope.currentPath = () => {
return $location.path()
}
$scope.buildZipDownloadPath = projectId => {
return `/overleaf/project/${projectId}/download/zip`
}
$scope.getProjectName = () => {
if ($scope.v1ImportData?.name) {
return $scope.v1ImportData.name
} else if ($scope.requireAccept?.projectName) {
return $scope.requireAccept.projectName
} else {
return 'This project'
}
}
$scope.postConfirmedByUser = () => {
$scope.post(true)
}
$scope.post = (confirmedByUser = false) => {
$scope.mode = 'accessAttempt'
const textData = $('#overleaf-token-access-data').text()
const parsedData = JSON.parse(textData)
const { postUrl, csrfToken } = parsedData
$scope.accessInFlight = true
$http({
method: 'POST',
url: postUrl,
data: {
_csrf: csrfToken,
confirmedByUser,
},
}).then(
function successCallback(response) {
$scope.accessInFlight = false
$scope.accessError = false
const { data } = response
if (data.redirect) {
const redirect = response.data.redirect
if (!redirect) {
console.warn(
'no redirect supplied in success response data',
response
)
$scope.accessError = true
return
}
window.location.replace(redirect)
} else if (data.v1Import) {
$scope.mode = 'v1Import'
$scope.v1ImportData = data.v1Import
} else if (data.requireAccept) {
$scope.mode = 'requireAccept'
$scope.requireAccept = data.requireAccept
} else {
console.warn(
'invalid data from server in success response',
response
)
$scope.accessError = true
}
},
function errorCallback(response) {
console.warn('error response from server', response)
$scope.accessInFlight = false
$scope.accessError = response.status === 404 ? 'not_found' : 'error'
}
)
}
}
)